@@ -699,7 +699,8 @@ def __init__(
699699 self .runtime_config = RuntimeConfigurationObject (
700700 ss58_format = self .ss58_format , implements_scale_info = True
701701 )
702- self ._metadata_cache = {}
702+ self .__metadata_cache = {}
703+ self ._nonces = {}
703704 self .metadata_version_hex = "0x0f000000" # v15
704705 self .reload_type_registry ()
705706 self ._initializing = False
@@ -2578,7 +2579,9 @@ async def get_account_nonce(self, account_address: str) -> int:
25782579
25792580 async def get_account_next_index (self , account_address : str ) -> int :
25802581 """
2581- Returns next index for the given account address, taking into account the transaction pool.
2582+ This method maintains a cache of nonces for each account ss58address.
2583+ Upon subsequent calls, it will return the cached nonce + 1 instead of fetching from the chain.
2584+ This allows for correct nonce management in-case of async context when gathering co-routines.
25822585
25832586 Args:
25842587 account_address: SS58 formatted address
@@ -2590,8 +2593,13 @@ async def get_account_next_index(self, account_address: str) -> int:
25902593 # Unlikely to happen, this is a common RPC method
25912594 raise Exception ("account_nextIndex not supported" )
25922595
2593- nonce_obj = await self .rpc_request ("account_nextIndex" , [account_address ])
2594- return nonce_obj ["result" ]
2596+ async with self ._lock :
2597+ if self ._nonces .get (account_address ) is None :
2598+ nonce_obj = await self .rpc_request ("account_nextIndex" , [account_address ])
2599+ self ._nonces [account_address ] = nonce_obj ["result" ]
2600+ else :
2601+ self ._nonces [account_address ] += 1
2602+ return self ._nonces [account_address ]
25952603
25962604 async def get_metadata_constant (self , module_name , constant_name , block_hash = None ):
25972605 """
0 commit comments