1- import json
21import websockets
32import asyncio
43
54import lighter
6- from utils import default_example_setup
7-
8-
9- async def ws_send_tx (ws_client : websockets .ClientConnection , tx_type , tx_info ):
10- await ws_client .send (
11- json .dumps (
12- {
13- "type" : "jsonapi/sendtx" ,
14- "data" : {
15- "id" : f"my_random_id_{ 12345678 } " , # optional, helps id the response
16- "tx_type" : tx_type ,
17- "tx_info" : json .loads (tx_info ),
18- },
19- }
20- )
21- )
22-
23- print ("Response:" , await ws_client .recv ())
5+ from utils import default_example_setup , ws_send_tx
246
257
268# this example does the same thing as the create_modify_cancel_order.py example, but sends the TX over WS instead of HTTP
279async def main ():
2810 client , api_client , ws_client_promise = default_example_setup ()
2911
30- # setup WS client and print connected message
12+ # set up WS client and print a connected message
3113 ws_client : websockets .ClientConnection = await ws_client_promise
3214 print ("Received:" , await ws_client .recv ())
3315
3416 # create order
35- tx_type , tx_info , err = client .sign_create_order (
17+ api_key_index , nonce = client .nonce_manager .next_nonce ()
18+ tx_type , tx_info , tx_hash , err = client .sign_create_order (
3619 market_index = 0 ,
3720 client_order_index = 123 ,
3821 base_amount = 1000 , # 0.1 ETH
@@ -42,31 +25,41 @@ async def main():
4225 time_in_force = lighter .SignerClient .ORDER_TIME_IN_FORCE_GOOD_TILL_TIME ,
4326 reduce_only = False ,
4427 trigger_price = 0 ,
28+ nonce = nonce ,
29+ api_key_index = api_key_index ,
4530 )
4631 if err is not None :
4732 raise Exception (err )
48- await ws_send_tx (ws_client , tx_type , tx_info )
33+ await ws_send_tx (ws_client , tx_type , tx_info , tx_hash )
4934
50- # modify order
51- tx_type , tx_info , err = client .sign_modify_order (
35+ ## modify order
36+ # use the same API key so the TX goes after the create order TX
37+ api_key_index , nonce = client .nonce_manager .next_nonce (api_key_index )
38+ tx_type , tx_info , tx_hash , err = client .sign_modify_order (
5239 market_index = 0 ,
5340 order_index = 123 ,
5441 base_amount = 1100 , # 0.11 ETH
5542 price = 410000 , # $4100
5643 trigger_price = 0 ,
44+ nonce = nonce ,
45+ api_key_index = api_key_index ,
5746 )
5847 if err is not None :
5948 raise Exception (err )
60- await ws_send_tx (ws_client , tx_type , tx_info )
49+ await ws_send_tx (ws_client , tx_type , tx_info , tx_hash )
6150
62- # cancel order
63- tx_type , tx_info , err = client .sign_cancel_order (
51+ ## cancel order
52+ # use the same API key so the TX goes after the modify order TX
53+ api_key_index , nonce = client .nonce_manager .next_nonce (api_key_index )
54+ tx_type , tx_info , tx_hash , err = client .sign_cancel_order (
6455 market_index = 0 ,
6556 order_index = 123 ,
57+ nonce = nonce ,
58+ api_key_index = api_key_index ,
6659 )
6760 if err is not None :
6861 raise Exception (err )
69- await ws_send_tx (ws_client , tx_type , tx_info )
62+ await ws_send_tx (ws_client , tx_type , tx_info , tx_hash )
7063
7164 await client .close ()
7265 await api_client .close ()
0 commit comments