Skip to content

Commit 9c7dd85

Browse files
Merge pull request #69 from InjectiveLabs/update-examples
Add examples
2 parents d98382d + 7fbc852 commit 9c7dd85

File tree

4 files changed

+158
-2
lines changed

4 files changed

+158
-2
lines changed

examples/async/chain_client/20_MsgExec.py

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

2525
# prepare tx msg
2626
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
27+
2728
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
28-
2929
granter_inj_address = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
3030
granter_address = Address.from_acc_bech32(granter_inj_address)
3131
granter_subaccount_id = granter_address.get_subaccount_id(index=0)
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+
import requests
4+
5+
from pyinjective.composer import Composer as ProtoMsgComposer
6+
from pyinjective.async_client import AsyncClient
7+
from pyinjective.transaction import Transaction
8+
from pyinjective.constant import Network
9+
from pyinjective.wallet import PrivateKey, PublicKey, Address
10+
11+
async def main() -> None:
12+
# select network: local, testnet, mainnet
13+
network = Network.testnet()
14+
composer = ProtoMsgComposer(network=network.string())
15+
16+
# initialize grpc client
17+
client = AsyncClient(network, insecure=False)
18+
19+
# load account
20+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
21+
pub_key = priv_key.to_public_key()
22+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
23+
24+
# prepare msg
25+
asset = "injective-protocol"
26+
coingecko_endpoint = f"https://api.coingecko.com/api/v3/simple/price?ids={asset}&vs_currencies=usd"
27+
token_price = requests.get(coingecko_endpoint).json()[asset]["usd"]
28+
minimum_bridge_fee_usd = 10
29+
bridge_fee = minimum_bridge_fee_usd / token_price
30+
31+
# prepare tx msg
32+
msg = composer.MsgSendToEth(
33+
sender=address.to_acc_bech32(),
34+
denom="INJ",
35+
eth_dest="0xaf79152ac5df276d9a8e1e2e22822f9713474902",
36+
amount=23,
37+
bridge_fee=bridge_fee
38+
)
39+
40+
# build sim tx
41+
tx = (
42+
Transaction()
43+
.with_messages(msg)
44+
.with_sequence(address.get_sequence())
45+
.with_account_num(address.get_number())
46+
.with_chain_id(network.chain_id)
47+
)
48+
sim_sign_doc = tx.get_sign_doc(pub_key)
49+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
50+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
51+
52+
# simulate tx
53+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
54+
if not success:
55+
print(sim_res)
56+
return
57+
58+
# build tx
59+
gas_price = 500000000
60+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
61+
fee = [composer.Coin(
62+
amount=gas_price * gas_limit,
63+
denom=network.fee_denom,
64+
)]
65+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
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 = await 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())

examples/sync/chain_client/20_MsgExec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ async def main() -> None:
2323

2424
# prepare tx msg
2525
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
26-
2726
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
2827
granter_inj_address = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
2928
granter_address = Address.from_acc_bech32(granter_inj_address)
@@ -38,6 +37,7 @@ async def main() -> None:
3837
is_buy=True
3938
)
4039

40+
4141
msg = composer.MsgExec(
4242
grantee=grantee,
4343
msgs=[msg0]
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+
import requests
4+
5+
from pyinjective.composer import Composer as ProtoMsgComposer
6+
from pyinjective.client import Client
7+
from pyinjective.transaction import Transaction
8+
from pyinjective.constant import Network
9+
from pyinjective.wallet import PrivateKey, PublicKey, Address
10+
11+
async def main() -> None:
12+
# select network: local, testnet, mainnet
13+
network = Network.testnet()
14+
composer = ProtoMsgComposer(network=network.string())
15+
16+
# initialize grpc client
17+
client = Client(network, insecure=False)
18+
19+
# load account
20+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
21+
pub_key = priv_key.to_public_key()
22+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
23+
24+
# prepare msg
25+
asset = "injective-protocol"
26+
coingecko_endpoint = f"https://api.coingecko.com/api/v3/simple/price?ids={asset}&vs_currencies=usd"
27+
token_price = requests.get(coingecko_endpoint).json()[asset]["usd"]
28+
minimum_bridge_fee_usd = 10
29+
bridge_fee = minimum_bridge_fee_usd / token_price
30+
31+
# prepare tx msg
32+
msg = composer.MsgSendToEth(
33+
sender=address.to_acc_bech32(),
34+
denom="INJ",
35+
eth_dest="0xaf79152ac5df276d9a8e1e2e22822f9713474902",
36+
amount=23,
37+
bridge_fee=bridge_fee
38+
)
39+
40+
# build sim tx
41+
tx = (
42+
Transaction()
43+
.with_messages(msg)
44+
.with_sequence(address.get_sequence())
45+
.with_account_num(address.get_number())
46+
.with_chain_id(network.chain_id)
47+
)
48+
sim_sign_doc = tx.get_sign_doc(pub_key)
49+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
50+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
51+
52+
# simulate tx
53+
(sim_res, success) = client.simulate_tx(sim_tx_raw_bytes)
54+
if not success:
55+
print(sim_res)
56+
return
57+
58+
# build tx
59+
gas_price = 500000000
60+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
61+
fee = [composer.Coin(
62+
amount=gas_price * gas_limit,
63+
denom=network.fee_denom,
64+
)]
65+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
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)