11import asyncio
2+ import os
23import uuid
4+ from decimal import Decimal
5+
6+ import dotenv
37
48from pyinjective .async_client import AsyncClient
59from pyinjective .constant import GAS_FEE_BUFFER_AMOUNT , GAS_PRICE
1014
1115
1216async def main () -> None :
17+ dotenv .load_dotenv ()
18+ configured_private_key = os .getenv ("INJECTIVE_PRIVATE_KEY" )
19+
1320 # select network: local, testnet, mainnet
1421 network = Network .testnet ()
1522
@@ -19,7 +26,7 @@ async def main() -> None:
1926 await client .sync_timeout_height ()
2027
2128 # load account
22- priv_key = PrivateKey .from_hex ("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3" )
29+ priv_key = PrivateKey .from_hex (configured_private_key )
2330 pub_key = priv_key .to_public_key ()
2431 address = pub_key .to_address ()
2532 await client .fetch_account (address .to_acc_bech32 ())
@@ -34,57 +41,59 @@ async def main() -> None:
3441 fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
3542
3643 spot_orders = [
37- composer .SpotOrder (
44+ composer .spot_order (
3845 market_id = spot_market_id ,
3946 subaccount_id = subaccount_id ,
4047 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" ,
4551 cid = str (uuid .uuid4 ()),
4652 ),
47- composer .SpotOrder (
53+ composer .spot_order (
4854 market_id = spot_market_id ,
4955 subaccount_id = subaccount_id ,
5056 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" ,
5560 cid = str (uuid .uuid4 ()),
5661 ),
5762 ]
5863
5964 derivative_orders = [
60- composer .DerivativeOrder (
65+ composer .derivative_order (
6166 market_id = deriv_market_id ,
6267 subaccount_id = subaccount_id ,
6368 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" ,
6975 cid = str (uuid .uuid4 ()),
7076 ),
71- composer .DerivativeOrder (
77+ composer .derivative_order (
7278 market_id = deriv_market_id ,
7379 subaccount_id = subaccount_id ,
7480 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" ,
8087 cid = str (uuid .uuid4 ()),
8188 ),
8289 ]
8390
8491 # 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 )
8693
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+ )
8897
8998 # compute order hashes
9099 order_hashes = order_hash_manager .compute_order_hashes (
@@ -107,7 +116,7 @@ async def main() -> None:
107116 gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
108117 gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
109118 fee = [
110- composer .Coin (
119+ composer .coin (
111120 amount = gas_price * gas_limit ,
112121 denom = network .fee_denom ,
113122 )
@@ -144,7 +153,7 @@ async def main() -> None:
144153 gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
145154 gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
146155 fee = [
147- composer .Coin (
156+ composer .coin (
148157 amount = gas_price * gas_limit ,
149158 denom = network .fee_denom ,
150159 )
@@ -161,57 +170,59 @@ async def main() -> None:
161170 print ("gas fee: {} INJ" .format (gas_fee ))
162171
163172 spot_orders = [
164- composer .SpotOrder (
173+ composer .spot_order (
165174 market_id = spot_market_id ,
166175 subaccount_id = subaccount_id_2 ,
167176 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" ,
172180 cid = str (uuid .uuid4 ()),
173181 ),
174- composer .SpotOrder (
182+ composer .spot_order (
175183 market_id = spot_market_id ,
176184 subaccount_id = subaccount_id_2 ,
177185 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" ,
182189 cid = str (uuid .uuid4 ()),
183190 ),
184191 ]
185192
186193 derivative_orders = [
187- composer .DerivativeOrder (
194+ composer .derivative_order (
188195 market_id = deriv_market_id ,
189196 subaccount_id = subaccount_id_2 ,
190197 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" ,
196204 cid = str (uuid .uuid4 ()),
197205 ),
198- composer .DerivativeOrder (
206+ composer .derivative_order (
199207 market_id = deriv_market_id ,
200208 subaccount_id = subaccount_id_2 ,
201209 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" ,
207216 cid = str (uuid .uuid4 ()),
208217 ),
209218 ]
210219
211220 # 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 )
213222
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+ )
215226
216227 # compute order hashes
217228 order_hashes = order_hash_manager .compute_order_hashes (
@@ -234,7 +245,7 @@ async def main() -> None:
234245 gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
235246 gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
236247 fee = [
237- composer .Coin (
248+ composer .coin (
238249 amount = gas_price * gas_limit ,
239250 denom = network .fee_denom ,
240251 )
0 commit comments