|
| 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