Skip to content

Commit 7051f7b

Browse files
committed
Adds nonce implementation
1 parent 7099594 commit 7051f7b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

async_substrate_interface/substrate_interface.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ def __init__(
10791079
ss58_format=self.ss58_format, implements_scale_info=True
10801080
)
10811081
self.__metadata_cache = {}
1082+
self._nonces = {}
10821083
self.metadata_version_hex = "0x0f000000" # v15
10831084
self.event_loop = event_loop or asyncio.get_event_loop()
10841085
self.sync_calls = sync_calls
@@ -3166,7 +3167,7 @@ async def create_signed_extrinsic(
31663167

31673168
# Retrieve nonce
31683169
if nonce is None:
3169-
nonce = await self.get_account_nonce(keypair.ss58_address) or 0
3170+
nonce = await self.get_account_next_index(keypair.ss58_address) or 0
31703171

31713172
# Process era
31723173
if era is None:
@@ -3358,8 +3359,13 @@ async def get_account_next_index(self, account_address: str) -> int:
33583359
# Unlikely to happen, this is a common RPC method
33593360
raise Exception("account_nextIndex not supported")
33603361

3361-
nonce_obj = await self.rpc_request("account_nextIndex", [account_address])
3362-
return nonce_obj["result"]
3362+
async with self._lock:
3363+
if self._nonces.get(account_address) is None:
3364+
nonce_obj = await self.rpc_request("account_nextIndex", [account_address])
3365+
self._nonces[account_address] = nonce_obj["result"]
3366+
else:
3367+
self._nonces[account_address] += 1
3368+
return self._nonces[account_address]
33633369

33643370
async def get_metadata_constant(self, module_name, constant_name, block_hash=None):
33653371
"""

0 commit comments

Comments
 (0)