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_v2 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 Address , PrivateKey
1413
1514
1615async def main () -> None :
1716 dotenv .load_dotenv ()
18- configured_private_key = os .getenv ("INJECTIVE_GRANTEE_PRIVATE_KEY" )
17+ private_key_in_hexa = os .getenv ("INJECTIVE_GRANTEE_PRIVATE_KEY" )
1918 granter_inj_address = os .getenv ("INJECTIVE_GRANTER_PUBLIC_ADDRESS" )
2019
2120 # select network: local, testnet, mainnet
@@ -24,21 +23,30 @@ async def main() -> None:
2423 # initialize grpc client
2524 client = AsyncClient (network )
2625 composer = await client .composer ()
27- await client .sync_timeout_height ()
2826
29- # load account
30- priv_key = PrivateKey .from_hex (configured_private_key )
27+ gas_price = await client .current_chain_gas_price ()
28+ # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
29+ gas_price = int (gas_price * 1.1 )
30+
31+ message_broadcaster = MsgBroadcasterWithPk .new_for_grantee_account_using_gas_heuristics (
32+ network = network ,
33+ grantee_private_key = private_key_in_hexa ,
34+ gas_price = gas_price ,
35+ client = client ,
36+ composer = composer ,
37+ )
38+
39+ priv_key = PrivateKey .from_hex (private_key_in_hexa )
3140 pub_key = priv_key .to_public_key ()
3241 address = pub_key .to_address ()
33- await client .fetch_account (address .to_acc_bech32 ())
3442
3543 # prepare tx msg
3644 market_id = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
3745 grantee = address .to_acc_bech32 ()
3846
3947 granter_address = Address .from_acc_bech32 (granter_inj_address )
4048 granter_subaccount_id = granter_address .get_subaccount_id (index = 0 )
41- msg0 = composer .msg_create_spot_limit_order (
49+ message = composer .msg_create_spot_limit_order (
4250 sender = granter_inj_address ,
4351 market_id = market_id ,
4452 subaccount_id = granter_subaccount_id ,
@@ -49,58 +57,15 @@ async def main() -> None:
4957 cid = str (uuid .uuid4 ()),
5058 )
5159
52- msg = composer .msg_exec (grantee = grantee , msgs = [msg0 ])
60+ # broadcast the transaction
61+ result = await message_broadcaster .broadcast ([message ])
62+ print ("---Transaction Response---" )
63+ print (json .dumps (result , indent = 2 ))
5364
54- # build sim tx
55- tx = (
56- Transaction ()
57- .with_messages (msg )
58- .with_sequence (client .get_sequence ())
59- .with_account_num (client .get_number ())
60- .with_chain_id (network .chain_id )
61- )
62- sim_sign_doc = tx .get_sign_doc (pub_key )
63- sim_sig = priv_key .sign (sim_sign_doc .SerializeToString ())
64- sim_tx_raw_bytes = tx .get_tx_data (sim_sig , pub_key )
65-
66- # simulate tx
67- try :
68- sim_res = await client .simulate (sim_tx_raw_bytes )
69- except RpcError as ex :
70- print (ex )
71- return
72-
73- sim_res_msgs = sim_res ["result" ]["msgResponses" ]
74- data = sim_res_msgs [0 ]
75- unpacked_msg_res = composer .unpack_msg_exec_response (
76- underlying_msg_type = msg0 .__class__ .__name__ , msg_exec_response = data
77- )
78- print ("simulation msg response" )
79- print (unpacked_msg_res )
80-
81- # build tx
8265 gas_price = await client .current_chain_gas_price ()
8366 # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
8467 gas_price = int (gas_price * 1.1 )
85-
86- gas_limit = int (sim_res ["gasInfo" ]["gasUsed" ]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
87- gas_fee = "{:.18f}" .format ((gas_price * gas_limit ) / pow (10 , 18 )).rstrip ("0" )
88- fee = [
89- composer .coin (
90- amount = gas_price * gas_limit ,
91- denom = network .fee_denom ,
92- )
93- ]
94- tx = tx .with_gas (gas_limit ).with_fee (fee ).with_memo ("" ).with_timeout_height (client .timeout_height )
95- sign_doc = tx .get_sign_doc (pub_key )
96- sig = priv_key .sign (sign_doc .SerializeToString ())
97- tx_raw_bytes = tx .get_tx_data (sig , pub_key )
98-
99- # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
100- res = await client .broadcast_tx_sync_mode (tx_raw_bytes )
101- print (res )
102- print ("gas wanted: {}" .format (gas_limit ))
103- print ("gas fee: {} INJ" .format (gas_fee ))
68+ message_broadcaster .update_gas_price (gas_price = gas_price )
10469
10570
10671if __name__ == "__main__" :
0 commit comments