Skip to content

Commit

Permalink
Version 0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Tao committed Feb 23, 2024
1 parent 84b854f commit ef147ea
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
31 changes: 31 additions & 0 deletions examples/basic_schedule_cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import time

from hyperliquid.utils import constants
from hyperliquid.utils.signing import get_timestamp_ms
import example_utils


def main():
address, info, exchange = example_utils.setup(base_url=constants.TESTNET_API_URL, skip_ws=True)

# Place an order that should rest by setting the price very low
order_result = exchange.order("ETH", True, 0.2, 1100, {"limit": {"tif": "Gtc"}})
print(order_result)

# Query the order status by oid
if order_result["status"] == "ok":
status = order_result["response"]["data"]["statuses"][0]
if "resting" in status:
order_status = info.query_order_by_oid(address, status["resting"]["oid"])
print("Order status by oid:", order_status)

# Schedule cancel
cancel_time = get_timestamp_ms() + 10000 # 10 seconds from now
print(exchange.schedule_cancel(cancel_time))

time.sleep(10)
print("open orders:", info.open_orders(address))


if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions hyperliquid/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
OrderRequest,
OrderType,
OrderWire,
ScheduleCancelAction,
float_to_usd_int,
get_timestamp_ms,
order_request_to_order_wire,
Expand Down Expand Up @@ -278,6 +279,33 @@ def bulk_cancel_by_cloid(self, cancel_requests: List[CancelByCloidRequest]) -> A
timestamp,
)

def schedule_cancel(self, time: Optional[int]) -> Any:
"""Schedules a time (in UTC millis) to cancel all open orders. The time must be at least 5 seconds after the current time.
Once the time comes, all open orders will be canceled and a trigger count will be incremented. The max number of triggers
per day is 10. This trigger count is reset at 00:00 UTC.
Args:
time (int): if time is not None, then set the cancel time in the future. If None, then unsets any cancel time in the future.
"""
timestamp = get_timestamp_ms()
schedule_cancel_action: ScheduleCancelAction = {
"type": "scheduleCancel",
}
if time is not None:
schedule_cancel_action["time"] = time
signature = sign_l1_action(
self.wallet,
schedule_cancel_action,
self.vault_address,
timestamp,
self.base_url == MAINNET_API_URL,
)
return self._post_action(
schedule_cancel_action,
signature,
timestamp,
)

def update_leverage(self, leverage: int, coin: str, is_cross: bool = True) -> Any:
timestamp = get_timestamp_ms()
asset = self.coin_to_asset[coin]
Expand Down
8 changes: 8 additions & 0 deletions hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
},
)

ScheduleCancelAction = TypedDict(
"ScheduleCancelAction",
{
"type": Literal["scheduleCancel"],
"time": NotRequired[Optional[int]],
},
)


def order_type_to_wire(order_type: OrderType) -> OrderTypeWire:
if "limit" in order_type:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "hyperliquid-python-sdk"
version = "0.2.2"
version = "0.2.3"
description = "SDK for Hyperliquid API trading with Python."
readme = "README.md"
authors = ["Hyperliquid <[email protected]>"]
Expand Down
29 changes: 29 additions & 0 deletions tests/signing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from hyperliquid.utils.signing import (
action_hash,
OrderRequest,
ScheduleCancelAction,
construct_phantom_agent,
float_to_int_for_hashing,
order_request_to_order_wire,
Expand Down Expand Up @@ -236,3 +237,31 @@ def test_sub_account_transfer_action():
assert signature_testnet["r"] == "0xe26574013395ad55ee2f4e0575310f003c5bb3351b5425482e2969fa51543927"
assert signature_testnet["s"] == "0xefb08999196366871f919fd0e138b3a7f30ee33e678df7cfaf203e25f0a4278"
assert signature_testnet["v"] == 28


def test_schedule_cancel_action():
wallet = eth_account.Account.from_key("0x0123456789012345678901234567890123456789012345678901234567890123")
action: ScheduleCancelAction = {
"type": "scheduleCancel",
}
signature_mainnet = sign_l1_action(wallet, action, None, 0, True)
assert signature_mainnet["r"] == "0x6cdfb286702f5917e76cd9b3b8bf678fcc49aec194c02a73e6d4f16891195df9"
assert signature_mainnet["s"] == "0x6557ac307fa05d25b8d61f21fb8a938e703b3d9bf575f6717ba21ec61261b2a0"
assert signature_mainnet["v"] == 27
signature_testnet = sign_l1_action(wallet, action, None, 0, False)
assert signature_testnet["r"] == "0xc75bb195c3f6a4e06b7d395acc20bbb224f6d23ccff7c6a26d327304e6efaeed"
assert signature_testnet["s"] == "0x342f8ede109a29f2c0723bd5efb9e9100e3bbb493f8fb5164ee3d385908233df"
assert signature_testnet["v"] == 28

action = {
"type": "scheduleCancel",
"time": 123456789,
}
signature_mainnet = sign_l1_action(wallet, action, None, 0, True)
assert signature_mainnet["r"] == "0x609cb20c737945d070716dcc696ba030e9976fcf5edad87afa7d877493109d55"
assert signature_mainnet["s"] == "0x16c685d63b5c7a04512d73f183b3d7a00da5406ff1f8aad33f8ae2163bab758b"
assert signature_mainnet["v"] == 28
signature_testnet = sign_l1_action(wallet, action, None, 0, False)
assert signature_testnet["r"] == "0x4e4f2dbd4107c69783e251b7e1057d9f2b9d11cee213441ccfa2be63516dc5bc"
assert signature_testnet["s"] == "0x706c656b23428c8ba356d68db207e11139ede1670481a9e01ae2dfcdb0e1a678"
assert signature_testnet["v"] == 27

0 comments on commit ef147ea

Please sign in to comment.