-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsubmit-amend-cancel-pegged-order.py
291 lines (234 loc) · 8.63 KB
/
submit-amend-cancel-pegged-order.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python3
import requests
import time
import helpers
import json
# Vega wallet interaction helper, see login.py for detail
from login import token, pubkey
# Load Vega node API v2 URL, this is set using 'source vega-config'
# located in the root folder of the sample-api-scripts repository
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
# Load Vega wallet server URL, set in same way as above
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
# Load Vega market id
market_id = helpers.env_market_id()
assert market_id != ""
# Set to False to ONLY submit/amend an order (no cancellation)
# e.g. orders will remain on the book
CANCEL_ORDER_AFTER_SUBMISSION = True
# Set market id in ENV or uncomment the line below to override market id directly
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
#####################################################################################
# B L O C K C H A I N T I M E #
#####################################################################################
# __get_expiry_time:
# Request the current blockchain time, calculate an expiry time
response = requests.get(f"{data_node_url_rest}/vega/time")
helpers.check_response(response)
blockchain_time = int(response.json()["timestamp"])
expiresAt = str(int(blockchain_time + 120 * 1e9)) # expire in 2 minutes
# :get_expiry_time__
assert blockchain_time > 0
print(f"Blockchain time: {blockchain_time}")
#####################################################################################
# S U B M I T P E G G E D O R D E R #
#####################################################################################
# __submit_pegged_order:
# Compose your submit pegged order command
# Set your own user specific reference to find the order in next step and
# as a foreign key to your local client/trading application
order_ref = f"{pubkey}-{helpers.generate_id(30)}"
submission = {
"orderSubmission": {
"marketId": market_id,
"size": "50",
"side": "SIDE_BUY",
"timeInForce": "TIME_IN_FORCE_GTT",
"expiresAt": expiresAt,
"type": "TYPE_LIMIT",
"reference": order_ref,
"peggedOrder": {
"offset": "5",
"reference": "PEGGED_REFERENCE_MID"
}
},
"pubKey": pubkey,
"propagate": True
}
# :submit_pegged_order__
print()
print("Pegged order submission: ", json.dumps(submission, indent=2, sort_keys=True))
print()
# __sign_tx_pegged_order:
# Sign the transaction with a pegged order submission command
url = "http://localhost:1789/api/v2/requests"
payload1 = {
"id": "1",
"jsonrpc": "2.0",
"method": "client.send_transaction",
"params": {
"publicKey": pubkey,
"sendingMode": "TYPE_SYNC",
"transaction": submission
}
}
payload = json.dumps(payload1)
headers = {
'Content-Type': 'application/json-rpc',
'Accept': 'application/json-rpc',
'Origin': 'application/json-rpc',
'Authorization': f'{token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# :sign_tx_pegged_order__
print(json.dumps(response.json(), indent=4, sort_keys=True))
print()
print("Signed pegged order and sent to Vega")
# Wait for order submission to be included in a block
print("Waiting for blockchain...", end="", flush=True)
url = f"{data_node_url_rest}/orders?partyId={pubkey}&reference={order_ref}"
response = requests.get(url)
while helpers.check_nested_response(response, "orders") is not True:
time.sleep(0.5)
print(".", end="", flush=True)
response = requests.get(url)
found_order = helpers.get_nested_response(response, "orders")[0]["node"]
orderID = found_order["id"]
orderStatus = found_order["status"]
createVersion = found_order["version"]
orderPegged = found_order["peggedOrder"]
print()
print(f"\nPegged order processed, ID: {orderID}, Status: {orderStatus}, Version: {createVersion}")
if orderStatus == "STATUS_REJECTED":
print("The pegged order was rejected by Vega")
exit(1) # Halt processing at this stage
else:
print(f"Pegged at: {orderPegged}")
#####################################################################################
# A M E N D P E G G E D O R D E R #
#####################################################################################
# __amend_pegged_order:
# Compose your amend order command, with changes to existing order
amendment = {
"orderAmendment": {
"orderId": orderID,
"marketId": market_id,
"sizeDelta": "25",
"timeInForce": "TIME_IN_FORCE_GTC",
"peggedReference": "PEGGED_REFERENCE_BEST_BID",
"peggedOffset": "-100",
},
"pubKey": pubkey,
"propagate": True
}
# :amend_pegged_order__
print()
print("Pegged order amendment: ", json.dumps(amendment, indent=2, sort_keys=True))
print()
# __sign_tx_pegged_amend:
# Sign the transaction with a pegged order amendment command
url = "http://localhost:1789/api/v2/requests"
payload1 = {
"id": "1",
"jsonrpc": "2.0",
"method": "client.send_transaction",
"params": {
"publicKey": pubkey,
"sendingMode": "TYPE_SYNC",
"transaction": amendment
}
}
payload = json.dumps(payload1)
headers = {
'Content-Type': 'application/json-rpc',
'Accept': 'application/json-rpc',
'Origin': 'application/json-rpc',
'Authorization': f'{token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# :sign_tx_pegged_amend__
print("Signed pegged order amendment and sent to Vega")
# Wait for order amendment to be included in a block
print("Waiting for blockchain...")
time.sleep(3)
url = f"{data_node_url_rest}/orders?partyId={pubkey}&reference={order_ref}"
response = requests.get(url)
found_order = helpers.get_nested_response(response, "orders")[0]["node"]
orderID = found_order["id"]
orderStatus = found_order["status"]
orderVersion = found_order["version"]
orderPegged = found_order["peggedOrder"]
orderPrice = found_order["price"]
orderSize = found_order["size"]
orderTif = found_order["timeInForce"]
print()
print("Amended pegged order:")
print(f"ID: {orderID}, Status: {orderStatus}, "
f"Size(Old): 50, Size(New): {orderSize}, "
f"TimeInForce(Old): TIME_IN_FORCE_GTT, TimeInForce(New): {orderTif}, "
f"Version(Old): {createVersion}, Version(new): {orderVersion}")
if orderStatus == "STATUS_REJECTED":
print("The pegged order amendment was rejected by Vega")
exit(1) # Halt processing at this stage
else:
print(f"Pegged at: {orderPegged}")
#####################################################################################
# C A N C E L P E G G E D O R D E R #
#####################################################################################
# Hint: For a full example with combinations please see submit-amend-cancel-order.py
# __cancel_pegged_order:
# # Cancellation command for the specific order
cancellation = {
"orderCancellation": {
# Include market and order identifier fields to cancel single order.
"marketId": market_id,
"orderId": orderID,
},
"pubKey": pubkey,
"propagate": True,
}
# :cancel_pegged_order__
print()
print("Pegged order cancellation: ", json.dumps(cancellation, indent=2, sort_keys=True))
print()
# __sign_tx_pegged_cancel:
# Sign the transaction for cancellation
url = "http://localhost:1789/api/v2/requests"
payload1 = {
"id": "1",
"jsonrpc": "2.0",
"method": "client.send_transaction",
"params": {
"publicKey": pubkey,
"sendingMode": "TYPE_SYNC",
"transaction": cancellation
}
}
payload = json.dumps(payload1)
headers = {
'Content-Type': 'application/json-rpc',
'Accept': 'application/json-rpc',
'Origin': 'application/json-rpc',
'Authorization': f'{token}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# :sign_tx_pegged_cancel__
print("Signed pegged cancellation and sent to Vega")
print()
# Wait for cancellation to be included in a block
print("Waiting for blockchain...")
time.sleep(3)
url = f"{data_node_url_rest}/orders?partyId={pubkey}&reference={order_ref}"
response = requests.get(url)
found_order = helpers.get_nested_response(response, "orders")[0]["node"]
orderID = found_order["id"]
orderStatus = found_order["status"]
print("Cancelled pegged order:")
print(f"ID: {orderID}, Status: {orderStatus}")
if orderStatus == "STATUS_REJECTED":
print("The pegged order cancellation was rejected by Vega")
exit(1) # Halt processing at this stage
print("Pegged order cancelled")