Skip to content

Commit 260548a

Browse files
committed
Add orderhash example
1 parent 9211abd commit 260548a

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright 2021 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Injective Chain Tx/Query client for Python. Example only."""
15+
16+
import sys
17+
sys.path.insert(0, '/Users/nam/desktop/injective/sdk-python/')
18+
19+
20+
import asyncio
21+
import logging
22+
23+
from pyinjective.composer import Composer as ProtoMsgComposer
24+
from pyinjective.client import Client
25+
from pyinjective.transaction import Transaction
26+
from pyinjective.constant import Network
27+
from pyinjective.wallet import PrivateKey, PublicKey, Address
28+
from pyinjective.orderhash import compute_order_hashes
29+
30+
async def main() -> None:
31+
# select network: local, testnet, mainnet
32+
network = Network.testnet()
33+
composer = ProtoMsgComposer(network=network.string())
34+
35+
# initialize grpc client
36+
client = Client(network, insecure=False)
37+
38+
# load account
39+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
40+
pub_key = priv_key.to_public_key()
41+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
42+
subaccount_id = address.get_subaccount_id(index=0)
43+
44+
# prepare trade info
45+
spot_market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
46+
deriv_market_id = "0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce"
47+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
48+
49+
spot_orders = [
50+
composer.SpotOrder(
51+
market_id=spot_market_id,
52+
subaccount_id=subaccount_id,
53+
fee_recipient=fee_recipient,
54+
price=7.523,
55+
quantity=0.01,
56+
is_buy=True
57+
),
58+
composer.SpotOrder(
59+
market_id=spot_market_id,
60+
subaccount_id=subaccount_id,
61+
fee_recipient=fee_recipient,
62+
price=27.92,
63+
quantity=0.01,
64+
is_buy=False
65+
),
66+
]
67+
68+
deriv_orders = [
69+
composer.DerivativeOrder(
70+
market_id=deriv_market_id,
71+
subaccount_id=subaccount_id,
72+
fee_recipient=fee_recipient,
73+
price=41027,
74+
quantity=0.01,
75+
is_buy=True,
76+
leverage=0.7,
77+
),
78+
composer.DerivativeOrder(
79+
market_id=deriv_market_id,
80+
subaccount_id=subaccount_id,
81+
fee_recipient=fee_recipient,
82+
price=62140,
83+
quantity=0.01,
84+
is_buy=False,
85+
is_reduce_only=True
86+
),
87+
]
88+
89+
# prepare tx msg
90+
spot_msg = composer.MsgBatchCreateSpotLimitOrders(
91+
sender=address.to_acc_bech32(),
92+
orders=spot_orders
93+
)
94+
95+
deriv_msg = composer.MsgBatchCreateDerivativeLimitOrders(
96+
sender=address.to_acc_bech32(),
97+
orders=deriv_orders
98+
)
99+
100+
# compute order hashes
101+
order_hashes = compute_order_hashes(network, spot_orders + deriv_orders)
102+
print(order_hashes)
103+
104+
# build sim tx
105+
tx = (
106+
Transaction()
107+
.with_messages(spot_msg,deriv_msg)
108+
.with_sequence(address.get_sequence())
109+
.with_account_num(address.get_number())
110+
.with_chain_id(network.chain_id)
111+
)
112+
sim_sign_doc = tx.get_sign_doc(pub_key)
113+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
114+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
115+
116+
# simulate tx
117+
(simRes, success) = client.simulate_tx(sim_tx_raw_bytes)
118+
if not success:
119+
print(simRes)
120+
return
121+
122+
sim_res_msg = ProtoMsgComposer.MsgResponses(simRes.result.data, simulation=True)
123+
print("simulation msg response")
124+
print(sim_res_msg)
125+
126+
# # build tx
127+
# gas_price = 500000000
128+
# gas_limit = simRes.gas_info.gas_used + 20000 # add 20k for gas, fee computation
129+
# fee = [composer.Coin(
130+
# amount=gas_price * gas_limit,
131+
# denom=network.fee_denom,
132+
# )]
133+
# current_height = client.get_latest_block().block.header.height
134+
# tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(current_height+50)
135+
# sign_doc = tx.get_sign_doc(pub_key)
136+
# sig = priv_key.sign(sign_doc.SerializeToString())
137+
# tx_raw_bytes = tx.get_tx_data(sig, pub_key)
138+
#
139+
# # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
140+
# res = client.send_tx_block_mode(tx_raw_bytes)
141+
# res_msg = ProtoMsgComposer.MsgResponses(res.data)
142+
# print("tx response")
143+
# print(res)
144+
# print("tx msg response")
145+
# print(res_msg)
146+
147+
if __name__ == "__main__":
148+
logging.basicConfig(level=logging.INFO)
149+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)