2121from pyinjective .transaction import Transaction
2222from pyinjective .constant import Network
2323from pyinjective .wallet import PrivateKey , PublicKey , Address
24+ from pyinjective .orderhash import compute_order_hashes
2425
2526
2627async def main () -> None :
@@ -38,57 +39,70 @@ async def main() -> None:
3839 subaccount_id = address .get_subaccount_id (index = 0 )
3940
4041 # prepare trade info
41- market_id1 = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
42- market_id2 = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30 "
42+ spot_market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
43+ deriv_market_id = "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce "
4344 fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
4445
46+ spot_orders = [
47+ composer .SpotOrder (
48+ market_id = spot_market_id ,
49+ subaccount_id = subaccount_id ,
50+ fee_recipient = fee_recipient ,
51+ price = 3.524 ,
52+ quantity = 0.01 ,
53+ is_buy = True
54+ ),
55+ composer .SpotOrder (
56+ market_id = spot_market_id ,
57+ subaccount_id = subaccount_id ,
58+ fee_recipient = fee_recipient ,
59+ price = 27.92 ,
60+ quantity = 0.01 ,
61+ is_buy = False
62+ ),
63+ ]
64+
65+ deriv_orders = [
66+ composer .DerivativeOrder (
67+ market_id = deriv_market_id ,
68+ subaccount_id = subaccount_id ,
69+ fee_recipient = fee_recipient ,
70+ price = 31027 ,
71+ quantity = 0.01 ,
72+ leverage = 0.7 ,
73+ is_buy = True ,
74+ ),
75+ composer .DerivativeOrder (
76+ market_id = deriv_market_id ,
77+ subaccount_id = subaccount_id ,
78+ fee_recipient = fee_recipient ,
79+ price = 62140 ,
80+ quantity = 0.01 ,
81+ leverage = 0.7 ,
82+ is_buy = False ,
83+ is_reduce_only = False
84+ ),
85+ ]
86+
4587 # prepare tx msg
46- msg1 = composer .MsgCreateSpotLimitOrder (
88+ spot_msg = composer .MsgBatchCreateSpotLimitOrders (
4789 sender = address .to_acc_bech32 (),
48- market_id = market_id1 ,
49- subaccount_id = subaccount_id ,
50- fee_recipient = fee_recipient ,
51- price = 7.523 ,
52- quantity = 0.01 ,
53- is_buy = True
90+ orders = spot_orders
5491 )
5592
56- msg2 = composer .MsgBatchCancelSpotOrders (
93+ deriv_msg = composer .MsgBatchCreateDerivativeLimitOrders (
5794 sender = address .to_acc_bech32 (),
58- data = [
59- composer .OrderData (
60- market_id = market_id2 ,
61- subaccount_id = subaccount_id ,
62- order_hash = "0x098f2c92336bb1ec3591435df1e135052760310bc08fc16e3b9bc409885b863b"
63- ),
64- composer .OrderData (
65- market_id = market_id2 ,
66- subaccount_id = subaccount_id ,
67- order_hash = "0x8d4e780927f91011bf77dea8b625948a14c1ae55d8c5d3f5af3dadbd6bec591d"
68- ),
69- composer .OrderData (
70- market_id = market_id2 ,
71- subaccount_id = subaccount_id ,
72- order_hash = "0x8d4e111127f91011bf77dea8b625948a14c1ae55d8c5d3f5af3dadbd6bec591d"
73- )
74- ]
95+ orders = deriv_orders
7596 )
7697
77- msg3 = composer .MsgCreateDerivativeLimitOrder (
78- sender = address .to_acc_bech32 (),
79- market_id = market_id2 ,
80- subaccount_id = subaccount_id ,
81- fee_recipient = fee_recipient ,
82- price = 44054.48 ,
83- quantity = 0.01 ,
84- leverage = 0.7 ,
85- is_buy = True
86- )
98+ # compute order hashes
99+ order_hashes = compute_order_hashes (network , spot_orders + deriv_orders )
100+ print ("The order hashes: " , order_hashes )
87101
88102 # build sim tx
89103 tx = (
90104 Transaction ()
91- .with_messages (msg1 , msg2 , msg3 )
105+ .with_messages (spot_msg , deriv_msg )
92106 .with_sequence (address .get_sequence ())
93107 .with_account_num (address .get_number ())
94108 .with_chain_id (network .chain_id )
@@ -98,34 +112,29 @@ async def main() -> None:
98112 sim_tx_raw_bytes = tx .get_tx_data (sim_sig , pub_key )
99113
100114 # simulate tx
101- (sim_res , success ) = await client .simulate_tx (sim_tx_raw_bytes )
115+ (simRes , success ) = await client .simulate_tx (sim_tx_raw_bytes )
102116 if not success :
103- print (sim_res )
117+ print (simRes )
104118 return
105119
106- sim_res_msg = ProtoMsgComposer .MsgResponses (sim_res .result .data , simulation = True )
107- print ("simulation msg response" )
108- print (sim_res_msg )
120+ sim_res_msg = ProtoMsgComposer .MsgResponses (simRes .result .data , simulation = True )
109121
110122 # build tx
111123 gas_price = 500000000
112- gas_limit = sim_res .gas_info .gas_used + 20000 # add 15k for gas, fee computation
124+ gas_limit = simRes .gas_info .gas_used + 20000 # add 20k for gas, fee computation
113125 fee = [composer .Coin (
114126 amount = gas_price * gas_limit ,
115127 denom = network .fee_denom ,
116128 )]
129+
117130 tx = tx .with_gas (gas_limit ).with_fee (fee ).with_memo ("" ).with_timeout_height (0 )
118131 sign_doc = tx .get_sign_doc (pub_key )
119132 sig = priv_key .sign (sign_doc .SerializeToString ())
120133 tx_raw_bytes = tx .get_tx_data (sig , pub_key )
121-
134+
122135 # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
123136 res = await client .send_tx_block_mode (tx_raw_bytes )
124137 res_msg = ProtoMsgComposer .MsgResponses (res .data )
125- print ("tx response" )
126- print (res )
127- print ("tx msg response" )
128- print (res_msg )
129138
130139if __name__ == "__main__" :
131140 logging .basicConfig (level = logging .INFO )
0 commit comments