Skip to content

Commit 9670f69

Browse files
authored
Merge pull request #173 from vegaprotocol/separating-amends
Submit amend and cancel separated into different scripts
2 parents 074a385 + 63cbf79 commit 9670f69

10 files changed

Lines changed: 951 additions & 0 deletions

rest/liquidity-commitment-amend.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
import requests
5+
import helpers
6+
7+
# Vega wallet interaction helper, see login.py for detail
8+
from login import token, pubkey
9+
10+
# Load Vega node API v2 URL, this is set using 'source vega-config'
11+
# located in the root folder of the sample-api-scripts repository
12+
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
13+
# Load Vega wallet server URL, set in same way as above
14+
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
15+
16+
# Load Vega market id
17+
market_id = helpers.env_market_id()
18+
assert market_id != ""
19+
20+
# Set to False to ONLY submit/amend a liquidity commitment (no cancellation)
21+
CANCEL_LP_AFTER_SUBMISSION = True
22+
23+
# Set market id in ENV or uncomment the line below to override market id directly
24+
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
25+
26+
#####################################################################################
27+
# A M E N D L I Q U I D I T Y C O M M I T M E N T #
28+
#####################################################################################
29+
30+
# __amend_liquidity_commitment:
31+
# Compose a liquidity commitment order message
32+
# (it will now serve as an amendment request):
33+
# modify fields you want to be amended
34+
submission = {
35+
"liquidityProvisionAmendment": {
36+
"marketId": market_id,
37+
"commitmentAmount": "500000000000000000000",
38+
"fee": "0.005",
39+
"buys": [
40+
{
41+
"offset": "1",
42+
"proportion": "1",
43+
"reference": "PEGGED_REFERENCE_MID"
44+
}
45+
],
46+
"sells": [
47+
{
48+
"offset": "1",
49+
"proportion": "1",
50+
"reference": "PEGGED_REFERENCE_MID"
51+
}
52+
]
53+
},
54+
"pubKey": pubkey,
55+
"propagate": True
56+
}
57+
# :amend_liquidity_commitment__
58+
59+
print("Liquidity commitment amendment:\n{}".format(
60+
json.dumps(submission, indent=2, sort_keys=True)
61+
))
62+
63+
# __sign_tx_liquidity_amend:
64+
# Sign the transaction with an order submission command
65+
# Hint: Setting propagate to true will also submit to a Vega node
66+
url = f"{wallet_server_url}/api/v1/command/sync"
67+
headers = {"Authorization": f"Bearer {token}"}
68+
response = requests.post(url, headers=headers, json=submission)
69+
helpers.check_response(response)
70+
# :sign_tx_liquidity_amend__
71+
72+
print("Signed liquidity commitment amendment and sent to Vega")
73+
74+
if CANCEL_LP_AFTER_SUBMISSION is not True:
75+
exit(1)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
import time
5+
import requests
6+
import helpers
7+
8+
# Vega wallet interaction helper, see login.py for detail
9+
from login import token, pubkey
10+
11+
# Load Vega node API v2 URL, this is set using 'source vega-config'
12+
# located in the root folder of the sample-api-scripts repository
13+
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
14+
# Load Vega wallet server URL, set in same way as above
15+
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
16+
17+
# Load Vega market id
18+
market_id = helpers.env_market_id()
19+
assert market_id != ""
20+
21+
# Set market id in ENV or uncomment the line below to override market id directly
22+
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
23+
24+
#####################################################################################
25+
# C A N C E L L I Q U I D I T Y C O M M I T M E N T #
26+
#####################################################################################
27+
28+
# __cancel_liquidity_commitment:
29+
# Compose a liquidity commitment cancellation command
30+
# Hint: The transaction may get rejected if removing previously supplied liquidity
31+
# will result in insufficient liquidity for the market to operate
32+
submission = {
33+
"liquidityProvisionCancellation": {
34+
"marketId": market_id,
35+
},
36+
"pubKey": pubkey,
37+
"propagate": True
38+
}
39+
# :cancel_liquidity_commitment__
40+
41+
print("Liquidity commitment cancellation:\n{}".format(
42+
json.dumps(submission, indent=2, sort_keys=True)
43+
))
44+
45+
# __sign_tx_liquidity_cancel:
46+
# Sign the transaction with an order submission command
47+
# Hint: Setting propagate to true will also submit to a Vega node
48+
url = f"{wallet_server_url}/api/v1/command/sync"
49+
headers = {"Authorization": f"Bearer {token}"}
50+
response = requests.post(url, headers=headers, json=submission)
51+
helpers.check_response(response)
52+
# :sign_tx_liquidity_cancel__
53+
54+
print("Signed liquidity commitment cancellation and sent to Vega")
55+
56+
# Wait for cancellation to be included in a block
57+
print("Waiting for blockchain...")
58+
time.sleep(3)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
import time
5+
import requests
6+
import helpers
7+
8+
# Vega wallet interaction helper, see login.py for detail
9+
from login import token, pubkey
10+
11+
# Load Vega node API v2 URL, this is set using 'source vega-config'
12+
# located in the root folder of the sample-api-scripts repository
13+
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
14+
# Load Vega wallet server URL, set in same way as above
15+
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
16+
17+
# Load Vega market id
18+
market_id = helpers.env_market_id()
19+
assert market_id != ""
20+
21+
# Set to False to ONLY submit/amend a liquidity commitment (no cancellation)
22+
CANCEL_LP_AFTER_SUBMISSION = True
23+
24+
# Set market id in ENV or uncomment the line below to override market id directly
25+
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
26+
27+
#####################################################################################
28+
# S U B M I T L I Q U I D I T Y C O M M I T M E N T #
29+
#####################################################################################
30+
31+
# Hint: commitmentAmount is an integer. For example 123456 is a price of 1.23456,
32+
# for a market which is configured to have a precision of 5 decimal places.
33+
34+
# __create_liquidity_commitment:
35+
# Compose your submit liquidity provision command
36+
# Set your own user specific reference to find the commitment by reference and
37+
# as a foreign key to your local client/trading application
38+
liquidity_ref = f"{pubkey}-{helpers.generate_id(30)}"
39+
submission = {
40+
"liquidityProvisionSubmission": {
41+
"marketId": market_id,
42+
"commitmentAmount": "100",
43+
"fee": "0.01",
44+
"buys": [
45+
{
46+
"offset": "1",
47+
"proportion": "1",
48+
"reference": "PEGGED_REFERENCE_MID"
49+
},
50+
{
51+
"offset": "2",
52+
"proportion": "2",
53+
"reference": "PEGGED_REFERENCE_MID"
54+
}
55+
],
56+
"sells": [
57+
{
58+
"offset": "1",
59+
"proportion": "1",
60+
"reference": "PEGGED_REFERENCE_MID"
61+
},
62+
{
63+
"offset": "2",
64+
"proportion": "2",
65+
"reference": "PEGGED_REFERENCE_MID"
66+
},
67+
{
68+
"offset": "3",
69+
"proportion": "5",
70+
"reference": "PEGGED_REFERENCE_MID"
71+
}
72+
],
73+
"reference": liquidity_ref
74+
},
75+
"pubKey": pubkey,
76+
"propagate": True
77+
}
78+
# :create_liquidity_commitment__
79+
80+
print("Liquidity commitment submission:\n{}".format(
81+
json.dumps(submission, indent=2, sort_keys=True)
82+
))
83+
84+
# __sign_tx_liquidity_submit:
85+
# Sign the transaction with an liquidity commitment command
86+
# Hint: Setting propagate to true will also submit to a Vega node
87+
url = f"{wallet_server_url}/api/v1/command/sync"
88+
headers = {"Authorization": f"Bearer {token}"}
89+
response = requests.post(url, headers=headers, json=submission)
90+
helpers.check_response(response)
91+
# :sign_tx_liquidity_submit__
92+
93+
print(json.dumps(response.json(), indent=4, sort_keys=True))
94+
print()
95+
96+
print("Signed liquidity commitment and sent to Vega")
97+
print()
98+
99+
# Wait for cancellation to be included in a block
100+
print("Waiting for blockchain...")
101+
time.sleep(3)

