Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ async def _start_receiving(self):
except ConnectionClosed:
await self.connect(force=True)

async def send(self, payload: dict) -> int:
async def send(self, payload: dict) -> str:
"""
Sends a payload to the websocket connection.

Expand All @@ -714,6 +714,7 @@ async def send(self, payload: dict) -> int:
return original_id
except (ConnectionClosed, ssl.SSLError, EOFError):
await self.connect(force=True)
return await self.send(payload)

async def retrieve(self, item_id: int) -> Optional[dict]:
"""
Expand Down Expand Up @@ -911,7 +912,7 @@ async def name(self):
return self._name

async def get_storage_item(
self, module: str, storage_function: str, block_hash: str = None
self, module: str, storage_function: str, block_hash: Optional[str] = None
):
runtime = await self.init_runtime(block_hash=block_hash)
metadata_pallet = runtime.metadata.get_metadata_pallet(module)
Expand Down Expand Up @@ -1014,7 +1015,7 @@ async def decode_scale(
# Decode AccountId bytes to SS58 address
return ss58_encode(scale_bytes, self.ss58_format)
else:
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
if runtime.metadata_v15 is not None and force_legacy is False:
obj = decode_by_type_string(type_string, runtime.registry, scale_bytes)
Expand Down Expand Up @@ -1154,7 +1155,7 @@ async def create_storage_key(
pallet: str,
storage_function: str,
params: Optional[list] = None,
block_hash: str = None,
block_hash: Optional[str] = None,
) -> StorageKey:
"""
Create a `StorageKey` instance providing storage function details. See `subscribe_storage()`.
Expand All @@ -1169,7 +1170,7 @@ async def create_storage_key(
StorageKey
"""
runtime = await self.init_runtime(block_hash=block_hash)

params = params or []
return StorageKey.create_from_storage_function(
pallet,
storage_function,
Expand Down Expand Up @@ -1317,7 +1318,7 @@ async def get_metadata_storage_functions(
Returns:
list of storage functions
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

storage_list = []
Expand Down Expand Up @@ -1355,7 +1356,7 @@ async def get_metadata_storage_function(
Returns:
Metadata storage function
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

pallet = runtime.metadata.get_metadata_pallet(module_name)
Expand All @@ -1376,7 +1377,7 @@ async def get_metadata_errors(
Returns:
list of errors in the metadata
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

error_list = []
Expand Down Expand Up @@ -1414,7 +1415,7 @@ async def get_metadata_error(
error

"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

for module_idx, module in enumerate(runtime.metadata.pallets):
Expand All @@ -1424,15 +1425,15 @@ async def get_metadata_error(
return error

async def get_metadata_runtime_call_functions(
self, block_hash: str = None, runtime: Optional[Runtime] = None
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
) -> list[GenericRuntimeCallDefinition]:
"""
Get a list of available runtime API calls

Returns:
list of runtime call functions
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
call_functions = []

Expand Down Expand Up @@ -1466,7 +1467,7 @@ async def get_metadata_runtime_call_function(
Returns:
GenericRuntimeCallDefinition
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

try:
Expand Down Expand Up @@ -1763,7 +1764,7 @@ async def get_block_header(
ignore_decoding_errors: bool = False,
include_author: bool = False,
finalized_only: bool = False,
) -> dict:
) -> Optional[dict]:
"""
Retrieves a block header and decodes its containing log digest items. If `block_hash` and `block_number`
is omitted the chain tip will be retrieved, or the finalized head if `finalized_only` is set to true.
Expand All @@ -1790,7 +1791,7 @@ async def get_block_header(
block_hash = await self.get_block_hash(block_number)

if block_hash is None:
return
return None

if block_hash and finalized_only:
raise ValueError(
Expand Down Expand Up @@ -1820,7 +1821,7 @@ async def get_block_header(

async def subscribe_block_headers(
self,
subscription_handler: callable,
subscription_handler: Callable,
ignore_decoding_errors: bool = False,
include_author: bool = False,
finalized_only=False,
Expand Down Expand Up @@ -1902,7 +1903,7 @@ def retrieve_extrinsic_by_hash(
)

async def get_extrinsics(
self, block_hash: str = None, block_number: int = None
self, block_hash: Optional[str] = None, block_number: Optional[int] = None
) -> Optional[list["AsyncExtrinsicReceipt"]]:
"""
Return all extrinsics for given block_hash or block_number
Expand Down Expand Up @@ -2141,7 +2142,7 @@ async def _preprocess(
"""
params = query_for if query_for else []
# Search storage call in metadata
if not runtime:
if runtime is None:
runtime = self.runtime
metadata_pallet = runtime.metadata.get_metadata_pallet(module)

Expand Down Expand Up @@ -2503,7 +2504,7 @@ async def query_multiple(
block_hash = await self._get_current_block_hash(block_hash, reuse_block_hash)
if block_hash:
self.last_block_hash = block_hash
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
preprocessed: tuple[Preprocessed] = await asyncio.gather(
*[
Expand Down Expand Up @@ -2561,7 +2562,7 @@ async def query_multi(
Returns:
list of `(storage_key, scale_obj)` tuples
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

# Retrieve corresponding value
Expand Down Expand Up @@ -2616,7 +2617,7 @@ async def create_scale_object(
Returns:
The created Scale Type object
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
if "metadata" not in kwargs:
kwargs["metadata"] = runtime.metadata
Expand Down Expand Up @@ -2780,7 +2781,7 @@ async def create_signed_extrinsic(
self,
call: GenericCall,
keypair: Keypair,
era: Optional[dict] = None,
era: Optional[Union[dict, str]] = None,
nonce: Optional[int] = None,
tip: int = 0,
tip_asset_id: Optional[int] = None,
Expand Down Expand Up @@ -2932,12 +2933,12 @@ async def _do_runtime_call_old(
params: Optional[Union[list, dict]] = None,
block_hash: Optional[str] = None,
runtime: Optional[Runtime] = None,
) -> ScaleType:
) -> ScaleObj:
logger.debug(
f"Decoding old runtime call: {api}.{method} with params: {params} at block hash: {block_hash}"
)
runtime_call_def = _TYPE_REGISTRY["runtime_api"][api]["methods"][method]

params = params or []
# Encode params
param_data = b""

Expand Down Expand Up @@ -3159,7 +3160,7 @@ async def get_metadata_constant(
Returns:
MetadataModuleConstants
"""
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)

for module in runtime.metadata.pallets:
Expand Down Expand Up @@ -3245,7 +3246,7 @@ async def get_payment_info(
return result.value

async def get_type_registry(
self, block_hash: str = None, max_recursion: int = 4
self, block_hash: Optional[str] = None, max_recursion: int = 4
) -> dict:
"""
Generates an exhaustive list of which RUST types exist in the runtime specified at given block_hash (or
Expand Down Expand Up @@ -3284,7 +3285,7 @@ async def get_type_registry(
return type_registry

async def get_type_definition(
self, type_string: str, block_hash: str = None
self, type_string: str, block_hash: Optional[str] = None
) -> str:
"""
Retrieves SCALE encoding specifications of given type_string
Expand Down Expand Up @@ -3360,7 +3361,7 @@ async def query(
block_hash = await self._get_current_block_hash(block_hash, reuse_block_hash)
if block_hash:
self.last_block_hash = block_hash
if not runtime:
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
preprocessed: Preprocessed = await self._preprocess(
params,
Expand Down Expand Up @@ -3589,11 +3590,11 @@ async def create_multisig_extrinsic(
keypair: Keypair,
multisig_account: MultiAccountId,
max_weight: Optional[Union[dict, int]] = None,
era: dict = None,
nonce: int = None,
era: Optional[dict] = None,
nonce: Optional[int] = None,
tip: int = 0,
tip_asset_id: int = None,
signature: Union[bytes, str] = None,
tip_asset_id: Optional[int] = None,
signature: Optional[Union[bytes, str]] = None,
) -> GenericExtrinsic:
"""
Create a Multisig extrinsic that will be signed by one of the signatories. Checks on-chain if the threshold
Expand Down Expand Up @@ -3878,6 +3879,9 @@ async def get_block_number(self, block_hash: Optional[str] = None) -> int:
elif "result" in response:
if response["result"]:
return int(response["result"]["number"], 16)
raise SubstrateRequestException(
f"Unable to retrieve block number for {block_hash}"
)

async def close(self):
"""
Expand Down Expand Up @@ -3973,14 +3977,14 @@ async def get_async_substrate_interface(
"""
substrate = AsyncSubstrateInterface(
url,
use_remote_preset,
auto_discover,
ss58_format,
type_registry,
chain_name,
max_retries,
retry_timeout,
_mock,
use_remote_preset=use_remote_preset,
auto_discover=auto_discover,
ss58_format=ss58_format,
type_registry=type_registry,
chain_name=chain_name,
max_retries=max_retries,
retry_timeout=retry_timeout,
_mock=_mock,
)
await substrate.initialize()
return substrate
Loading
Loading