Skip to content

Commit 89ace79

Browse files
committed
Version 0.1.9
1 parent 4263fdc commit 89ace79

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

assets/images/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

hyperliquid/utils/signing.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
Tif = Union[Literal["Alo"], Literal["Ioc"], Literal["Gtc"]]
1212
Tpsl = Union[Literal["tp"], Literal["sl"]]
1313
LimitOrderType = TypedDict("LimitOrderType", {"tif": Tif})
14-
TriggerOrderType = TypedDict("TriggerOrderType", {"triggerPx": int, "isMarket": bool, "tpsl": Tpsl})
14+
TriggerOrderType = TypedDict("TriggerOrderType", {"triggerPx": float, "isMarket": bool, "tpsl": Tpsl})
15+
TriggerOrderTypeWire = TypedDict("TriggerOrderTypeWire", {"triggerPx": str, "isMarket": bool, "tpsl": Tpsl})
1516
OrderType = TypedDict("OrderType", {"limit": LimitOrderType, "trigger": TriggerOrderType}, total=False)
17+
OrderTypeWire = TypedDict("OrderTypeWire", {"limit": LimitOrderType, "trigger": TriggerOrderTypeWire}, total=False)
1618

1719

18-
def order_type_to_tuple(order_type: OrderType) -> Tuple[int, int]:
20+
def order_type_to_tuple(order_type: OrderType) -> Tuple[int, float]:
1921
if "limit" in order_type:
2022
tif = order_type["limit"]["tif"]
2123
if tif == "Gtc":
@@ -69,10 +71,25 @@ def order_spec_preprocessing(order_spec: OrderSpec) -> Any:
6971

7072

7173
OrderWire = TypedDict(
72-
"OrderWire", {"asset": int, "isBuy": bool, "limitPx": str, "sz": str, "reduceOnly": bool, "orderType": OrderType}
74+
"OrderWire",
75+
{"asset": int, "isBuy": bool, "limitPx": str, "sz": str, "reduceOnly": bool, "orderType": OrderTypeWire},
7376
)
7477

7578

79+
def order_type_to_wire(order_type: OrderType) -> OrderTypeWire:
80+
if "limit" in order_type:
81+
return {"limit": order_type["limit"]}
82+
elif "trigger" in order_type:
83+
return {
84+
"trigger": {
85+
"triggerPx": float_to_wire(order_type["trigger"]["triggerPx"]),
86+
"tpsl": order_type["trigger"]["tpsl"],
87+
"isMarket": order_type["trigger"]["isMarket"],
88+
}
89+
}
90+
raise ValueError("Invalid order type", order_type)
91+
92+
7693
def order_spec_to_order_wire(order_spec: OrderSpec) -> OrderWire:
7794
order = order_spec["order"]
7895
return {
@@ -81,7 +98,7 @@ def order_spec_to_order_wire(order_spec: OrderSpec) -> OrderWire:
8198
"limitPx": float_to_wire(order["limitPx"]),
8299
"sz": float_to_wire(order["sz"]),
83100
"reduceOnly": order["reduceOnly"],
84-
"orderType": order_spec["orderType"],
101+
"orderType": order_type_to_wire(order_spec["orderType"]),
85102
}
86103

87104

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

tests/signing_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ def test_l1_action_signing_matches():
4444
assert signature["v"] == 27
4545

4646

47+
def test_l1_action_signing_order_matches():
48+
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
49+
order_spec: OrderSpec = {
50+
"order": {
51+
"asset": 1,
52+
"isBuy": True,
53+
"reduceOnly": False,
54+
"limitPx": 100,
55+
"sz": 100,
56+
},
57+
"orderType": {"limit": {"tif": "Gtc"}},
58+
}
59+
timestamp = 0
60+
grouping: Literal["na"] = "na"
61+
62+
signature = sign_l1_action(
63+
wallet,
64+
["(uint32,bool,uint64,uint64,bool,uint8,uint64)[]", "uint8"],
65+
[[order_spec_preprocessing(order_spec)], order_grouping_to_number(grouping)],
66+
ZERO_ADDRESS,
67+
timestamp,
68+
)
69+
assert signature["r"] == "0xc7249fd2e6483b2ab8fb0a80ac5201c4d36c8ed52bea5896012de499cdb78f4f"
70+
assert signature["s"] == "0x235a525be1a3b3c5186a241763a47f4e1db463dbba64a3c4abad3696852cd6ff"
71+
assert signature["v"] == 27
72+
73+
4774
def test_l1_action_signing_matches_with_vault():
4875
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
4976
signature = sign_l1_action(
@@ -54,6 +81,33 @@ def test_l1_action_signing_matches_with_vault():
5481
assert signature["v"] == 27
5582

5683

84+
def test_l1_action_signing_tpsl_order_matches():
85+
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
86+
order_spec: OrderSpec = {
87+
"order": {
88+
"asset": 1,
89+
"isBuy": True,
90+
"reduceOnly": False,
91+
"limitPx": 100,
92+
"sz": 100,
93+
},
94+
"orderType": {"trigger": {"triggerPx": 103, "isMarket": True, "tpsl": "sl"}},
95+
}
96+
timestamp = 0
97+
grouping: Literal["na"] = "na"
98+
99+
signature = sign_l1_action(
100+
wallet,
101+
["(uint32,bool,uint64,uint64,bool,uint8,uint64)[]", "uint8"],
102+
[[order_spec_preprocessing(order_spec)], order_grouping_to_number(grouping)],
103+
ZERO_ADDRESS,
104+
timestamp,
105+
)
106+
assert signature["r"] == "0xac0669959d0031e822c0aac9569f256ed66adfc409ab0bc3349f3006b794daf"
107+
assert signature["s"] == "0x25e31fba6a324b13f1b1960142b9af31bfc4cc73796ac988aef434d693f865ea"
108+
assert signature["v"] == 28
109+
110+
57111
def test_float_to_int_for_hashing():
58112
assert float_to_int_for_hashing(123123123123) == 12312312312300000000
59113
assert float_to_int_for_hashing(0.00001231) == 1231

0 commit comments

Comments
 (0)