Skip to content

Commit 7446cd6

Browse files
Merge pull request #85 from InjectiveLabs/f/update-examples
chore: update examples for local order hash calculation
2 parents 753e4c3 + 9c86bc7 commit 7446cd6

File tree

3 files changed

+112
-241
lines changed

3 files changed

+112
-241
lines changed

examples/async/chain_client/MultipleMsgs.py

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pyinjective.transaction import Transaction
2222
from pyinjective.constant import Network
2323
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
from pyinjective.orderhash import compute_order_hashes
2425

2526

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

130139
if __name__ == "__main__":
131140
logging.basicConfig(level=logging.INFO)

examples/sync/chain_client/MultipleMsgs.py

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pyinjective.transaction import Transaction
2222
from pyinjective.constant import Network
2323
from pyinjective.wallet import PrivateKey, PublicKey, Address
24-
24+
from pyinjective.orderhash import compute_order_hashes
2525

2626
async def main() -> None:
2727
# select network: local, testnet, mainnet
@@ -32,63 +32,76 @@ async def main() -> None:
3232
client = Client(network, insecure=False)
3333

3434
# load account
35-
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
35+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
3636
pub_key = priv_key.to_public_key()
3737
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
3838
subaccount_id = address.get_subaccount_id(index=0)
3939

4040
# prepare trade info
41-
market_id1 = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
42-
market_id2 = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30"
41+
spot_market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
42+
deriv_market_id = "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce"
4343
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
4444

45+
spot_orders = [
46+
composer.SpotOrder(
47+
market_id=spot_market_id,
48+
subaccount_id=subaccount_id,
49+
fee_recipient=fee_recipient,
50+
price=3.524,
51+
quantity=0.01,
52+
is_buy=True
53+
),
54+
composer.SpotOrder(
55+
market_id=spot_market_id,
56+
subaccount_id=subaccount_id,
57+
fee_recipient=fee_recipient,
58+
price=27.92,
59+
quantity=0.01,
60+
is_buy=False
61+
),
62+
]
63+
64+
deriv_orders = [
65+
composer.DerivativeOrder(
66+
market_id=deriv_market_id,
67+
subaccount_id=subaccount_id,
68+
fee_recipient=fee_recipient,
69+
price=31027,
70+
quantity=0.01,
71+
leverage=0.7,
72+
is_buy=True,
73+
),
74+
composer.DerivativeOrder(
75+
market_id=deriv_market_id,
76+
subaccount_id=subaccount_id,
77+
fee_recipient=fee_recipient,
78+
price=62140,
79+
quantity=0.01,
80+
leverage=0.7,
81+
is_buy=False,
82+
is_reduce_only=False
83+
),
84+
]
85+
4586
# prepare tx msg
46-
msg1 = composer.MsgCreateSpotLimitOrder(
87+
spot_msg = composer.MsgBatchCreateSpotLimitOrders(
4788
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
89+
orders=spot_orders
5490
)
5591

56-
msg2 = composer.MsgBatchCancelSpotOrders(
92+
deriv_msg = composer.MsgBatchCreateDerivativeLimitOrders(
5793
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-
]
94+
orders=deriv_orders
7595
)
7696

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-
)
97+
# compute order hashes
98+
order_hashes = compute_order_hashes(network, spot_orders + deriv_orders)
99+
print("The order hashes: ", order_hashes)
87100

88101
# build sim tx
89102
tx = (
90103
Transaction()
91-
.with_messages(msg1, msg2, msg3)
104+
.with_messages(spot_msg,deriv_msg)
92105
.with_sequence(address.get_sequence())
93106
.with_account_num(address.get_number())
94107
.with_chain_id(network.chain_id)
@@ -104,8 +117,6 @@ async def main() -> None:
104117
return
105118

106119
sim_res_msg = ProtoMsgComposer.MsgResponses(simRes.result.data, simulation=True)
107-
print("simulation msg response")
108-
print(sim_res_msg)
109120

110121
# build tx
111122
gas_price = 500000000
@@ -119,14 +130,10 @@ async def main() -> None:
119130
sign_doc = tx.get_sign_doc(pub_key)
120131
sig = priv_key.sign(sign_doc.SerializeToString())
121132
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
122-
133+
123134
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
124135
res = client.send_tx_block_mode(tx_raw_bytes)
125136
res_msg = ProtoMsgComposer.MsgResponses(res.data)
126-
print("tx response")
127-
print(res)
128-
print("tx msg response")
129-
print(res_msg)
130137

131138
if __name__ == "__main__":
132139
logging.basicConfig(level=logging.INFO)

0 commit comments

Comments
 (0)