-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path9_MsgBatchUpdateOrders.py
More file actions
172 lines (149 loc) · 5.94 KB
/
9_MsgBatchUpdateOrders.py
File metadata and controls
172 lines (149 loc) · 5.94 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import asyncio
import json
import os
import uuid
from decimal import Decimal
import dotenv
from pyinjective.async_client_v2 import AsyncClient
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
from pyinjective.core.network import Network
from pyinjective.wallet import PrivateKey
async def main() -> None:
dotenv.load_dotenv()
private_key_in_hexa = os.getenv("INJECTIVE_PRIVATE_KEY")
# select network: local, testnet, mainnet
network = Network.testnet()
# initialize grpc client
client = AsyncClient(network)
composer = await client.composer()
gas_price = await client.current_chain_gas_price()
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
gas_price = int(gas_price * 1.1)
message_broadcaster = MsgBroadcasterWithPk.new_using_gas_heuristics(
network=network,
private_key=private_key_in_hexa,
gas_price=gas_price,
client=client,
composer=composer,
)
priv_key = PrivateKey.from_hex(private_key_in_hexa)
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
subaccount_id = address.get_subaccount_id(index=0)
# prepare trade info
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
derivative_market_id_create = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
spot_market_id_create = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
derivative_market_id_cancel = "0xd5e4b12b19ecf176e4e14b42944731c27677819d2ed93be4104ad7025529c7ff"
derivative_market_id_cancel_2 = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
spot_market_id_cancel = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
spot_market_id_cancel_2 = "0x7a57e705bb4e09c88aecfc295569481dbf2fe1d5efe364651fbe72385938e9b0"
derivative_orders_to_cancel = [
composer.create_order_data_without_mask(
market_id=derivative_market_id_cancel,
subaccount_id=subaccount_id,
order_hash="0x48690013c382d5dbaff9989db04629a16a5818d7524e027d517ccc89fd068103",
),
composer.create_order_data_without_mask(
market_id=derivative_market_id_cancel_2,
subaccount_id=subaccount_id,
order_hash="0x7ee76255d7ca763c56b0eab9828fca89fdd3739645501c8a80f58b62b4f76da5",
),
]
spot_orders_to_cancel = [
composer.create_order_data_without_mask(
market_id=spot_market_id_cancel,
subaccount_id=subaccount_id,
cid="0e5c3ad5-2cc4-4a2a-bbe5-b12697739163",
),
composer.create_order_data_without_mask(
market_id=spot_market_id_cancel_2,
subaccount_id=subaccount_id,
order_hash="0x222daa22f60fe9f075ed0ca583459e121c23e64431c3fbffdedda04598ede0d2",
),
]
derivative_orders_to_create = [
composer.derivative_order(
market_id=derivative_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("25000"),
quantity=Decimal("0.1"),
margin=Decimal("2500"),
order_type="BUY",
cid=str(uuid.uuid4()),
),
composer.derivative_order(
market_id=derivative_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("50000"),
quantity=Decimal("0.01"),
margin=Decimal("500"),
order_type="SELL",
cid=str(uuid.uuid4()),
),
]
derivative_market_orders_to_create = [
composer.derivative_order(
market_id=derivative_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("25100"),
quantity=Decimal("0.1"),
margin=Decimal("2510"),
order_type="BUY",
cid=str(uuid.uuid4()),
),
]
spot_orders_to_create = [
composer.spot_order(
market_id=spot_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("3"),
quantity=Decimal("55"),
order_type="BUY",
cid=str(uuid.uuid4()),
),
composer.spot_order(
market_id=spot_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("300"),
quantity=Decimal("55"),
order_type="SELL",
cid=str(uuid.uuid4()),
),
]
spot_market_orders_to_create = [
composer.spot_order(
market_id=spot_market_id_create,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=Decimal("3.5"),
quantity=Decimal("1"),
order_type="BUY",
cid=str(uuid.uuid4()),
),
]
# prepare tx msg
msg = composer.msg_batch_update_orders(
sender=address.to_acc_bech32(),
derivative_orders_to_create=derivative_orders_to_create,
spot_orders_to_create=spot_orders_to_create,
derivative_orders_to_cancel=derivative_orders_to_cancel,
spot_orders_to_cancel=spot_orders_to_cancel,
spot_market_orders_to_create=spot_market_orders_to_create,
derivative_market_orders_to_create=derivative_market_orders_to_create,
)
# broadcast the transaction
result = await message_broadcaster.broadcast([msg])
print("---Transaction Response---")
print(json.dumps(result, indent=2))
gas_price = await client.current_chain_gas_price()
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
gas_price = int(gas_price * 1.1)
message_broadcaster.update_gas_price(gas_price=gas_price)
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())