Skip to content

Commit ea84213

Browse files
authored
Version 0.20.0 (#219)
Add Dex Abstraction and update multi sig usd send example
1 parent 64b252e commit ea84213

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed

examples/dex_abstraction.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# See https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#enable-hip-3-dex-abstraction for more details
2+
import example_utils
3+
4+
from hyperliquid.exchange import Exchange
5+
from hyperliquid.utils import constants
6+
7+
SUB_ACCOUNT_NAME = "One"
8+
9+
10+
def main():
11+
address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True)
12+
13+
# enable dex abstraction for user via agent
14+
agent_enable_dex_abstraction_result = exchange.agent_enable_dex_abstraction()
15+
print(agent_enable_dex_abstraction_result)
16+
17+
user = exchange.account_address
18+
if user == exchange.wallet.address:
19+
# disable dex abstraction for user
20+
user_dex_abstraction_result = exchange.user_dex_abstraction(user, False)
21+
print(user_dex_abstraction_result)
22+
print("current user dex abstraction state:", info.query_user_dex_abstraction_state(user))
23+
24+
# enable and disable dex abstraction for sub-account of user
25+
sub_accounts = info.query_sub_accounts(user)
26+
sub_account_user = None
27+
for sub_account in sub_accounts:
28+
if sub_account["name"] == SUB_ACCOUNT_NAME:
29+
sub_account_user = sub_account["subAccountUser"]
30+
print("found sub-account, enabling and disabling dex abstraction for", sub_account_user)
31+
32+
# enable dex abstraction for user via agent by setting the vault_address to the sub_account_user
33+
exchange_with_sub_account = Exchange(exchange.wallet, exchange.base_url, vault_address=sub_account_user)
34+
agent_enable_dex_abstraction_result = exchange_with_sub_account.agent_enable_dex_abstraction()
35+
print("sub-account agent_enable_dex_abstraction result:", agent_enable_dex_abstraction_result)
36+
37+
for enabled in [True, False]:
38+
user_dex_abstraction_result = exchange.user_dex_abstraction(sub_account_user, enabled)
39+
print(user_dex_abstraction_result)
40+
print(
41+
"current sub-account user dex abstraction state:",
42+
info.query_user_dex_abstraction_state(sub_account_user),
43+
)
44+
45+
break
46+
47+
else:
48+
print("not performing user dex abstraction because not user", exchange.account_address, exchange.wallet.address)
49+
50+
51+
if __name__ == "__main__":
52+
main()

examples/multi_sig_usd_send.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import example_utils
22

33
from hyperliquid.utils import constants
4-
from hyperliquid.utils.signing import USD_SEND_SIGN_TYPES, get_timestamp_ms, sign_multi_sig_user_signed_action_payload
4+
from hyperliquid.utils.signing import SEND_ASSET_SIGN_TYPES, get_timestamp_ms, sign_multi_sig_user_signed_action_payload
55

66

77
def main():
@@ -19,12 +19,16 @@ def main():
1919

2020
# Define the multi-sig inner action - in this case, sending USD
2121
action = {
22-
"type": "usdSend",
22+
"type": "sendAsset",
2323
"signatureChainId": "0x66eee",
2424
"hyperliquidChain": "Testnet",
2525
"destination": "0x0000000000000000000000000000000000000000",
26+
"sourceDex": "",
27+
"destinationDex": "",
28+
"token": "USDC",
2629
"amount": "100.0",
27-
"time": timestamp,
30+
"fromSubAccount": "",
31+
"nonce": timestamp,
2832
}
2933
signatures = []
3034

@@ -35,8 +39,8 @@ def main():
3539
wallet,
3640
action,
3741
exchange.base_url == constants.MAINNET_API_URL,
38-
USD_SEND_SIGN_TYPES,
39-
"HyperliquidTransaction:UsdSend",
42+
SEND_ASSET_SIGN_TYPES,
43+
"HyperliquidTransaction:SendAsset",
4044
multi_sig_user,
4145
address,
4246
)

hyperliquid/exchange.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
sign_token_delegate_action,
3232
sign_usd_class_transfer_action,
3333
sign_usd_transfer_action,
34+
sign_user_dex_abstraction_action,
3435
sign_withdraw_from_bridge_action,
3536
)
3637
from hyperliquid.utils.types import (
@@ -1110,6 +1111,40 @@ def use_big_blocks(self, enable: bool) -> Any:
11101111
timestamp,
11111112
)
11121113

1114+
def agent_enable_dex_abstraction(self) -> Any:
1115+
timestamp = get_timestamp_ms()
1116+
action = {
1117+
"type": "agentEnableDexAbstraction",
1118+
}
1119+
signature = sign_l1_action(
1120+
self.wallet,
1121+
action,
1122+
self.vault_address,
1123+
timestamp,
1124+
self.expires_after,
1125+
self.base_url == MAINNET_API_URL,
1126+
)
1127+
return self._post_action(
1128+
action,
1129+
signature,
1130+
timestamp,
1131+
)
1132+
1133+
def user_dex_abstraction(self, user: str, enabled: bool) -> Any:
1134+
timestamp = get_timestamp_ms()
1135+
action = {
1136+
"type": "userDexAbstraction",
1137+
"user": user.lower(),
1138+
"enabled": enabled,
1139+
"nonce": timestamp,
1140+
}
1141+
signature = sign_user_dex_abstraction_action(self.wallet, action, self.base_url == MAINNET_API_URL)
1142+
return self._post_action(
1143+
action,
1144+
signature,
1145+
timestamp,
1146+
)
1147+
11131148
def noop(self, nonce):
11141149
action = {"type": "noop"}
11151150
signature = sign_l1_action(

hyperliquid/info.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ def query_user_to_multi_sig_signers(self, multi_sig_user: str) -> Any:
626626
def query_perp_deploy_auction_status(self) -> Any:
627627
return self.post("/info", {"type": "perpDeployAuctionStatus"})
628628

629+
def query_user_dex_abstraction_state(self, user: str) -> Any:
630+
return self.post("/info", {"type": "userDexAbstraction", "user": user})
631+
629632
def historical_orders(self, user: str) -> Any:
630633
"""Retrieve a user's historical orders.
631634

hyperliquid/utils/signing.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@
115115
{"name": "nonce", "type": "uint64"},
116116
]
117117

118+
USER_DEX_ABSTRACTION_SIGN_TYPES = [
119+
{"name": "hyperliquidChain", "type": "string"},
120+
{"name": "user", "type": "address"},
121+
{"name": "enabled", "type": "bool"},
122+
{"name": "nonce", "type": "uint64"},
123+
]
124+
118125
TOKEN_DELEGATE_TYPES = [
119126
{"name": "hyperliquidChain", "type": "string"},
120127
{"name": "validator", "type": "address"},
@@ -362,6 +369,16 @@ def sign_send_asset_action(wallet, action, is_mainnet):
362369
)
363370

364371

372+
def sign_user_dex_abstraction_action(wallet, action, is_mainnet):
373+
return sign_user_signed_action(
374+
wallet,
375+
action,
376+
USER_DEX_ABSTRACTION_SIGN_TYPES,
377+
"HyperliquidTransaction:UserDexAbstraction",
378+
is_mainnet,
379+
)
380+
381+
365382
def sign_convert_to_multi_sig_user_action(wallet, action, is_mainnet):
366383
return sign_user_signed_action(
367384
wallet,

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.19.0"
8+
version = "0.20.0"
99
description = "SDK for Hyperliquid API trading with Python."
1010
readme = "README.md"
1111
authors = ["Hyperliquid <[email protected]>"]

0 commit comments

Comments
 (0)