rest/liquidity-commitments-list.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
3+
import json
4+
import requests
5+
import helpers
6+
7+
# Vega wallet interaction helper, see login.py for detail
8+
from login import token, pubkey
9+
10+
# Load Vega node API v2 URL, this is set using 'source vega-config'
11+
# located in the root folder of the sample-api-scripts repository
12+
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
13+
# Load Vega wallet server URL, set in same way as above
14+
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
15+
16+
# Load Vega market id
17+
market_id = helpers.env_market_id()
18+
assert market_id != ""
19+
20+
# Set market id in ENV or uncomment the line below to override market id directly
21+
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
22+
23+
#####################################################################################
24+
# L I S T L I Q U I D I T Y P R O V I S I O N S #
25+
#####################################################################################
26+
# __get_liquidity_provisions:
27+
# Request liquidity provisions for a party on a Vega network
28+
url = f"{data_node_url_rest}/liquidity/provisions?partyId={pubkey}"
29+
headers = {"Authorization": f"Bearer {token}"}
30+
response = requests.get(url)
31+
helpers.check_response(response)
32+
print("Liquidity Provisions for party:\n{}".format(
33+
json.dumps(response.json(), indent=2, sort_keys=True)
34+
))
35+

rest/order-amend.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/python3
2+
3+
import requests
4+
import time
5+
import helpers
6+
import json
7+
8+
# Vega wallet interaction helper, see login.py for detail
9+
from login import token, pubkey
10+
11+
# Load Vega node API v2 URL, this is set using 'source vega-config'
12+
# located in the root folder of the sample-api-scripts repository
13+
data_node_url_rest = helpers.get_from_env("DATA_NODE_URL_REST")
14+
# Load Vega wallet server URL, set in same way as above
15+
wallet_server_url = helpers.get_from_env("WALLET_SERVER_URL")
16+
17+
# Load Vega market id
18+
market_id = helpers.env_market_id()
19+
assert market_id != ""
20+
21+
# Set to False to ONLY submit/amend an order (no cancellation)
22+
# e.g. orders will remain on the book
23+
CANCEL_ORDER_AFTER_SUBMISSION = True
24+
25+
# Set market id in ENV or uncomment the line below to override market id directly
26+
market_id = "e503cadb437861037cddfd7263d25b69102098a97573db23f8e5fc320cea1ce9"
27+
28+
# Grab order reference from original order submission
29+
order_ref = ""
30+
url = f"{data_node_url_rest}/orders?partyId={pubkey}&reference={order_ref}"
31+
response = requests.get(url)
32+
33+
found_order = helpers.get_nested_response(response, "orders")[0]["node"]
34+
35+
orderID = found_order["id"]
36+
orderStatus = found_order["status"]
37+
createVersion = found_order["version"]
38+
39+
###############################################################################
40+
# B L O C K C H A I N T I M E #
41+
###############################################################################
42+
43+
# __get_expiry_time:
44+
# Request the current blockchain time, calculate an expiry time
45+
response = requests.get(f"{data_node_url_rest}/vega/time")
46+
helpers.check_response(response)
47+
blockchain_time = int(response.json()["timestamp"])
48+
expiresAt = str(int(blockchain_time + 120 * 1e9)) # expire in 2 minutes
49+
# :get_expiry_time__
50+
51+
assert blockchain_time > 0
52+
print(f"Blockchain time: {blockchain_time}")
53+
54+
#####################################################################################
55+
# A M E N D O R D E R #
56+
#####################################################################################
57+
58+
# __amend_order:
59+
# Compose your amend order command, with changes to existing order
60+
amendment = {
61+
"orderAmendment": {
62+
"orderId": orderID,
63+
"marketId": market_id,
64+
"price": "2",
65+
"sizeDelta": "-25",
66+
"timeInForce": "TIME_IN_FORCE_GTC",
67+
},
68+
"pubKey": pubkey,
69+
"propagate": True
70+
}
71+
# :amend_order__
72+
73+
print()
74+
print("Order amendment: ", json.dumps(amendment, indent=2, sort_keys=True))
75+
print()
76+
77+
# __sign_tx_amend:
78+
# Sign the transaction with an order amendment command
79+
# Hint: Setting propagate to true will also submit to a Vega node
80+
url = f"{wallet_server_url}/api/v1/command/sync"
81+
headers = {"Authorization": f"Bearer {token}"}
82+
response = requests.post(url, headers=headers, json=amendment)
83+
helpers.check_response(response)
84+
# :sign_tx_amend__
85+
86+
print("Signed amendment and sent to Vega")
87+
88+
# Wait for amendment to be included in a block
89+
print("Waiting for blockchain...", end="", flush=True)
90+
time.sleep(3)
91+
92+
url = f"{data_node_url_rest}/orders?partyId={pubkey}&reference={order_ref}"
93+
response = requests.get(url)
94+
95+
found_order = helpers.get_nested_response(response, "orders")[0]["node"]
96+
97+
orderID = found_order["id"]
98+
orderPrice = found_order["price"]
99+
orderSize = found_order["size"]
100+
orderTif = found_order["timeInForce"]
101+
orderStatus = found_order["status"]
102+
orderVersion = found_order["version"]
103+
104+
print()
105+
print("Amended Order:")
106+
print(f"ID: {orderID}, Status: {orderStatus}, Price(Old): 1, "
107+
f"Price(New): {orderPrice}, Size(Old): 100, Size(New): {orderSize}, "
108+
f"TimeInForce(Old): TIME_IN_FORCE_GTT, TimeInForce(New): {orderTif}, "
109+
f"Version(Old): {createVersion}, Version(new): {orderVersion}")
110+
if orderStatus == "STATUS_REJECTED":
111+
print("The order amendment was rejected by Vega")
112+
exit(1) # Halt processing at this stage
113+
114+
if CANCEL_ORDER_AFTER_SUBMISSION is not True:
115+
exit(1)

0 commit comments

Comments
 (0)