diff --git a/hyperliquid/utils/signing.py b/hyperliquid/utils/signing.py index 56041471..0a74a43a 100644 --- a/hyperliquid/utils/signing.py +++ b/hyperliquid/utils/signing.py @@ -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: diff --git a/tests/signing_test.py b/tests/signing_test.py index b6e89d9f..50daba61 100644 --- a/tests/signing_test.py +++ b/tests/signing_test.py @@ -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, @@ -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 = {