Skip to content

Commit afacb99

Browse files
authored
Merge pull request #383 from InjectiveLabs/chore/fix_example_scripts
[chore] fix example scripts
2 parents fbe418c + 7fa372d commit afacb99

File tree

79 files changed

+908
-1601
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+908
-1601
lines changed

examples/chain_client/1_LocalOrderHash.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23
import os
34
import uuid
45
from decimal import Decimal
@@ -132,7 +133,7 @@ async def main() -> None:
132133

133134
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
134135
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
135-
print(res)
136+
print(json.dumps(res, indent=2))
136137
print("gas wanted: {}".format(gas_limit))
137138
print("gas fee: {} INJ".format(gas_fee))
138139

@@ -173,7 +174,7 @@ async def main() -> None:
173174

174175
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
175176
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
176-
print(res)
177+
print(json.dumps(res, indent=2))
177178
print("gas wanted: {}".format(gas_limit))
178179
print("gas fee: {} INJ".format(gas_fee))
179180

@@ -269,7 +270,7 @@ async def main() -> None:
269270

270271
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
271272
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
272-
print(res)
273+
print(json.dumps(res, indent=2))
273274
print("gas wanted: {}".format(gas_limit))
274275
print("gas fee: {} INJ".format(gas_fee))
275276

examples/chain_client/5_MessageBroadcasterWithoutSimulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def main() -> None:
2727
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
2828
gas_price = int(gas_price * 1.1)
2929

