Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,11 @@ def float_to_wire(x: float) -> str:
rounded = f"{x:.8f}"
if abs(float(rounded) - x) >= 1e-12:
raise ValueError("float_to_wire causes rounding", x)
if rounded == "-0":
rounded = "0"
normalized = Decimal(rounded).normalize()
return f"{normalized:f}"
wire = f"{normalized:f}"
if wire == "-0":
wire = "0"
return wire


def float_to_int_for_hashing(x: float) -> int:
Expand Down
10 changes: 10 additions & 0 deletions tests/signing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
action_hash,
construct_phantom_agent,
float_to_int_for_hashing,
float_to_wire,
order_request_to_order_wire,
order_wires_to_order_action,
sign_l1_action,
Expand Down Expand Up @@ -186,6 +187,15 @@ def test_float_to_int_for_hashing():
float_to_int_for_hashing(0.000012312312)


def test_float_to_wire_normalizes_negative_zero():
assert float_to_wire(-0.0) == "0"
assert float_to_wire(0.0) == "0"
assert float_to_wire(-1e-13) == "0"
assert float_to_wire(-1.5) == "-1.5"
assert float_to_wire(1.5) == "1.5"
assert float_to_wire(0.00001231) == "0.00001231"


def test_sign_usd_transfer_action():
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
message = {
Expand Down