1+ import asyncio
2+ import logging
3+ import lighter
4+ import json
5+
6+ logging .basicConfig (level = logging .DEBUG )
7+
8+ # The API_KEY_PRIVATE_KEY provided belongs to a dummy account registered on Testnet.
9+ # It was generated using the setup_system.py script, and servers as an example.
10+ BASE_URL = "https://testnet.zklighter.elliot.ai"
11+ API_KEY_PRIVATE_KEY = "0xc0a06468a5bbc9a7b785065a8b227a37fdfa18e2b81d51b018cb03ddd99bfbef4b7551f0f0765639"
12+ ACCOUNT_INDEX = 595
13+ API_KEY_INDEX = 1
14+
15+ def trim_exception (e : Exception ) -> str :
16+ return str (e ).strip ().split ("\n " )[- 1 ]
17+
18+
19+ async def main ():
20+ # Initialize configuration and clients
21+ configuration = lighter .Configuration (BASE_URL )
22+ api_client = lighter .ApiClient (configuration )
23+ transaction_api = lighter .TransactionApi (api_client )
24+
25+ # Initialize signer client
26+ client = lighter .SignerClient (
27+ url = BASE_URL ,
28+ private_key = API_KEY_PRIVATE_KEY ,
29+ account_index = ACCOUNT_INDEX ,
30+ api_key_index = API_KEY_INDEX
31+ )
32+
33+ # Check client connection
34+ err = client .check_client ()
35+ if err is not None :
36+ print (f"CheckClient error: { trim_exception (err )} " )
37+ return
38+
39+ # use next nonce for getting nonces
40+ next_nonce = await transaction_api .next_nonce (account_index = ACCOUNT_INDEX , api_key_index = API_KEY_INDEX )
41+ nonce_value = next_nonce .nonce
42+
43+ ask_tx_info , error = client .sign_create_order (
44+ market_index = 0 ,
45+ client_order_index = 1001 , # Unique identifier for this order
46+ base_amount = 100000 ,
47+ price = 300000 ,
48+ is_ask = True ,
49+ order_type = client .ORDER_TYPE_LIMIT ,
50+ time_in_force = client .ORDER_TIME_IN_FORCE_GOOD_TILL_TIME ,
51+ reduce_only = False ,
52+ trigger_price = 0 ,
53+ nonce = nonce_value
54+ )
55+ nonce_value += 1
56+
57+ if error is not None :
58+ print (f"Error signing first order (first batch): { trim_exception (error )} " )
59+ return
60+
61+ # Sign second order
62+ bid_tx_info , error = client .sign_create_order (
63+ market_index = 0 ,
64+ client_order_index = 1002 , # Different unique identifier
65+ base_amount = 200000 ,
66+ price = 51000 ,
67+ is_ask = False ,
68+ order_type = client .ORDER_TYPE_LIMIT ,
69+ time_in_force = client .ORDER_TIME_IN_FORCE_GOOD_TILL_TIME ,
70+ reduce_only = False ,
71+ trigger_price = 0 ,
72+ nonce = nonce_value
73+ )
74+ nonce_value += 1
75+
76+ if error is not None :
77+ print (f"Error signing second order (first batch): { trim_exception (error )} " )
78+ return
79+
80+ tx_types = json .dumps ([client .TX_TYPE_CREATE_ORDER , client .TX_TYPE_CREATE_ORDER ])
81+ tx_infos = json .dumps ([ask_tx_info , bid_tx_info ])
82+
83+ try :
84+ tx_hashes = await transaction_api .send_tx_batch (tx_types = tx_types , tx_infos = tx_infos )
85+ print (f"Batch transaction successful: { tx_hashes } " )
86+ except Exception as e :
87+ print (f"Error sending batch transaction: { trim_exception (e )} " )
88+
89+ # In case we want to see the changes in the UI, sleep a bit
90+ import time
91+ time .sleep (5 )
92+
93+ cancel_tx_info , error = client .sign_cancel_order (
94+ market_index = 0 ,
95+ order_index = 1001 , # the index of the order we want cancelled
96+ nonce = nonce_value
97+ )
98+ nonce_value += 1
99+
100+ if error is not None :
101+ print (f"Error signing first order (second batch): { trim_exception (error )} " )
102+ return
103+
104+ # Sign second order
105+ new_ask_tx_info , error = client .sign_create_order (
106+ market_index = 0 ,
107+ client_order_index = 1003 , # Different unique identifier
108+ base_amount = 300000 ,
109+ price = 310000 ,
110+ is_ask = True ,
111+ order_type = client .ORDER_TYPE_LIMIT ,
112+ time_in_force = client .ORDER_TIME_IN_FORCE_GOOD_TILL_TIME ,
113+ reduce_only = False ,
114+ trigger_price = 0 ,
115+ nonce = nonce_value
116+ )
117+ nonce_value += 1
118+
119+ if error is not None :
120+ print (f"Error signing second order (second batch): { trim_exception (error )} " )
121+ return
122+
123+ tx_types = json .dumps ([client .TX_TYPE_CANCEL_ORDER , client .TX_TYPE_CREATE_ORDER ])
124+ tx_infos = json .dumps ([cancel_tx_info , new_ask_tx_info ])
125+
126+ try :
127+ tx_hashes = await transaction_api .send_tx_batch (tx_types = tx_types , tx_infos = tx_infos )
128+ print (f"Batch 2 transaction successful: { tx_hashes } " )
129+ except Exception as e :
130+ print (f"Error sending batch transaction 2: { trim_exception (e )} " )
131+
132+ # Clean up
133+ await client .close ()
134+ await api_client .close ()
135+
136+ # Run the async main function
137+ if __name__ == "__main__" :
138+ asyncio .run (main ())
0 commit comments