30-
message_broadcaster = MsgBroadcasterWithPk.new_without_simulation(
30+
message_broadcaster = MsgBroadcasterWithPk.new_using_gas_heuristics(
3131
network=network,
3232
private_key=private_key_in_hexa,
3333
gas_price=gas_price,

examples/chain_client/auction/1_MsgBid.py

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,54 @@
11
import asyncio
2+
import json
23
import os
34

45
import dotenv
5-
from grpc import RpcError
66

77
from pyinjective.async_client_v2 import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
99
from pyinjective.core.network import Network
10-
from pyinjective.transaction import Transaction
1110
from pyinjective.wallet import PrivateKey
1211

1312

1413
async def main() -> None:
1514
dotenv.load_dotenv()
16-
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
15+
private_key_in_hexa = os.getenv("INJECTIVE_PRIVATE_KEY")
1716

1817
# select network: local, testnet, mainnet
1918
network = Network.testnet()
2019

2120
# initialize grpc client
2221
client = AsyncClient(network)
2322
composer = await client.composer()
24-
await client.sync_timeout_height()
2523

26-
# load account
27-
priv_key = PrivateKey.from_hex(configured_private_key)
24+
gas_price = await client.current_chain_gas_price()
25+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
26+
gas_price = int(gas_price * 1.1)
27+
28+
message_broadcaster = MsgBroadcasterWithPk.new_using_gas_heuristics(
29+
network=network,
30+
private_key=private_key_in_hexa,
31+
gas_price=gas_price,
32+
client=client,
33+
composer=composer,
34+
)
35+
36+
priv_key = PrivateKey.from_hex(private_key_in_hexa)
2837
pub_key = priv_key.to_public_key()
2938
address = pub_key.to_address()
30-
await client.fetch_account(address.to_acc_bech32())
3139

3240
# prepare tx msg
3341
msg = composer.msg_bid(sender=address.to_acc_bech32(), round=16250, bid_amount=1)
3442

35-
# build sim tx
36-
tx = (
37-
Transaction()
38-
.with_messages(msg)
39-
.with_sequence(client.get_sequence())
40-
.with_account_num(client.get_number())
41-
.with_chain_id(network.chain_id)
42-
)
43-
sim_sign_doc = tx.get_sign_doc(pub_key)
44-
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
45-
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
46-
47-
# simulate tx
48-
try:
49-
sim_res = await client.simulate(sim_tx_raw_bytes)
50-
except RpcError as ex:
51-
print(ex)
52-
return
43+
# broadcast the transaction
44+
result = await message_broadcaster.broadcast([msg])
45+
print("---Transaction Response---")
46+
print(json.dumps(result, indent=2))
5347

54-
# build tx
5548
gas_price = await client.current_chain_gas_price()
5649
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
5750
gas_price = int(gas_price * 1.1)
58-
59-
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
60-
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
61-
fee = [
62-
composer.coin(
63-
amount=gas_price * gas_limit,
64-
denom=network.fee_denom,
65-
)
66-
]
67-
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
68-
sign_doc = tx.get_sign_doc(pub_key)
69-
sig = priv_key.sign(sign_doc.SerializeToString())
70-
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
71-
72-
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
73-
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
74-
print(res)
75-
print("gas wanted: {}".format(gas_limit))
76-
print("gas fee: {} INJ".format(gas_fee))
51+
message_broadcaster.update_gas_price(gas_price=gas_price)
7752

7853

7954
if __name__ == "__main__":

examples/chain_client/auth/query/1_Account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23

34
from pyinjective.async_client_v2 import AsyncClient
45
from pyinjective.core.network import Network
@@ -9,7 +10,7 @@ async def main() -> None:
910
client = AsyncClient(network)
1011
address = "inj1knhahceyp57j5x7xh69p7utegnnnfgxavmahjr"
1112
acc = await client.fetch_account(address=address)
12-
print(acc)
13+
print(json.dumps(acc, indent=2))
1314

1415

1516
if __name__ == "__main__":

examples/chain_client/authz/1_MsgGrant.py

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import asyncio
2+
import json
23
import os
34

45
import dotenv
5-
from grpc import RpcError
66

77
from pyinjective.async_client_v2 import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
99
from pyinjective.core.network import Network
10-
from pyinjective.transaction import Transaction
1110
from pyinjective.wallet import PrivateKey
1211

1312

1413
async def main() -> None:
1514
dotenv.load_dotenv()
16-
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
15+
private_key_in_hexa = os.getenv("INJECTIVE_PRIVATE_KEY")
1716
grantee_public_address = os.getenv("INJECTIVE_GRANTEE_PUBLIC_ADDRESS")
1817

1918
# select network: local, testnet, mainnet
@@ -22,13 +21,22 @@ async def main() -> None:
2221
# initialize grpc client
2322
client = AsyncClient(network)
2423
composer = await client.composer()
25-
await client.sync_timeout_height()
2624

27-
# load account
28-
priv_key = PrivateKey.from_hex(configured_private_key)
25+
gas_price = await client.current_chain_gas_price()
26+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
27+
gas_price = int(gas_price * 1.1)
28+
29+
message_broadcaster = MsgBroadcasterWithPk.new_using_gas_heuristics(
30+
network=network,
31+
private_key=private_key_in_hexa,
32+
gas_price=gas_price,
33+
client=client,
34+
composer=composer,
35+
)
36+
37+
priv_key = PrivateKey.from_hex(private_key_in_hexa)
2938
pub_key = priv_key.to_public_key()
3039
address = pub_key.to_address()
31-
await client.fetch_account(address.to_acc_bech32())
3240
# subaccount_id = address.get_subaccount_id(index=0)
3341
# market_ids = ["0x0511ddc4e6586f3bfe1acb2dd905f8b8a82c97e1edaef654b12ca7e6031ca0fa"]
3442

@@ -52,48 +60,15 @@ async def main() -> None:
5260
# market_ids=market_ids
5361
# )
5462

55-
# build sim tx
56-
tx = (
57-
Transaction()
58-
.with_messages(msg)
59-
.with_sequence(client.get_sequence())
60-
.with_account_num(client.get_number())
61-
.with_chain_id(network.chain_id)
62-
)
63-
sim_sign_doc = tx.get_sign_doc(pub_key)
64-
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
65-
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
66-
67-
# simulate tx
68-
try:
69-
sim_res = await client.simulate(sim_tx_raw_bytes)
70-
except RpcError as ex:
71-
print(ex)
72-
return
73-
74-
# build tx
63+
# broadcast the transaction
64+
result = await message_broadcaster.broadcast([msg])
65+
print("---Transaction Response---")
66+
print(json.dumps(result, indent=2))
67+
7568
gas_price = await client.current_chain_gas_price()
7669
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
7770
gas_price = int(gas_price * 1.1)
78-
79-
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
80-
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
81-
fee = [
82-
composer.coin(
83-
amount=gas_price * gas_limit,
84-
denom=network.fee_denom,
85-
)
86-
]
87-
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
88-
sign_doc = tx.get_sign_doc(pub_key)
89-
sig = priv_key.sign(sign_doc.SerializeToString())
90-
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
91-
92-
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
93-
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
94-
print(res)
95-
print("gas wanted: {}".format(gas_limit))
96-
print("gas fee: {} INJ".format(gas_fee))
71+
message_broadcaster.update_gas_price(gas_price=gas_price)
9772

9873

9974
if __name__ == "__main__":

examples/chain_client/authz/2_MsgExec.py

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import asyncio
2+
import json
23
import os
34
import uuid
45
from decimal import Decimal
56

67
import dotenv
7-
from grpc import RpcError
88

99
from pyinjective.async_client_v2 import AsyncClient
10-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
10+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1111
from pyinjective.core.network import Network
12-
from pyinjective.transaction import Transaction
1312
from pyinjective.wallet import Address, PrivateKey
1413

1514

1615
async 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

10671
if __name__ == "__main__":

0 commit comments

Comments
 (0)