Skip to content

Commit 356de43

Browse files
Update examples (#54)
* add sync chain client examples * add sync exchange client examples * add async chain client examples * add async exchange client examples
1 parent 9f57c29 commit 356de43

File tree

134 files changed

+3411
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+3411
-4
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=True)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
subaccount_id = address.get_subaccount_id(index=0)
39+
40+
# prepare trade info
41+
market_id = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30"
42+
orders = [
43+
composer.OrderData(
44+
market_id=market_id,
45+
subaccount_id=subaccount_id,
46+
order_hash="0xfcbedb1f8135204e7d8b8e6e683042e61834435fb7841b9ef243ef7196ec6938"
47+
),
48+
composer.OrderData(
49+
market_id=market_id,
50+
subaccount_id=subaccount_id,
51+
order_hash="0x0d19f6a10ad017abeac1b14070fec5d044128e40902085654f4da4055a8f6510"
52+
)
53+
]
54+
55+
# prepare tx msg
56+
msg = composer.MsgBatchCancelDerivativeOrders(
57+
sender=address.to_acc_bech32(),
58+
data=orders
59+
)
60+
61+
# build sim tx
62+
tx = (
63+
Transaction()
64+
.with_messages(msg)
65+
.with_sequence(address.get_sequence())
66+
.with_account_num(address.get_number())
67+
.with_chain_id(network.chain_id)
68+
)
69+
sim_sign_doc = tx.get_sign_doc(pub_key)
70+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
71+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
72+
73+
# simulate tx
74+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
75+
if not success:
76+
print(sim_res)
77+
return
78+
79+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
80+
print("simulation msg response")
81+
print(sim_res_msg)
82+
83+
# build tx
84+
gas_price = 500000000
85+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
86+
fee = [composer.Coin(
87+
amount=gas_price * gas_limit,
88+
denom=network.fee_denom,
89+
)]
90+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
91+
sign_doc = tx.get_sign_doc(pub_key)
92+
sig = priv_key.sign(sign_doc.SerializeToString())
93+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
94+
95+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
96+
res = await client.send_tx_block_mode(tx_raw_bytes)
97+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
98+
print("tx response")
99+
print(res)
100+
print("tx msg response")
101+
print(res_msg)
102+
103+
if __name__ == "__main__":
104+
logging.basicConfig(level=logging.INFO)
105+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=True)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
subaccount_id = address.get_subaccount_id(index=0)
39+
40+
# prepare trade info
41+
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
42+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
43+
44+
orders = [
45+
composer.SpotOrder(
46+
market_id=market_id,
47+
subaccount_id=subaccount_id,
48+
fee_recipient=fee_recipient,
49+
price=7.523,
50+
quantity=0.01,
51+
is_buy=True
52+
),
53+
composer.SpotOrder(
54+
market_id=market_id,
55+
subaccount_id=subaccount_id,
56+
fee_recipient=fee_recipient,
57+
price=27.92,
58+
quantity=0.01,
59+
is_buy=False
60+
),
61+
]
62+
63+
# prepare tx msg
64+
msg = composer.MsgBatchCreateSpotLimitOrders(
65+
sender=address.to_acc_bech32(),
66+
orders=orders
67+
)
68+
69+
# build sim tx
70+
tx = (
71+
Transaction()
72+
.with_messages(msg)
73+
.with_sequence(address.get_sequence())
74+
.with_account_num(address.get_number())
75+
.with_chain_id(network.chain_id)
76+
)
77+
sim_sign_doc = tx.get_sign_doc(pub_key)
78+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
79+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
80+
81+
# simulate tx
82+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
83+
if not success:
84+
print(sim_res)
85+
return
86+
87+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
88+
print("simulation msg response")
89+
print(sim_res_msg)
90+
91+
# build tx
92+
gas_price = 500000000
93+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
94+
fee = [composer.Coin(
95+
amount=gas_price * gas_limit,
96+
denom=network.fee_denom,
97+
)]
98+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
99+
sign_doc = tx.get_sign_doc(pub_key)
100+
sig = priv_key.sign(sign_doc.SerializeToString())
101+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
102+
103+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
104+
res = await client.send_tx_block_mode(tx_raw_bytes)
105+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
106+
print("tx response")
107+
print(res)
108+
print("tx msg response")
109+
print(res_msg)
110+
111+
if __name__ == "__main__":
112+
logging.basicConfig(level=logging.INFO)
113+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=True)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
subaccount_id = address.get_subaccount_id(index=0)
39+
40+
# prepare trade info
41+
market_id = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30"
42+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
43+
44+
orders = [
45+
composer.DerivativeOrder(
46+
market_id=market_id,
47+
subaccount_id=subaccount_id,
48+
fee_recipient=fee_recipient,
49+
price=41027,
50+
quantity=0.01,
51+
is_buy=True,
52+
leverage=0.7,
53+
),
54+
composer.DerivativeOrder(
55+
market_id=market_id,
56+
subaccount_id=subaccount_id,
57+
fee_recipient=fee_recipient,
58+
price=62140,
59+
quantity=0.01,
60+
is_buy=False,
61+
is_reduce_only=True
62+
),
63+
]
64+
65+
# prepare tx msg
66+
msg = composer.MsgBatchCreateDerivativeLimitOrders(
67+
sender=address.to_acc_bech32(),
68+
orders=orders
69+
)
70+
71+
# build sim tx
72+
tx = (
73+
Transaction()
74+
.with_messages(msg)
75+
.with_sequence(address.get_sequence())
76+
.with_account_num(address.get_number())
77+
.with_chain_id(network.chain_id)
78+
)
79+
sim_sign_doc = tx.get_sign_doc(pub_key)
80+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
81+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
82+
83+
# simulate tx
84+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
85+
if not success:
86+
print(sim_res)
87+
return
88+
89+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
90+
print("simulation msg response")
91+
print(sim_res_msg)
92+
93+
# build tx
94+
gas_price = 500000000
95+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
96+
fee = [composer.Coin(
97+
amount=gas_price * gas_limit,
98+
denom=network.fee_denom,
99+
)]
100+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
101+
sign_doc = tx.get_sign_doc(pub_key)
102+
sig = priv_key.sign(sign_doc.SerializeToString())
103+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
104+
105+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
106+
res = await client.send_tx_block_mode(tx_raw_bytes)
107+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
108+
print("tx response")
109+
print(res)
110+
print("tx msg response")
111+
print(res_msg)
112+
113+
if __name__ == "__main__":
114+
logging.basicConfig(level=logging.INFO)
115+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)