-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathsend_batch_tx_ws.py
More file actions
150 lines (121 loc) · 5.38 KB
/
Copy pathsend_batch_tx_ws.py
File metadata and controls
150 lines (121 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import websockets
import asyncio
import time
from utils import default_example_setup, ws_send_batch_tx, trim_exception
# this example does the same thing as the send_batch_tx_http.py example, but sends the TX over WS instead of HTTP
async def main():
client, api_client, ws_client_promise = default_example_setup()
# set up WS client and print a connected message
ws_client: websockets.ClientConnection = await ws_client_promise
print("Received:", await ws_client.recv())
# Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot.
market_index = 2048
api_key_index, nonce = client.nonce_manager.next_nonce()
ask_tx_type, ask_tx_info, ask_tx_hash, error = client.sign_create_order(
market_index=market_index,
client_order_index=1001, # Unique identifier for this order
base_amount=1000, # 0.1 ETH
price=5000_00, # $5000
is_ask=True,
order_type=client.ORDER_TYPE_LIMIT,
time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
reduce_only=False,
trigger_price=0,
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing ask order (first batch): {trim_exception(error)}")
return
# intentionally pass api_key_index to the client.nonce_manager so it increases the nonce, without changing the API key.
# in batch TXs, all TXs must come from the same API key.
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
bid_tx_type, bid_tx_info, bid_tx_hash, error = client.sign_create_order(
market_index=market_index,
client_order_index=1002, # Different unique identifier
base_amount=1000, # 0.1 ETH
price=1500_00, # $1500
is_ask=False,
order_type=client.ORDER_TYPE_LIMIT,
time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
reduce_only=False,
trigger_price=0,
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing second order (first batch): {trim_exception(error)}")
return
tx_types = [ask_tx_type, bid_tx_type]
tx_infos = [ask_tx_info, bid_tx_info]
tx_hashes = [ask_tx_hash, bid_tx_hash]
await ws_send_batch_tx(ws_client, tx_types, tx_infos, tx_hashes)
# In case we want to see the changes in the UI, sleep a bit
time.sleep(5)
# since this is a new batch, we can request a fresh API key
api_key_index, nonce = client.nonce_manager.next_nonce()
cancel_tx_type, cancel_tx_info, cancel_tx_hash, error = client.sign_cancel_order(
market_index=market_index,
order_index=1001, # the index of the order we want cancelled
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing first order (second batch): {trim_exception(error)}")
return
# intentionally pass api_key_index to the client.nonce_manager so it increases the nonce, without changing the API key.
# in batch TXs, all TXs must come from the same API key.
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
new_ask_tx_type, new_ask_tx_info, new_ask_tx_hash, error = client.sign_create_order(
market_index=market_index,
client_order_index=1003, # Different unique identifier
base_amount=2000, # 0.2 ETH
price=5500_00, # $5500
is_ask=True,
order_type=client.ORDER_TYPE_LIMIT,
time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
reduce_only=False,
trigger_price=0,
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing second order (second batch): {trim_exception(error)}")
return
tx_types = [cancel_tx_type, new_ask_tx_type]
tx_infos = [cancel_tx_info, new_ask_tx_info]
tx_hashes = [cancel_tx_hash, new_ask_tx_hash]
await ws_send_batch_tx(ws_client, tx_types, tx_infos, tx_hashes)
# In case we want to see the changes in the UI, sleep a bit
time.sleep(5)
# since this is a new batch, we can request a fresh API key
api_key_index, nonce = client.nonce_manager.next_nonce()
cancel_1_tx_type, cancel_1_tx_info, cancel_1_tx_hash, error = client.sign_cancel_order(
market_index=market_index,
order_index=1002, # the index of the order we want cancelled
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing first order (third batch): {trim_exception(error)}")
return
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
cancel_2_tx_type, cancel_2_tx_info, cancel_2_tx_hash, error = client.sign_cancel_order(
market_index=market_index,
order_index=1003, # the index of the order we want cancelled
nonce=nonce,
api_key_index=api_key_index,
)
if error is not None:
print(f"Error signing second order (third batch): {trim_exception(error)}")
return
tx_types = [cancel_1_tx_type, cancel_2_tx_type]
tx_infos = [cancel_1_tx_info, cancel_2_tx_info]
tx_hashes = [cancel_1_tx_hash, cancel_2_tx_hash]
await ws_send_batch_tx(ws_client, tx_types, tx_infos, tx_hashes)
# Clean up
await client.close()
await api_client.close()
await ws_client.close()
if __name__ == "__main__":
asyncio.run(main())