1
1
import asyncio
2
+ import os
2
3
import uuid
4
+ from decimal import Decimal
5
+
6
+ import dotenv
3
7
4
8
from pyinjective .async_client import AsyncClient
5
9
from pyinjective .constant import GAS_FEE_BUFFER_AMOUNT , GAS_PRICE
10
14
11
15
12
16
async def main () -> None :
17
+ dotenv .load_dotenv ()
18
+ configured_private_key = os .getenv ("INJECTIVE_PRIVATE_KEY" )
19
+
13
20
# select network: local, testnet, mainnet
14
21
network = Network .testnet ()
15
22
@@ -19,7 +26,7 @@ async def main() -> None:
19
26
await client .sync_timeout_height ()
20
27
21
28
# load account
22
- priv_key = PrivateKey .from_hex ("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3" )
29
+ priv_key = PrivateKey .from_hex (configured_private_key )
23
30
pub_key = priv_key .to_public_key ()
24
31
address = pub_key .to_address ()
25
32
await client .fetch_account (address .to_acc_bech32 ())
@@ -34,57 +41,59 @@ async def main() -> None:
34
41
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
35
42
36
43
spot_orders = [
37
- composer .SpotOrder (
44
+ composer .spot_order (
38
45
market_id = spot_market_id ,
39
46
subaccount_id = subaccount_id ,
40
47
fee_recipient = fee_recipient ,
41
- price = 0.524 ,
42
- quantity = 0.01 ,
43
- is_buy = True ,
44
- is_po = False ,
48
+ price = Decimal ("0.524" ),
49
+ quantity = Decimal ("0.01" ),
50
+ order_type = "BUY" ,
45
51
cid = str (uuid .uuid4 ()),
46
52
),
47
- composer .SpotOrder (
53
+ composer .spot_order (
48
54
market_id = spot_market_id ,
49
55
subaccount_id = subaccount_id ,
50
56
fee_recipient = fee_recipient ,
51
- price = 27.92 ,
52
- quantity = 0.01 ,
53
- is_buy = False ,
54
- is_po = False ,
57
+ price = Decimal ("27.92" ),
58
+ quantity = Decimal ("0.01" ),
59
+ order_type = "SELL" ,
55
60
cid = str (uuid .uuid4 ()),
56
61
),
57
62
]
58
63
59
64
derivative_orders = [
60
- composer .DerivativeOrder (
65
+ composer .derivative_order (
61
66
market_id = deriv_market_id ,
62
67
subaccount_id = subaccount_id ,
63
68
fee_recipient = fee_recipient ,
64
- price = 10500 ,
65
- quantity = 0.01 ,
66
- leverage = 1.5 ,
67
- is_buy = True ,
68
- is_po = False ,
69
+ price = Decimal (10500 ),
70
+ quantity = Decimal (0.01 ),
71
+ margin = composer .calculate_margin (
72
+ quantity = Decimal (0.01 ), price = Decimal (10500 ), leverage = Decimal (2 ), is_reduce_only = False
73
+ ),
74
+ order_type = "BUY" ,
69
75
cid = str (uuid .uuid4 ()),
70
76
),
71
- composer .DerivativeOrder (
77
+ composer .derivative_order (
72
78
market_id = deriv_market_id ,
73
79
subaccount_id = subaccount_id ,
74
80
fee_recipient = fee_recipient ,
75
- price = 65111 ,
76
- quantity = 0.01 ,
77
- leverage = 2 ,
78
- is_buy = False ,
79
- is_reduce_only = False ,
81
+ price = Decimal (65111 ),
82
+ quantity = Decimal (0.01 ),
83
+ margin = composer .calculate_margin (
84
+ quantity = Decimal (0.01 ), price = Decimal (65111 ), leverage = Decimal (2 ), is_reduce_only = False
85
+ ),
86
+ order_type = "SELL" ,
80
87
cid = str (uuid .uuid4 ()),
81
88
),
82
89
]
83
90
84
91
# prepare tx msg
85
- spot_msg = composer .MsgBatchCreateSpotLimitOrders (sender = address .to_acc_bech32 (), orders = spot_orders )
92
+ spot_msg = composer .msg_batch_create_spot_limit_orders (sender = address .to_acc_bech32 (), orders = spot_orders )
86
93
87
- deriv_msg = composer .MsgBatchCreateDerivativeLimitOrders (sender = address .to_acc_bech32 (), orders = derivative_orders )
94
+ deriv_msg = composer .msg_batch_create_derivative_limit_orders (
95
+ sender = address .to_acc_bech32 (), orders = derivative_orders
96
+ )
88
97
89
98
# compute order hashes
90
99
order_hashes = order_hash_manager .compute_order_hashes (
@@ -107,7 +116,7 @@ async def main() -> None:
107
116
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
108
117
gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
109
118
fee = [
110
- composer .Coin (
119
+ composer .coin (
111
120
amount = gas_price * gas_limit ,
112
121
denom = network .fee_denom ,
113
122
)
@@ -144,7 +153,7 @@ async def main() -> None:
144
153
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
145
154
gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
146
155
fee = [
147
- composer .Coin (
156
+ composer .coin (
148
157
amount = gas_price * gas_limit ,
149
158
denom = network .fee_denom ,
150
159
)
@@ -161,57 +170,59 @@ async def main() -> None:
161
170
print ("gas fee: {} INJ" .format (gas_fee ))
162
171
163
172
spot_orders = [
164
- composer .SpotOrder (
173
+ composer .spot_order (
165
174
market_id = spot_market_id ,
166
175
subaccount_id = subaccount_id_2 ,
167
176
fee_recipient = fee_recipient ,
168
- price = 1.524 ,
169
- quantity = 0.01 ,
170
- is_buy = True ,
171
- is_po = True ,
177
+ price = Decimal ("1.524" ),
178
+ quantity = Decimal ("0.01" ),
179
+ order_type = "BUY_PO" ,
172
180
cid = str (uuid .uuid4 ()),
173
181
),
174
- composer .SpotOrder (
182
+ composer .spot_order (
175
183
market_id = spot_market_id ,
176
184
subaccount_id = subaccount_id_2 ,
177
185
fee_recipient = fee_recipient ,
178
- price = 27.92 ,
179
- quantity = 0.01 ,
180
- is_buy = False ,
181
- is_po = False ,
186
+ price = Decimal ("27.92" ),
187
+ quantity = Decimal ("0.01" ),
188
+ order_type = "SELL_PO" ,
182
189
cid = str (uuid .uuid4 ()),
183
190
),
184
191
]
185
192
186
193
derivative_orders = [
187
- composer .DerivativeOrder (
194
+ composer .derivative_order (
188
195
market_id = deriv_market_id ,
189
196
subaccount_id = subaccount_id_2 ,
190
197
fee_recipient = fee_recipient ,
191
- price = 25111 ,
192
- quantity = 0.01 ,
193
- leverage = 1.5 ,
194
- is_buy = True ,
195
- is_po = False ,
198
+ price = Decimal (25111 ),
199
+ quantity = Decimal (0.01 ),
200
+ margin = composer .calculate_margin (
201
+ quantity = Decimal (0.01 ), price = Decimal (25111 ), leverage = Decimal ("1.5" ), is_reduce_only = False
202
+ ),
203
+ order_type = "BUY" ,
196
204
cid = str (uuid .uuid4 ()),
197
205
),
198
- composer .DerivativeOrder (
206
+ composer .derivative_order (
199
207
market_id = deriv_market_id ,
200
208
subaccount_id = subaccount_id_2 ,
201
209
fee_recipient = fee_recipient ,
202
- price = 65111 ,
203
- quantity = 0.01 ,
204
- leverage = 2 ,
205
- is_buy = False ,
206
- is_reduce_only = False ,
210
+ price = Decimal (65111 ),
211
+ quantity = Decimal (0.01 ),
212
+ margin = composer .calculate_margin (
213
+ quantity = Decimal (0.01 ), price = Decimal (25111 ), leverage = Decimal (2 ), is_reduce_only = False
214
+ ),
215
+ order_type = "SELL" ,
207
216
cid = str (uuid .uuid4 ()),
208
217
),
209
218
]
210
219
211
220
# prepare tx msg
212
- spot_msg = composer .MsgBatchCreateSpotLimitOrders (sender = address .to_acc_bech32 (), orders = spot_orders )
221
+ spot_msg = composer .msg_batch_create_spot_limit_orders (sender = address .to_acc_bech32 (), orders = spot_orders )
213
222
214
- deriv_msg = composer .MsgBatchCreateDerivativeLimitOrders (sender = address .to_acc_bech32 (), orders = derivative_orders )
223
+ deriv_msg = composer .msg_batch_create_derivative_limit_orders (
224
+ sender = address .to_acc_bech32 (), orders = derivative_orders
225
+ )
215
226
216
227
# compute order hashes
217
228
order_hashes = order_hash_manager .compute_order_hashes (
@@ -234,7 +245,7 @@ async def main() -> None:
234
245
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
235
246
gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
236
247
fee = [
237
- composer .Coin (
248
+ composer .coin (
238
249
amount = gas_price * gas_limit ,
239
250
denom = network .fee_denom ,
240
251
)
0 commit comments