Skip to content

Commit 45b98d2

Browse files
committed
feat: added a new message based fee calculator supporting the Exchange module gas heuristics
1 parent fd60afc commit 45b98d2

84 files changed

Lines changed: 2941 additions & 810 deletions

File tree

Some content is hidden

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

examples/chain_client/1_LocalOrderHash.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dotenv
77

88
from pyinjective.async_client import AsyncClient
9-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
9+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
1010
from pyinjective.core.network import Network
1111
from pyinjective.orderhash import OrderHashManager
1212
from pyinjective.transaction import Transaction
@@ -111,7 +111,11 @@ async def main() -> None:
111111
.with_account_num(client.get_number())
112112
.with_chain_id(network.chain_id)
113113
)
114-
gas_price = GAS_PRICE
114+
115+
gas_price = await client.current_chain_gas_price()
116+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
117+
gas_price = int(gas_price * 1.1)
118+
115119
base_gas = 85000
116120
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
117121
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
@@ -148,7 +152,11 @@ async def main() -> None:
148152
.with_account_num(client.get_number())
149153
.with_chain_id(network.chain_id)
150154
)
151-
gas_price = GAS_PRICE
155+
156+
gas_price = await client.current_chain_gas_price()
157+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
158+
gas_price = int(gas_price * 1.1)
159+
152160
base_gas = 85000
153161
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
154162
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
@@ -240,7 +248,11 @@ async def main() -> None:
240248
.with_account_num(client.get_number())
241249
.with_chain_id(network.chain_id)
242250
)
243-
gas_price = GAS_PRICE
251+
252+
gas_price = await client.current_chain_gas_price()
253+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
254+
gas_price = int(gas_price * 1.1)
255+
244256
base_gas = 85000
245257
gas_limit = base_gas + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
246258
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")

examples/chain_client/3_MessageBroadcaster.py

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

67
import dotenv
78

8-
from pyinjective.composer import Composer as ProtoMsgComposer
9+
from pyinjective.async_client import AsyncClient
910
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1011
from pyinjective.core.network import Network
1112
from pyinjective.wallet import PrivateKey
@@ -17,11 +18,20 @@ async def main() -> None:
1718

1819
# select network: local, testnet, mainnet
1920
network = Network.testnet()
20-
composer = ProtoMsgComposer(network=network.string())
21+
22+
client = AsyncClient(network)
23+
composer = await client.composer()
24+
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)
2128

2229
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
2330
network=network,
2431
private_key=private_key_in_hexa,
32+
gas_price=gas_price,
33+
client=client,
34+
composer=composer,
2535
)
2636

2737
priv_key = PrivateKey.from_hex(private_key_in_hexa)
@@ -64,7 +74,12 @@ async def main() -> None:
6474
# broadcast the transaction
6575
result = await message_broadcaster.broadcast([msg])
6676
print("---Transaction Response---")
67-
print(result)
77+
print(json.dumps(result, indent=2))
78+
79+
gas_price = await client.current_chain_gas_price()
80+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
81+
gas_price = int(gas_price * 1.1)
82+
message_broadcaster.update_gas_price(gas_price=gas_price)
6883

6984

7085
if __name__ == "__main__":

examples/chain_client/4_MessageBroadcasterWithGranteeAccount.py

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

67
import dotenv
78

89
from pyinjective.async_client import AsyncClient
9-
from pyinjective.composer import Composer as ProtoMsgComposer
1010
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1111
from pyinjective.core.network import Network
1212
from pyinjective.wallet import Address, PrivateKey
@@ -19,20 +19,27 @@ 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)
25+
composer = await client.composer()
2626
await client.sync_timeout_height()
2727

2828
# load account
2929
priv_key = PrivateKey.from_hex(private_key_in_hexa)
3030
pub_key = priv_key.to_public_key()
3131
address = pub_key.to_address()
3232

33+
gas_price = await client.current_chain_gas_price()
34+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
35+
gas_price = int(gas_price * 1.1)
36+
3337
message_broadcaster = MsgBroadcasterWithPk.new_for_grantee_account_using_simulation(
3438
network=network,
3539
grantee_private_key=private_key_in_hexa,
40+
gas_price=gas_price,
41+
client=client,
42+
composer=composer,
3643
)
3744

3845
# prepare tx msg
@@ -55,7 +62,12 @@ async def main() -> None:
5562
# broadcast the transaction
5663
result = await message_broadcaster.broadcast([msg])
5764
print("---Transaction Response---")
58-
print(result)
65+
print(json.dumps(result, indent=2))
66+
67+
gas_price = await client.current_chain_gas_price()
68+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
69+
gas_price = int(gas_price * 1.1)
70+
message_broadcaster.update_gas_price(gas_price=gas_price)
5971

6072

6173
if __name__ == "__main__":

examples/chain_client/5_MessageBroadcasterWithoutSimulation.py

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

