Skip to content

Commit edc20c1

Browse files
committed
Refactor and add more examples
1 parent 1fd3585 commit edc20c1

10 files changed

+334
-6
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
14+
# initialize grpc client
15+
client = Client(network.grpc_endpoint, insecure=True)
16+
17+
# load account
18+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
19+
pub_key = priv_key.to_public_key()
20+
address = pub_key.to_address()
21+
subaccount_id = address.get_subaccount_id(index=0)
22+
23+
# prepare trade info
24+
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25+
orders = [
26+
ProtoMsgComposer.OrderData(
27+
market_id=market_id,
28+
subaccount_id=subaccount_id,
29+
order_hash="0x098f2c92336bb1ec3591435df1e135052760310bc08fc16e3b9bc409885b863b"
30+
),
31+
ProtoMsgComposer.OrderData(
32+
market_id=market_id,
33+
subaccount_id=subaccount_id,
34+
order_hash="0x8d4e780927f91011bf77dea8b625948a14c1ae55d8c5d3f5af3dadbd6bec591d"
35+
)
36+
]
37+
38+
# prepare tx msg
39+
msg = ProtoMsgComposer.MsgBatchCancelDerivativeOrders(
40+
sender=address.to_acc_bech32(),
41+
data=orders
42+
)
43+
44+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
45+
gas_price = 500000000
46+
gas_limit = 200000
47+
fee = [ProtoMsgComposer.Coin(
48+
amount=str(gas_price * gas_limit),
49+
denom=network.fee_denom,
50+
)]
51+
52+
# build tx
53+
tx = (
54+
Transaction()
55+
.with_messages(msg)
56+
.with_sequence(acc_seq)
57+
.with_account_num(acc_num)
58+
.with_chain_id(network.chain_id)
59+
.with_gas(gas_limit)
60+
.with_fee(fee)
61+
.with_memo("")
62+
.with_timeout_height(0)
63+
)
64+
65+
# build signed tx
66+
sign_doc = tx.get_sign_doc(pub_key)
67+
sig = priv_key.sign(sign_doc.SerializeToString())
68+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
69+
70+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
71+
res = client.send_tx_block_mode(tx_raw_bytes)
72+
73+
# print tx response
74+
print(res)
75+
76+
if __name__ == "__main__":
77+
logging.basicConfig(level=logging.INFO)
78+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
14+
# initialize grpc client
15+
client = Client(network.grpc_endpoint, insecure=True)
16+
17+
# load account
18+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
19+
pub_key = priv_key.to_public_key()
20+
address = pub_key.to_address()
21+
subaccount_id = address.get_subaccount_id(index=0)
22+
23+
# prepare trade info
24+
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
26+
27+
orders = [
28+
ProtoMsgComposer.SpotOrder(
29+
market_id=market_id,
30+
subaccount_id=subaccount_id,
31+
fee_recipient=fee_recipient,
32+
price=7.523,
33+
quantity=0.01,
34+
isBuy=True
35+
),
36+
ProtoMsgComposer.SpotOrder(
37+
market_id=market_id,
38+
subaccount_id=subaccount_id,
39+
fee_recipient=fee_recipient,
40+
price=27.92,
41+
quantity=0.01,
42+
isBuy=False
43+
),
44+
]
45+
46+
# prepare tx msg
47+
msg = ProtoMsgComposer.MsgBatchCreateSpotLimitOrders(
48+
sender=address.to_acc_bech32(),
49+
orders=orders
50+
)
51+
52+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
53+
gas_price = 500000000
54+
gas_limit = 200000
55+
fee = [ProtoMsgComposer.Coin(
56+
amount=str(gas_price * gas_limit),
57+
denom=network.fee_denom,
58+
)]
59+
60+
# build tx
61+
tx = (
62+
Transaction()
63+
.with_messages(msg)
64+
.with_sequence(acc_seq)
65+
.with_account_num(acc_num)
66+
.with_chain_id(network.chain_id)
67+
.with_gas(gas_limit)
68+
.with_fee(fee)
69+
.with_memo("")
70+
.with_timeout_height(0)
71+
)
72+
73+
# build signed tx
74+
sign_doc = tx.get_sign_doc(pub_key)
75+
sig = priv_key.sign(sign_doc.SerializeToString())
76+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
77+
78+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
79+
res = client.send_tx_block_mode(tx_raw_bytes)
80+
81+
# print tx response
82+
print(res)
83+
84+
if __name__ == "__main__":
85+
logging.basicConfig(level=logging.INFO)
86+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
14+
# initialize grpc client
15+
client = Client(network.grpc_endpoint, insecure=True)
16+
17+
# load account
18+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
19+
pub_key = priv_key.to_public_key()
20+
address = pub_key.to_address()
21+
subaccount_id = address.get_subaccount_id(index=0)
22+
23+
# prepare trade info
24+
market_id = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30"
25+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
26+
27+
orders = [
28+
ProtoMsgComposer.DerivativeOrder(
29+
market_id=market_id,
30+
subaccount_id=subaccount_id,
31+
fee_recipient=fee_recipient,
32+
price=41027,
33+
quantity=0.01,
34+
leverage=0.7,
35+
isBuy=True
36+
),
37+
ProtoMsgComposer.DerivativeOrder(
38+
market_id=market_id,
39+
subaccount_id=subaccount_id,
40+
fee_recipient=fee_recipient,
41+
price=62140,
42+
quantity=0.01,
43+
leverage=1.4,
44+
isBuy=False
45+
),
46+
]
47+
48+
# prepare tx msg
49+
msg = ProtoMsgComposer.MsgBatchCreateDerivativeLimitOrders(
50+
sender=address.to_acc_bech32(),
51+
orders=orders
52+
)
53+
54+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
55+
gas_price = 500000000
56+
gas_limit = 200000
57+
fee = [ProtoMsgComposer.Coin(
58+
amount=str(gas_price * gas_limit),
59+
denom=network.fee_denom,
60+
)]
61+
62+
# build tx
63+
tx = (
64+
Transaction()
65+
.with_messages(msg)
66+
.with_sequence(acc_seq)
67+
.with_account_num(acc_num)
68+
.with_chain_id(network.chain_id)
69+
.with_gas(gas_limit)
70+
.with_fee(fee)
71+
.with_memo("")
72+
.with_timeout_height(0)
73+
)
74+
75+
# build signed tx
76+
sign_doc = tx.get_sign_doc(pub_key)
77+
sig = priv_key.sign(sign_doc.SerializeToString())
78+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
79+
80+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
81+
res = client.send_tx_block_mode(tx_raw_bytes)
82+
83+
# print tx response
84+
print(res)
85+
86+
if __name__ == "__main__":
87+
logging.basicConfig(level=logging.INFO)
88+
asyncio.get_event_loop().run_until_complete(main())

