Skip to content

Commit

Permalink
Version 0.1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
traderben committed Dec 6, 2023
1 parent 374c86e commit 51a235f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/basic_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main():

# Transfer 1 usd to the zero address for demonstration purposes
exchange = Exchange(account, constants.TESTNET_API_URL)
transfer_result = exchange.usd_tranfer(1, "0x0000000000000000000000000000000000000000")
transfer_result = exchange.usd_transfer(1, "0x0000000000000000000000000000000000000000")
print(transfer_result)


Expand Down
21 changes: 21 additions & 0 deletions examples/basic_withdraw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import eth_account
import utils
from eth_account.signers.local import LocalAccount

from hyperliquid.exchange import Exchange
from hyperliquid.utils import constants


def main():
config = utils.get_config()
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
print("Running with account address:", account.address)

# Withdraw 1 usd
exchange = Exchange(account, constants.TESTNET_API_URL)
withdraw_result = exchange.withdraw_from_bridge(1, account.address)
print(withdraw_result)


if __name__ == "__main__":
main()
22 changes: 21 additions & 1 deletion hyperliquid/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
order_spec_to_order_wire,
sign_l1_action,
sign_usd_transfer_action,
sign_withdraw_from_bridge_action,
sign_agent,
str_to_bytes16,
)
Expand Down Expand Up @@ -335,7 +336,7 @@ def update_isolated_margin(self, amount: float, coin: str) -> Any:
timestamp,
)

def usd_tranfer(self, amount: float, destination: str) -> Any:
def usd_transfer(self, amount: float, destination: str) -> Any:
timestamp = get_timestamp_ms()
payload = {
"destination": destination,
Expand All @@ -354,6 +355,25 @@ def usd_tranfer(self, amount: float, destination: str) -> Any:
timestamp,
)

def withdraw_from_bridge(self, usd: float, destination: str) -> Any:
timestamp = get_timestamp_ms()
payload = {
"destination": destination,
"usd": str(usd),
"time": timestamp,
}
is_mainnet = self.base_url == MAINNET_API_URL
signature = sign_withdraw_from_bridge_action(self.wallet, payload, is_mainnet)
return self._post_action(
{
"chain": "Arbitrum" if is_mainnet else "ArbitrumTestnet",
"payload": payload,
"type": "withdraw2",
},
signature,
timestamp,
)

def approve_agent(self, name: Optional[str] = None) -> Tuple[Any, str]:
agent_key = "0x" + secrets.token_hex(32)
account = eth_account.Account.from_key(agent_key)
Expand Down
27 changes: 27 additions & 0 deletions hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,33 @@ def sign_usd_transfer_action(wallet, message, is_mainnet):
return sign_inner(wallet, data)


def sign_withdraw_from_bridge_action(wallet, message, is_mainnet):
data = {
"domain": {
"name": "Exchange",
"version": "1",
"chainId": 42161 if is_mainnet else 421614,
"verifyingContract": "0x0000000000000000000000000000000000000000",
},
"types": {
"WithdrawFromBridge2SignPayload": [
{"name": "destination", "type": "string"},
{"name": "usd", "type": "string"},
{"name": "time", "type": "uint64"},
],
"EIP712Domain": [
{"name": "name", "type": "string"},
{"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"},
{"name": "verifyingContract", "type": "address"},
],
},
"primaryType": "WithdrawFromBridge2SignPayload",
"message": message,
}
return sign_inner(wallet, data)


def sign_agent(wallet, agent, is_mainnet):
data = {
"domain": {
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "hyperliquid-python-sdk"
version = "0.1.16"
version = "0.1.17"
description = "SDK for Hyperliquid API trading with Python."
readme = "README.md"
authors = ["Hyperliquid <[email protected]>"]
Expand Down
14 changes: 14 additions & 0 deletions tests/signing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
order_spec_preprocessing,
sign_l1_action,
sign_usd_transfer_action,
sign_withdraw_from_bridge_action,
)
from hyperliquid.utils.types import Cloid

Expand Down Expand Up @@ -210,3 +211,16 @@ def test_sign_usd_transfer_action():
assert signature["r"] == "0x283ca602ac69be536bd2272f050eddf8d250ed3eef083d1fc26989e57f891759"
assert signature["s"] == "0x9bc743cf95042269236bc7f48c06ab8a6a9ee53e04f3336c6cfd1b22783aa74"
assert signature["v"] == 28


def test_sign_withdraw_from_bridge_action():
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
message = {
"destination": "0x5e9ee1089755c3435139848e47e6635505d5a13a",
"usd": "1",
"time": 1687816341423,
}
signature = sign_withdraw_from_bridge_action(wallet, message, False)
assert signature["r"] == "0xd60816bf99a00645aa81b9ade23f03bf15994cd2c6d06fc3740a4c74530e36d9"
assert signature["s"] == "0x4552f30419166a6e9d8dbd49b14aeef1e7606fe9e0caec8c0211608d79ce43a3"
assert signature["v"] == 28

0 comments on commit 51a235f

Please sign in to comment.