diff --git a/examples/create_cancel_order.py b/examples/create_cancel_order.py index 9f6f898..8456845 100644 --- a/examples/create_cancel_order.py +++ b/examples/create_cancel_order.py @@ -1,19 +1,41 @@ import asyncio import logging +import time from lighter import SignerClient import lighter +import lighter.configuration logging.basicConfig(level=logging.DEBUG) -BASE_URL = "https://mainnet.zklighter.elliot.ai" +BASE_URL = "https://testnet.zklighter.elliot.ai" PRIVATE_KEY = "WALLET_PRIVATE_KEY" +def trim_exception(e: Exception) -> str: + return str(e).strip().split("\n")[-1] + + async def main(): - api_client = lighter.ApiClient() - client = SignerClient(url=BASE_URL, private_key=PRIVATE_KEY) - await client.set_account_index() + api_client = lighter.ApiClient( + configuration=lighter.Configuration(host=BASE_URL) + ) + + # assuring that client is created + account_created = False + new_account = False + while not account_created: + try: + client = SignerClient(url=BASE_URL, private_key=PRIVATE_KEY) + await client.set_account_index() + account_created = True + if new_account: + time.sleep(10) + except Exception as e: + new_account = True + print("Account not created yet", trim_exception(e)) + time.sleep(2) + print("Account Index: ", client.account_index) tx = await client.create_order( @@ -21,7 +43,7 @@ async def main(): client_order_index=0, base_amount=100000, price=250000, - is_ask=0, + is_ask=False, order_type=lighter.SignerClient.ORDER_TYPE_LIMIT, time_in_force=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME, reduce_only=0, @@ -29,7 +51,7 @@ async def main(): ) print("Create Order Tx:", tx) - auth = client.create_auth_token() + auth = client.create_auth_token_with_expiry(SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY) print("Auth:", auth) active_orders = await lighter.OrderApi(api_client).account_active_orders( diff --git a/examples/get_info.py b/examples/get_info.py index b38c653..0572e71 100644 --- a/examples/get_info.py +++ b/examples/get_info.py @@ -6,6 +6,9 @@ logging.basicConfig(level=logging.INFO) +L1_ADDRESS = "WALLET_L1_ADDRESS" +account_index = 0 + async def print_api(method, *args, **kwargs): logging.info(f"{method.__name__}: {await method(*args, **kwargs)}") @@ -13,22 +16,25 @@ async def print_api(method, *args, **kwargs): async def account_apis(client: lighter.ApiClient): logging.info("ACCOUNT APIS") account_instance = lighter.AccountApi(client) - await print_api(account_instance.account, by="index", value="3") + res = await account_instance.account(by="l1_address", value=L1_ADDRESS) + global account_index + if res.accounts: + account_index = res.accounts[0].index + else: + print("NO ACCOUNT FOUND") + exit(0) + await print_api(account_instance.account, by="index", value=str(account_index)) await print_api(account_instance.accounts, index=0, limit=2, sort="asc") await print_api( account_instance.accounts_by_l1_address, - l1_address="0x3fBBA2b0e07895ae8638D17fd83d72338954D272", + l1_address=L1_ADDRESS, ) - # await print_api(account_instance.apikeys, account_index=3, api_key_index=255) + await print_api(account_instance.apikeys, account_index=3, api_key_index=255) await print_api(account_instance.fee_bucket, account_index=3) - await print_api( - account_instance.is_whitelisted, - l1_address="0x3fBBA2b0e07895ae8638D17fd83d72338954D272", - ) await print_api( account_instance.pnl, by="index", - value="1", + value=str(account_index), resolution="1h", start_timestamp=int(datetime.datetime.now().timestamp() - 60 * 60 * 24), end_timestamp=int(datetime.datetime.now().timestamp()), @@ -75,21 +81,6 @@ async def info_apis(client: lighter.ApiClient): async def order_apis(client: lighter.ApiClient): logging.info("ORDER APIS") order_instance = lighter.OrderApi(client) - # TODO: Add auth to sharedlib - # await print_api( - # order_instance.account_active_orders, - # account_index=3, - # market_id=0, - # ) - await print_api( - order_instance.account_inactive_orders, - account_index=3, - market_id=0, - limit=2, - ) - await print_api( - order_instance.account_orders, account_index=3, market_id=0, limit=2 - ) await print_api(order_instance.exchange_stats) await print_api(order_instance.order_book_details, market_id=0) await print_api(order_instance.order_book_orders, market_id=0, limit=2) @@ -101,34 +92,18 @@ async def order_apis(client: lighter.ApiClient): async def transaction_apis(client: lighter.ApiClient): logging.info("TRANSACTION APIS") transaction_instance = lighter.TransactionApi(client) - # await print_api( - # transaction_instance.account_pending_txs, - # by="account_index", - # value="3", - # ) - await print_api( - transaction_instance.account_txs, - by="account_index", - value="0", - index=0, - limit=2, - ) await print_api(transaction_instance.block_txs, by="block_height", value="1") - await print_api( - transaction_instance.deposit_history, - l1_address="0x3fBBA2b0e07895ae8638D17fd83d72338954D272", - ) - await print_api(transaction_instance.next_nonce, account_index=1, api_key_index=0) - # await print_api(transaction_instance.pending_txs, limit=2) - # await print_api(transaction_instance.tx, by="sequence_index", value="0") - # await print_api(transaction_instance.tx_from_l1_tx_hash, hash="0x") + await print_api(transaction_instance.next_nonce, account_index=int(account_index), api_key_index=0) + # use with a valid sequence index + # await print_api(transaction_instance.tx, by="sequence_index", value="5") await print_api(transaction_instance.txs, index=0, limit=2) - await print_api(transaction_instance.withdraw_history, account_index=3) async def main(): client = lighter.ApiClient( - configuration=lighter.Configuration(host="https://mainnet.zklighter.elliot.ai") + configuration=lighter.Configuration( + host="https://testnet.zklighter.elliot.ai" + ) ) await account_apis(client) await block_apis(client) diff --git a/lighter/signer_client.py b/lighter/signer_client.py index 98dbc3a..be65e97 100644 --- a/lighter/signer_client.py +++ b/lighter/signer_client.py @@ -2,6 +2,7 @@ import platform import logging import os +import time from eth_account import Account import lighter @@ -39,6 +40,11 @@ class SignerClient: ORDER_TIME_IN_FORCE_GOOD_TILL_TIME = 1 ORDER_TIME_IN_FORCE_POST_ONLY = 2 + NIL_TRIGGER_PRICE = 0 + DEFAULT_28_DAY_ORDER_EXPIRY = -1 + DEFAULT_10_MIN_AUTH_EXPIRY = -1 + MINUTE = 60 + def __init__( self, url, private_key, chain_id=300, api_key_index=0, account_index=-1 ): @@ -118,7 +124,7 @@ def sign_create_order( time_in_force, reduce_only, trigger_price, - order_expiry=-1, # -1 means constant 28 days expiry + order_expiry=DEFAULT_28_DAY_ORDER_EXPIRY, nonce=-1, ): self.signer.SignCreateOrder.argtypes = [ @@ -140,7 +146,7 @@ def sign_create_order( client_order_index, base_amount, price, - is_ask, + int(is_ask), order_type, time_in_force, reduce_only, @@ -270,7 +276,9 @@ def sign_update_leverage(self, market_index, leverage, nonce=-1): "utf-8" ) - def create_auth_token(self, deadline=0): + def create_auth_token_with_expiry(self, deadline: int=DEFAULT_10_MIN_AUTH_EXPIRY): + if deadline == SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY: + deadline = int(time.time() + 10 * SignerClient.MINUTE) self.signer.CreateAuthToken.argtypes = [ctypes.c_longlong] self.signer.CreateAuthToken.restype = ctypes.c_char_p return self.signer.CreateAuthToken(deadline).decode("utf-8") @@ -284,8 +292,8 @@ async def create_order( is_ask, order_type, time_in_force, - reduce_only, - trigger_price, + reduce_only=False, + trigger_price=NIL_TRIGGER_PRICE, order_expiry=-1, nonce=-1, ) -> (CreateOrder, TxHash): @@ -294,10 +302,10 @@ async def create_order( client_order_index, base_amount, price, - is_ask, + int(is_ask), order_type, time_in_force, - reduce_only, + int(reduce_only), trigger_price, order_expiry, nonce,