Skip to content

Commit 54d7394

Browse files
committed
feat: add MsgRegisterAsDMM example
1 parent b6197b2 commit 54d7394

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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=False)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
39+
# prepare tx msg
40+
msg = composer.MsgRegisterAsDMM(
41+
sender=address.to_acc_bech32(),
42+
dmm_account=address.to_acc_bech32()
43+
)
44+
45+
# build sim tx
46+
tx = (
47+
Transaction()
48+
.with_messages(msg)
49+
.with_sequence(address.get_sequence())
50+
.with_account_num(address.get_number())
51+
.with_chain_id(network.chain_id)
52+
)
53+
sim_sign_doc = tx.get_sign_doc(pub_key)
54+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
55+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
56+
57+
# simulate tx
58+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
59+
if not success:
60+
print(sim_res)
61+
return
62+
63+
# build tx
64+
gas_price = 500000000
65+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
66+
fee = [composer.Coin(
67+
amount=gas_price * gas_limit,
68+
denom=network.fee_denom,
69+
)]
70+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
71+
sign_doc = tx.get_sign_doc(pub_key)
72+
sig = priv_key.sign(sign_doc.SerializeToString())
73+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
74+
75+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
76+
res = await client.send_tx_block_mode(tx_raw_bytes)
77+
print("tx response")
78+
print(res)
79+
80+
if __name__ == "__main__":
81+
logging.basicConfig(level=logging.INFO)
82+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.client import Client
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 = Client(network, insecure=False)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
36+
pub_key = priv_key.to_public_key()
37+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
38+
39+
# prepare tx msg
40+
msg = composer.MsgRegisterAsDMM(
41+
sender=address.to_acc_bech32(),
42+
dmm_account=address.to_acc_bech32()
43+
)
44+
45+
# build sim tx
46+
tx = (
47+
Transaction()
48+
.with_messages(msg)
49+
.with_sequence(address.get_sequence())
50+
.with_account_num(address.get_number())
51+
.with_chain_id(network.chain_id)
52+
)
53+
sim_sign_doc = tx.get_sign_doc(pub_key)
54+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
55+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
56+
57+
# simulate tx
58+
(sim_res, success) = client.simulate_tx(sim_tx_raw_bytes)
59+
if not success:
60+
print(sim_res)
61+
return
62+
63+
# build tx
64+
gas_price = 500000000
65+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
66+
fee = [composer.Coin(
67+
amount=gas_price * gas_limit,
68+
denom=network.fee_denom,
69+
)]
70+
current_height = client.get_latest_block().block.header.height
71+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(current_height+50)
72+
sign_doc = tx.get_sign_doc(pub_key)
73+
sig = priv_key.sign(sign_doc.SerializeToString())
74+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
75+
76+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
77+
res = client.send_tx_block_mode(tx_raw_bytes)
78+
print("tx response")
79+
print(res)
80+
81+
if __name__ == "__main__":
82+
logging.basicConfig(level=logging.INFO)
83+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)