11import asyncio
2+ import json
23import os
34import uuid
45from decimal import Decimal
56
67import dotenv
7- from grpc import RpcError
88
99from pyinjective .async_client import AsyncClient
10- from pyinjective .constant import GAS_FEE_BUFFER_AMOUNT
10+ from pyinjective .core . broadcaster import MsgBroadcasterWithPk
1111from pyinjective .core .network import Network
12- from pyinjective .transaction import Transaction
1312from pyinjective .wallet import PrivateKey
1413
1514
@@ -23,7 +22,18 @@ async def main() -> None:
2322 # initialize grpc client
2423 client = AsyncClient (network )
2524 composer = await client .composer ()
26- await client .sync_timeout_height ()
25+
26+ gas_price = await client .current_chain_gas_price ()
27+ # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
28+ gas_price = int (gas_price * 1.1 )
29+
30+ message_broadcaster = MsgBroadcasterWithPk .new_using_simulation (
31+ network = network ,
32+ private_key = configured_private_key ,
33+ gas_price = gas_price ,
34+ client = client ,
35+ composer = composer ,
36+ )
2737
2838 # load account
2939 priv_key = PrivateKey .from_hex (configured_private_key )
@@ -49,54 +59,15 @@ async def main() -> None:
4959 cid = cid ,
5060 )
5161
52- # build sim tx
53- tx = (
54- Transaction ()
55- .with_messages (msg )
56- .with_sequence (client .get_sequence ())
57- .with_account_num (client .get_number ())
58- .with_chain_id (network .chain_id )
59- )
60- sim_sign_doc = tx .get_sign_doc (pub_key )
61- sim_sig = priv_key .sign (sim_sign_doc .SerializeToString ())
62- sim_tx_raw_bytes = tx .get_tx_data (sim_sig , pub_key )
63-
64- # simulate tx
65- try :
66- sim_res = await client .simulate (sim_tx_raw_bytes )
67- except RpcError as ex :
68- print (ex )
69- return
70-
71- sim_res_msg = sim_res ["result" ]["msgResponses" ]
72- print ("---Simulation Response---" )
73- print (sim_res_msg )
62+ # broadcast the transaction
63+ result = await message_broadcaster .broadcast ([msg ])
64+ print ("---Transaction Response---" )
65+ print (json .dumps (result , indent = 2 ))
7466
75- # build tx
7667 gas_price = await client .current_chain_gas_price ()
7768 # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
7869 gas_price = int (gas_price * 1.1 )
79-
80- gas_limit = int (sim_res ["gasInfo" ]["gasUsed" ]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
81- gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
82- fee = [
83- composer .coin (
84- amount = gas_price * gas_limit ,
85- denom = network .fee_denom ,
86- )
87- ]
88- tx = tx .with_gas (gas_limit ).with_fee (fee ).with_memo ("" ).with_timeout_height (client .timeout_height )
89- sign_doc = tx .get_sign_doc (pub_key )
90- sig = priv_key .sign (sign_doc .SerializeToString ())
91- tx_raw_bytes = tx .get_tx_data (sig , pub_key )
92-
93- # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
94- res = await client .broadcast_tx_sync_mode (tx_raw_bytes )
95- print ("---Transaction Response---" )
96- print (res )
97- print ("gas wanted: {}" .format (gas_limit ))
98- print ("gas fee: {} INJ" .format (gas_fee ))
99- print (f"\n \n cid: { cid } " )
70+ message_broadcaster .update_gas_price (gas_price = gas_price )
10071
10172
10273if __name__ == "__main__" :
0 commit comments