Skip to content

Commit 51a235f

Browse files
committed
Version 0.1.17
1 parent 374c86e commit 51a235f

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

examples/basic_transfer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def main():
1313

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

1919

examples/basic_withdraw.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import eth_account
2+
import utils
3+
from eth_account.signers.local import LocalAccount
4+
5+
from hyperliquid.exchange import Exchange
6+
from hyperliquid.utils import constants
7+
8+
9+
def main():
10+
config = utils.get_config()
11+
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
12+
print("Running with account address:", account.address)
13+
14+
# Withdraw 1 usd
15+
exchange = Exchange(account, constants.TESTNET_API_URL)
16+
withdraw_result = exchange.withdraw_from_bridge(1, account.address)
17+
print(withdraw_result)
18+
19+
20+
if __name__ == "__main__":
21+
main()

hyperliquid/exchange.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
order_spec_to_order_wire,
2525
sign_l1_action,
2626
sign_usd_transfer_action,
27+
sign_withdraw_from_bridge_action,
2728
sign_agent,
2829
str_to_bytes16,
2930
)
@@ -335,7 +336,7 @@ def update_isolated_margin(self, amount: float, coin: str) -> Any:
335336
timestamp,
336337
)
337338

338-
def usd_tranfer(self, amount: float, destination: str) -> Any:
339+
def usd_transfer(self, amount: float, destination: str) -> Any:
339340
timestamp = get_timestamp_ms()
340341
payload = {
341342
"destination": destination,
@@ -354,6 +355,25 @@ def usd_tranfer(self, amount: float, destination: str) -> Any:
354355
timestamp,
355356
)
356357

358+
def withdraw_from_bridge(self, usd: float, destination: str) -> Any:
359+
timestamp = get_timestamp_ms()
360+
payload = {
361+
"destination": destination,
362+
"usd": str(usd),
363+
"time": timestamp,
364+
}
365+
is_mainnet = self.base_url == MAINNET_API_URL
366+
signature = sign_withdraw_from_bridge_action(self.wallet, payload, is_mainnet)
367+
return self._post_action(
368+
{
369+
"chain": "Arbitrum" if is_mainnet else "ArbitrumTestnet",
370+
"payload": payload,
371+
"type": "withdraw2",
372+
},
373+
signature,
374+
timestamp,
375+
)
376+
357377
def approve_agent(self, name: Optional[str] = None) -> Tuple[Any, str]:
358378
agent_key = "0x" + secrets.token_hex(32)
359379
account = eth_account.Account.from_key(agent_key)

hyperliquid/utils/signing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ def sign_usd_transfer_action(wallet, message, is_mainnet):
203203
return sign_inner(wallet, data)
204204

205205

206+
def sign_withdraw_from_bridge_action(wallet, message, is_mainnet):
207+
data = {
208+
"domain": {
209+
"name": "Exchange",
210+
"version": "1",
211+
"chainId": 42161 if is_mainnet else 421614,
212+
"verifyingContract": "0x0000000000000000000000000000000000000000",
213+
},
214+
"types": {
215+
"WithdrawFromBridge2SignPayload": [
216+
{"name": "destination", "type": "string"},
217+
{"name": "usd", "type": "string"},
218+
{"name": "time", "type": "uint64"},
219+
],
220+
"EIP712Domain": [
221+
{"name": "name", "type": "string"},
222+
{"name": "version", "type": "string"},
223+
{"name": "chainId", "type": "uint256"},
224+
{"name": "verifyingContract", "type": "address"},
225+
],
226+
},
227+
"primaryType": "WithdrawFromBridge2SignPayload",
228+
"message": message,
229+
}
230+
return sign_inner(wallet, data)
231+
232+
206233
def sign_agent(wallet, agent, is_mainnet):
207234
data = {
208235
"domain": {

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
55

66
[tool.poetry]
77
name = "hyperliquid-python-sdk"
8-
version = "0.1.16"
8+
version = "0.1.17"
99
description = "SDK for Hyperliquid API trading with Python."
1010
readme = "README.md"
1111
authors = ["Hyperliquid <[email protected]>"]

tests/signing_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
order_spec_preprocessing,
1414
sign_l1_action,
1515
sign_usd_transfer_action,
16+
sign_withdraw_from_bridge_action,
1617
)
1718
from hyperliquid.utils.types import Cloid
1819

@@ -210,3 +211,16 @@ def test_sign_usd_transfer_action():
210211
assert signature["r"] == "0x283ca602ac69be536bd2272f050eddf8d250ed3eef083d1fc26989e57f891759"
211212
assert signature["s"] == "0x9bc743cf95042269236bc7f48c06ab8a6a9ee53e04f3336c6cfd1b22783aa74"
212213
assert signature["v"] == 28
214+
215+
216+
def test_sign_withdraw_from_bridge_action():
217+
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
218+
message = {
219+
"destination": "0x5e9ee1089755c3435139848e47e6635505d5a13a",
220+
"usd": "1",
221+
"time": 1687816341423,
222+
}
223+
signature = sign_withdraw_from_bridge_action(wallet, message, False)
224+
assert signature["r"] == "0xd60816bf99a00645aa81b9ade23f03bf15994cd2c6d06fc3740a4c74530e36d9"
225+
assert signature["s"] == "0x4552f30419166a6e9d8dbd49b14aeef1e7606fe9e0caec8c0211608d79ce43a3"
226+
assert signature["v"] == 28

0 commit comments

Comments
 (0)