-
Notifications
You must be signed in to change notification settings - Fork 33
/
order-cancel.py
144 lines (114 loc) · 4.14 KB
/
order-cancel.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
#!/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 market id in ENV or uncomment the line below to override market id directly
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
# Grab order reference from original order submission
order_ref = ""
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"]
createVersion = found_order["version"]
###############################################################################
# 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}")
#####################################################################################
# C A N C E L O R D E R S #
#####################################################################################
# Select the mode to cancel orders from the following (comment out others), default = 3
# __cancel_order_req1:
# 1 - Cancel single order for party (pubkey)
cancellation = {
"orderCancellation": {
# Include market and order identifier fields to cancel single order.
"marketId": market_id,
"orderId": orderID,
},
"pubKey": pubkey,
"propagate": True,
}
# :cancel_order_req1__
# __cancel_order_req2:
# 2 - Cancel all orders on market for party (pubkey)
cancellation = {
"orderCancellation": {
# Only include market identifier field.
"marketId": market_id,
},
"pubKey": pubkey,
"propagate": True,
}
# :cancel_order_req2__
# __cancel_order_req3:
# 3 - Cancel all orders on all markets for party (pubkey)
cancellation = {
"orderCancellation": {},
"pubKey": pubkey,
"propagate": True,
}
# :cancel_order_req3__
print()
print("Order cancellation: ", json.dumps(cancellation, indent=2, sort_keys=True))
print()
# __sign_tx_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_cancel__
print("Signed 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 Order(s):")
print(f"ID: {orderID}, Status: {orderStatus}")
if orderStatus == "STATUS_REJECTED":
print("The order cancellation was rejected by Vega")
exit(1) # Halt processing at this stage
print("Order(s) cancelled")