Skip to content

Commit 7de7299

Browse files
authored
Merge pull request #226 from opentensor/feat/thewhaleking/update-get-payment-info
Update get_payment_info to include addl params
2 parents 25d55a1 + 16ef730 commit 7de7299

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,13 @@ async def get_constant(
34263426
return None
34273427

34283428
async def get_payment_info(
3429-
self, call: GenericCall, keypair: Keypair
3429+
self,
3430+
call: GenericCall,
3431+
keypair: Keypair,
3432+
era: Optional[Union[dict, str]] = None,
3433+
nonce: Optional[int] = None,
3434+
tip: int = 0,
3435+
tip_asset_id: Optional[int] = None,
34303436
) -> dict[str, Any]:
34313437
"""
34323438
Retrieves fee estimation via RPC for given extrinsic
@@ -3435,6 +3441,11 @@ async def get_payment_info(
34353441
call: Call object to estimate fees for
34363442
keypair: Keypair of the sender, does not have to include private key because no valid signature is
34373443
required
3444+
era: Specify mortality in blocks in follow format:
3445+
{'period': [amount_blocks]} If omitted the extrinsic is immortal
3446+
nonce: nonce to include in extrinsics, if omitted the current nonce is retrieved on-chain
3447+
tip: The tip for the block author to gain priority during network congestion
3448+
tip_asset_id: Optional asset ID with which to pay the tip
34383449
34393450
Returns:
34403451
Dict with payment info
@@ -3454,7 +3465,13 @@ async def get_payment_info(
34543465

34553466
# Create extrinsic
34563467
extrinsic = await self.create_signed_extrinsic(
3457-
call=call, keypair=keypair, signature=signature
3468+
call=call,
3469+
keypair=keypair,
3470+
era=era,
3471+
nonce=nonce,
3472+
tip=tip,
3473+
tip_asset_id=tip_asset_id,
3474+
signature=signature,
34583475
)
34593476
extrinsic_len = len(extrinsic.data)
34603477

async_substrate_interface/sync_substrate.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,14 +2715,27 @@ def get_constant(
27152715
else:
27162716
return None
27172717

2718-
def get_payment_info(self, call: GenericCall, keypair: Keypair) -> dict[str, Any]:
2718+
def get_payment_info(
2719+
self,
2720+
call: GenericCall,
2721+
keypair: Keypair,
2722+
era: Optional[Union[dict, str]] = None,
2723+
nonce: Optional[int] = None,
2724+
tip: int = 0,
2725+
tip_asset_id: Optional[int] = None,
2726+
) -> dict[str, Any]:
27192727
"""
27202728
Retrieves fee estimation via RPC for given extrinsic
27212729
27222730
Args:
27232731
call: Call object to estimate fees for
27242732
keypair: Keypair of the sender, does not have to include private key because no valid signature is
27252733
required
2734+
era: Specify mortality in blocks in follow format:
2735+
{'period': [amount_blocks]} If omitted the extrinsic is immortal
2736+
nonce: nonce to include in extrinsics, if omitted the current nonce is retrieved on-chain
2737+
tip: The tip for the block author to gain priority during network congestion
2738+
tip_asset_id: Optional asset ID with which to pay the tip
27262739
27272740
Returns:
27282741
Dict with payment info
@@ -2742,7 +2755,13 @@ def get_payment_info(self, call: GenericCall, keypair: Keypair) -> dict[str, Any
27422755

27432756
# Create extrinsic
27442757
extrinsic = self.create_signed_extrinsic(
2745-
call=call, keypair=keypair, signature=signature
2758+
call=call,
2759+
keypair=keypair,
2760+
era=era,
2761+
nonce=nonce,
2762+
tip=tip,
2763+
tip_asset_id=tip_asset_id,
2764+
signature=signature,
27462765
)
27472766
extrinsic_len = len(extrinsic.data)
27482767

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ dev = [
5656
"pytest-split==0.10.0",
5757
"pytest-xdist==3.6.1",
5858
"pytest-rerunfailures==10.2",
59-
"substrate-interface"
59+
"substrate-interface",
60+
"bittensor-wallet>=4.0.0"
6061
]

tests/integration_tests/test_async_substrate_interface.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import threading
66

7+
import bittensor_wallet
78
import pytest
89
from scalecodec import ss58_encode
910

@@ -235,3 +236,42 @@ async def test_improved_reconnection():
235236
shutdown_thread.start()
236237
shutdown_thread.join(timeout=5)
237238
server_thread.join(timeout=5)
239+
240+
241+
@pytest.mark.asyncio
242+
async def test_get_payment_info():
243+
alice_coldkey = bittensor_wallet.Keypair.create_from_uri("//Alice")
244+
bob_coldkey = bittensor_wallet.Keypair.create_from_uri("//Bob")
245+
async with AsyncSubstrateInterface(
246+
LATENT_LITE_ENTRYPOINT, ss58_format=42, chain_name="Bittensor"
247+
) as substrate:
248+
block_hash = await substrate.get_chain_head()
249+
call = await substrate.compose_call(
250+
"Balances",
251+
"transfer_keep_alive",
252+
{"dest": bob_coldkey.ss58_address, "value": 100_000},
253+
block_hash,
254+
)
255+
payment_info = await substrate.get_payment_info(
256+
call=call,
257+
keypair=alice_coldkey,
258+
)
259+
partial_fee_no_era = payment_info["partial_fee"]
260+
assert partial_fee_no_era > 0
261+
payment_info_era = await substrate.get_payment_info(
262+
call=call, keypair=alice_coldkey, era={"period": 64}
263+
)
264+
partial_fee_era = payment_info_era["partial_fee"]
265+
assert partial_fee_era > partial_fee_no_era
266+
267+
payment_info_all_options = await substrate.get_payment_info(
268+
call=call,
269+
keypair=alice_coldkey,
270+
era={"period": 64},
271+
nonce=await substrate.get_account_nonce(alice_coldkey.ss58_address),
272+
tip=5_000_000,
273+
tip_asset_id=64,
274+
)
275+
partial_fee_all_options = payment_info_all_options["partial_fee"]
276+
assert partial_fee_all_options > partial_fee_no_era
277+
assert partial_fee_all_options > partial_fee_era

tests/integration_tests/test_substrate_interface.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import bittensor_wallet
12
from scalecodec import ss58_encode
23

34
from async_substrate_interface.sync_substrate import SubstrateInterface
@@ -112,3 +113,41 @@ def test_query_map_with_odd_number_of_params():
112113
first_record = qm.records[0]
113114
assert len(first_record) == 2
114115
assert len(first_record[0]) == 4
116+
117+
118+
def test_get_payment_info():
119+
alice_coldkey = bittensor_wallet.Keypair.create_from_uri("//Alice")
120+
bob_coldkey = bittensor_wallet.Keypair.create_from_uri("//Bob")
121+
with SubstrateInterface(
122+
LATENT_LITE_ENTRYPOINT, ss58_format=42, chain_name="Bittensor"
123+
) as substrate:
124+
block_hash = substrate.get_chain_head()
125+
call = substrate.compose_call(
126+
"Balances",
127+
"transfer_keep_alive",
128+
{"dest": bob_coldkey.ss58_address, "value": 100_000},
129+
block_hash,
130+
)
131+
payment_info = substrate.get_payment_info(
132+
call=call,
133+
keypair=alice_coldkey,
134+
)
135+
partial_fee_no_era = payment_info["partial_fee"]
136+
assert partial_fee_no_era > 0
137+
payment_info_era = substrate.get_payment_info(
138+
call=call, keypair=alice_coldkey, era={"period": 64}
139+
)
140+
partial_fee_era = payment_info_era["partial_fee"]
141+
assert partial_fee_era > partial_fee_no_era
142+
143+
payment_info_all_options = substrate.get_payment_info(
144+
call=call,
145+
keypair=alice_coldkey,
146+
era={"period": 64},
147+
nonce=substrate.get_account_nonce(alice_coldkey.ss58_address),
148+
tip=5_000_000,
149+
tip_asset_id=64,
150+
)
151+
partial_fee_all_options = payment_info_all_options["partial_fee"]
152+
assert partial_fee_all_options > partial_fee_no_era
153+
assert partial_fee_all_options > partial_fee_era

0 commit comments

Comments
 (0)