67
import dotenv
78

8-
from pyinjective.composer import Composer as ProtoMsgComposer
9+
from pyinjective.async_client import AsyncClient
910
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1011
from pyinjective.core.network import Network
1112
from pyinjective.wallet import PrivateKey
@@ -17,11 +18,21 @@ async def main() -> None:
1718

1819
# select network: local, testnet, mainnet
1920
network = Network.testnet()
20-
composer = ProtoMsgComposer(network=network.string())
2121

22-
message_broadcaster = MsgBroadcasterWithPk.new_without_simulation(
22+
client = AsyncClient(network)
23+
composer = await client.composer()
24+
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(
2331
network=network,
2432
private_key=private_key_in_hexa,
33+
gas_price=gas_price,
34+
client=client,
35+
composer=composer,
2536
)
2637

2738
priv_key = PrivateKey.from_hex(private_key_in_hexa)
@@ -64,7 +75,12 @@ async def main() -> None:
6475
# broadcast the transaction
6576
result = await message_broadcaster.broadcast([msg])
6677
print("---Transaction Response---")
67-
print(result)
78+
print(json.dumps(result, indent=2))
79+
80+
gas_price = await client.current_chain_gas_price()
81+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
82+
gas_price = int(gas_price * 1.1)
83+
message_broadcaster.update_gas_price(gas_price=gas_price)
6884

6985

7086
if __name__ == "__main__":

examples/chain_client/6_MessageBroadcasterWithGranteeAccountWithoutSimulation.py

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

67
import dotenv
78

89
from pyinjective.async_client import AsyncClient
9-
from pyinjective.composer import Composer as ProtoMsgComposer
1010
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1111
from pyinjective.core.network import Network
1212
from 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

6071
if __name__ == "__main__":

examples/chain_client/auction/1_MsgBid.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from grpc import RpcError
66

77
from pyinjective.async_client import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
8+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
99
from pyinjective.core.network import Network
1010
from pyinjective.transaction import Transaction
1111
from pyinjective.wallet import PrivateKey
@@ -52,7 +52,10 @@ async def main() -> None:
5252
return
5353

5454
# build tx
55-
gas_price = GAS_PRICE
55+
gas_price = await client.current_chain_gas_price()
56+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
57+
gas_price = int(gas_price * 1.1)
58+
5659
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
5760
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
5861
fee = [

examples/chain_client/authz/1_MsgGrant.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from grpc import RpcError
66

77
from pyinjective.async_client import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
8+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
99
from pyinjective.core.network import Network
1010
from pyinjective.transaction import Transaction
1111
from pyinjective.wallet import PrivateKey
@@ -72,7 +72,10 @@ async def main() -> None:
7272
return
7373

7474
# build tx
75-
gas_price = GAS_PRICE
75+
gas_price = await client.current_chain_gas_price()
76+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
77+
gas_price = int(gas_price * 1.1)
78+
7679
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
7780
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
7881
fee = [

examples/chain_client/authz/2_MsgExec.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from grpc import RpcError
88

99
from pyinjective.async_client import AsyncClient
10-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
10+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
1111
from pyinjective.core.network import Network
1212
from pyinjective.transaction import Transaction
1313
from pyinjective.wallet import Address, PrivateKey
@@ -79,7 +79,10 @@ async def main() -> None:
7979
print(unpacked_msg_res)
8080

8181
# build tx
82-
gas_price = GAS_PRICE
82+
gas_price = await client.current_chain_gas_price()
83+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
84+
gas_price = int(gas_price * 1.1)
85+
8386
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
8487
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
8588
fee = [

examples/chain_client/authz/3_MsgRevoke.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from grpc import RpcError
66

77
from pyinjective.async_client import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
8+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
99
from pyinjective.core.network import Network
1010
from pyinjective.transaction import Transaction
1111
from pyinjective.wallet import PrivateKey
@@ -57,7 +57,10 @@ async def main() -> None:
5757
return
5858

5959
# build tx
60-
gas_price = GAS_PRICE
60+
gas_price = await client.current_chain_gas_price()
61+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
62+
gas_price = int(gas_price * 1.1)
63+
6164
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
6265
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
6366
fee = [

examples/chain_client/bank/1_MsgSend.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from grpc import RpcError
66

77
from pyinjective.async_client import AsyncClient
8-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
8+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
99
from pyinjective.core.network import Network
1010
from pyinjective.transaction import Transaction
1111
from pyinjective.wallet import PrivateKey
@@ -57,7 +57,10 @@ async def main() -> None:
5757
return
5858

5959
# build tx
60-
gas_price = GAS_PRICE
60+
gas_price = await client.current_chain_gas_price()
61+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
62+
gas_price = int(gas_price * 1.1)
63+
6164
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
6265
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
6366
fee = [

0 commit comments

Comments
 (0)