11import asyncio
2+ import json
23import os
34import uuid
45from decimal import Decimal
56
67import dotenv
78
89from pyinjective .async_client import AsyncClient
9- from pyinjective .composer import Composer as ProtoMsgComposer
1010from pyinjective .core .broadcaster import MsgBroadcasterWithPk
1111from pyinjective .core .network import Network
1212from pyinjective .wallet import Address , PrivateKey
@@ -19,20 +19,26 @@ async def main() -> None:
1919
2020 # select network: local, testnet, mainnet
2121 network = Network .testnet ()
22- composer = ProtoMsgComposer (network = network .string ())
2322
2423 # initialize grpc client
2524 client = AsyncClient (network )
26- await client .sync_timeout_height ()
25+ composer = await client .composer ()
2726
2827 # load account
2928 priv_key = PrivateKey .from_hex (private_key_in_hexa )
3029 pub_key = priv_key .to_public_key ()
3130 address = pub_key .to_address ()
3231
33- message_broadcaster = MsgBroadcasterWithPk .new_for_grantee_account_without_simulation (
32+ gas_price = await client .current_chain_gas_price ()
33+ # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
34+ gas_price = int (gas_price * 1.1 )
35+
36+ message_broadcaster = MsgBroadcasterWithPk .new_using_simulation (
3437 network = network ,
35- grantee_private_key = private_key_in_hexa ,
38+ private_key = private_key_in_hexa ,
39+ gas_price = gas_price ,
40+ client = client ,
41+ composer = composer ,
3642 )
3743
3844 # prepare tx msg
@@ -54,7 +60,12 @@ async def main() -> None:
5460 # broadcast the transaction
5561 result = await message_broadcaster .broadcast ([msg ])
5662 print ("---Transaction Response---" )
57- print (result )
63+ print (json .dumps (result , indent = 2 ))
64+
65+ gas_price = await client .current_chain_gas_price ()
66+ # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
67+ gas_price = int (gas_price * 1.1 )
68+ message_broadcaster .update_gas_price (gas_price = gas_price )
5869
5970
6071if __name__ == "__main__" :
0 commit comments