examples/chain_client_examples/3_ExchangeMsgCreateSpotLimitOrder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async def main() -> None:
2626

2727
# prepare tx msg
2828
msg = ProtoMsgComposer.MsgCreateSpotLimitOrder(
29-
market_id=market_id,
3029
sender=address.to_acc_bech32(),
30+
market_id=market_id,
3131
subaccount_id=subaccount_id,
3232
fee_recipient=fee_recipient,
3333
price=7.523,

examples/chain_client_examples/4_ExchangeMsgCreateSpotMarketOrder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async def main() -> None:
2626

2727
# prepare tx msg
2828
msg = ProtoMsgComposer.MsgCreateSpotMarketOrder(
29-
market_id=market_id,
3029
sender=address.to_acc_bech32(),
30+
market_id=market_id,
3131
subaccount_id=subaccount_id,
3232
fee_recipient=fee_recipient,
3333
price=7.523,

examples/chain_client_examples/5_ExchangeMsgCancelSpotOrder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ async def main() -> None:
2222

2323
# prepare trade info
2424
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25-
subaccount_id = "0xbdaedec95d563fb05240d6e01821008454c24c36000000000000000000000000"
2625
order_hash = "0x098f2c92336bb1ec3591435df1e135052760310bc08fc16e3b9bc409885b863b"
2726

2827
# prepare tx msg

examples/chain_client_examples/6_ExchangeMsgCreateDerivativeLimitOrder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async def main() -> None:
2626

2727
# prepare tx msg
2828
msg = ProtoMsgComposer.MsgCreateDerivativeLimitOrder(
29-
market_id=market_id,
3029
sender=address.to_acc_bech32(),
30+
market_id=market_id,
3131
subaccount_id=subaccount_id,
3232
fee_recipient=fee_recipient,
3333
price=41027,

examples/chain_client_examples/7_ExchangeMsgCreateDerivativeMarketOrder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async def main() -> None:
2626

2727
# prepare tx msg
2828
msg = ProtoMsgComposer.MsgCreateDerivativeLimitOrder(
29-
market_id=market_id,
3029
sender=address.to_acc_bech32(),
30+
market_id=market_id,
3131
subaccount_id=subaccount_id,
3232
fee_recipient=fee_recipient,
3333
price=60000,

examples/chain_client_examples/8_ExchangeMsgCancelDerivativeOrder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ async def main() -> None:
2222

2323
# prepare trade info
2424
market_id = "0xd0f46edfba58827fe692aab7c8d46395d1696239fdf6aeddfa668b73ca82ea30"
25-
subaccount_id = "0xbdaedec95d563fb05240d6e01821008454c24c36000000000000000000000000"
2625
order_hash = "0x5f4672dcca9b96ba2bb72e2ab484f71adf9814e74d12e615f489d0a616cddb8c"
2726

2827
# prepare tx msg
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
14+
# initialize grpc client
15+
client = Client(network.grpc_endpoint, insecure=True)
16+
17+
# load account
18+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
19+
pub_key = priv_key.to_public_key()
20+
address = pub_key.to_address()
21+
subaccount_id = address.get_subaccount_id(index=0)
22+
23+
# prepare trade info
24+
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
25+
orders = [
26+
ProtoMsgComposer.OrderData(
27+
market_id=market_id,
28+
subaccount_id=subaccount_id,
29+
order_hash="0x098f2c92336bb1ec3591435df1e135052760310bc08fc16e3b9bc409885b863b"
30+
),
31+
ProtoMsgComposer.OrderData(
32+
market_id=market_id,
33+
subaccount_id=subaccount_id,
34+
order_hash="0x8d4e780927f91011bf77dea8b625948a14c1ae55d8c5d3f5af3dadbd6bec591d"
35+
)
36+
]
37+
38+
# prepare tx msg
39+
msg = ProtoMsgComposer.MsgBatchCancelSpotOrders(
40+
sender=address.to_acc_bech32(),
41+
data=orders
42+
)
43+
44+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
45+
gas_price = 500000000
46+
gas_limit = 200000
47+
fee = [ProtoMsgComposer.Coin(
48+
amount=str(gas_price * gas_limit),
49+
denom=network.fee_denom,
50+
)]
51+
52+
# build tx
53+
tx = (
54+
Transaction()
55+
.with_messages(msg)
56+
.with_sequence(acc_seq)
57+
.with_account_num(acc_num)
58+
.with_chain_id(network.chain_id)
59+
.with_gas(gas_limit)
60+
.with_fee(fee)
61+
.with_memo("")
62+
.with_timeout_height(0)
63+
)
64+
65+
# build signed tx
66+
sign_doc = tx.get_sign_doc(pub_key)
67+
sig = priv_key.sign(sign_doc.SerializeToString())
68+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
69+
70+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
71+
res = client.send_tx_block_mode(tx_raw_bytes)
72+
73+
# print tx response
74+
print(res)
75+
76+
if __name__ == "__main__":
77+
logging.basicConfig(level=logging.INFO)
78+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)