Skip to content

Commit 25d55a1

Browse files
authored
Merge pull request #224 from opentensor/chore/thewhaleking/move-metadata-methods
2 parents 3445e5f + e1e37b3 commit 25d55a1

File tree

3 files changed

+254
-262
lines changed

3 files changed

+254
-262
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 22 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
cast,
2424
)
2525

26+
import scalecodec
2627
import websockets.exceptions
2728
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
2829
from scalecodec import GenericVariant
@@ -1588,7 +1589,7 @@ async def retrieve_pending_extrinsics(self) -> list:
15881589

15891590
async def get_metadata_storage_functions(
15901591
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
1591-
) -> list:
1592+
) -> list[dict[str, Any]]:
15921593
"""
15931594
Retrieves a list of all storage functions in metadata active at given block_hash (or chaintip if
15941595
block_hash and runtime are omitted)
@@ -1603,21 +1604,7 @@ async def get_metadata_storage_functions(
16031604
if runtime is None:
16041605
runtime = await self.init_runtime(block_hash=block_hash)
16051606

1606-
storage_list = []
1607-
1608-
for module_idx, module in enumerate(runtime.metadata.pallets):
1609-
if module.storage:
1610-
for storage in module.storage:
1611-
storage_list.append(
1612-
self.serialize_storage_item(
1613-
storage_item=storage,
1614-
module=module,
1615-
spec_version_id=runtime.runtime_version,
1616-
runtime=runtime,
1617-
)
1618-
)
1619-
1620-
return storage_list
1607+
return self._get_metadata_storage_functions(runtime=runtime)
16211608

16221609
async def get_metadata_storage_function(
16231610
self,
@@ -1662,28 +1649,15 @@ async def get_metadata_errors(
16621649
if runtime is None:
16631650
runtime = await self.init_runtime(block_hash=block_hash)
16641651

1665-
error_list = []
1666-
1667-
for module_idx, module in enumerate(runtime.metadata.pallets):
1668-
if module.errors:
1669-
for error in module.errors:
1670-
error_list.append(
1671-
self.serialize_module_error(
1672-
module=module,
1673-
error=error,
1674-
spec_version=runtime.runtime_version,
1675-
)
1676-
)
1677-
1678-
return error_list
1652+
return self._get_metadata_errors(runtime=runtime)
16791653

16801654
async def get_metadata_error(
16811655
self,
16821656
module_name: str,
16831657
error_name: str,
16841658
block_hash: Optional[str] = None,
16851659
runtime: Optional[Runtime] = None,
1686-
):
1660+
) -> Optional[scalecodec.GenericVariant]:
16871661
"""
16881662
Retrieves the details of an error for given module name, call function name and block_hash
16891663
@@ -1699,16 +1673,13 @@ async def get_metadata_error(
16991673
"""
17001674
if runtime is None:
17011675
runtime = await self.init_runtime(block_hash=block_hash)
1702-
1703-
for module_idx, module in enumerate(runtime.metadata.pallets):
1704-
if module.name == module_name and module.errors:
1705-
for error in module.errors:
1706-
if error_name == error.name:
1707-
return error
1676+
return self._get_metadata_error(
1677+
module_name=module_name, error_name=error_name, runtime=runtime
1678+
)
17081679

17091680
async def get_metadata_runtime_call_functions(
17101681
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
1711-
) -> list[GenericRuntimeCallDefinition]:
1682+
) -> list[scalecodec.GenericRuntimeCallDefinition]:
17121683
"""
17131684
Get a list of available runtime API calls
17141685
@@ -1717,25 +1688,15 @@ async def get_metadata_runtime_call_functions(
17171688
"""
17181689
if runtime is None:
17191690
runtime = await self.init_runtime(block_hash=block_hash)
1720-
call_functions = []
1721-
1722-
for api, methods in runtime.runtime_config.type_registry["runtime_api"].items():
1723-
for method in methods["methods"].keys():
1724-
call_functions.append(
1725-
await self.get_metadata_runtime_call_function(
1726-
api, method, runtime=runtime
1727-
)
1728-
)
1729-
1730-
return call_functions
1691+
return self._get_metadata_runtime_call_functions(runtime=runtime)
17311692

17321693
async def get_metadata_runtime_call_function(
17331694
self,
17341695
api: str,
17351696
method: str,
17361697
block_hash: Optional[str] = None,
17371698
runtime: Optional[Runtime] = None,
1738-
) -> GenericRuntimeCallDefinition:
1699+
) -> scalecodec.GenericRuntimeCallDefinition:
17391700
"""
17401701
Get details of a runtime API call. If not supplying `block_hash` or `runtime`, the runtime of the current block
17411702
will be used.
@@ -1751,28 +1712,7 @@ async def get_metadata_runtime_call_function(
17511712
"""
17521713
if runtime is None:
17531714
runtime = await self.init_runtime(block_hash=block_hash)
1754-
1755-
try:
1756-
runtime_call_def = runtime.runtime_config.type_registry["runtime_api"][api][
1757-
"methods"
1758-
][method]
1759-
runtime_call_def["api"] = api
1760-
runtime_call_def["method"] = method
1761-
runtime_api_types = runtime.runtime_config.type_registry["runtime_api"][
1762-
api
1763-
].get("types", {})
1764-
except KeyError:
1765-
raise ValueError(f"Runtime API Call '{api}.{method}' not found in registry")
1766-
1767-
# Add runtime API types to registry
1768-
runtime.runtime_config.update_type_registry_types(runtime_api_types)
1769-
1770-
runtime_call_def_obj = await self.create_scale_object(
1771-
"RuntimeCallDefinition", runtime=runtime
1772-
)
1773-
runtime_call_def_obj.encode(runtime_call_def)
1774-
1775-
return runtime_call_def_obj
1715+
return self._get_metadata_runtime_call_function(api, method, runtime)
17761716

17771717
async def _get_block_handler(
17781718
self,
@@ -3422,24 +3362,15 @@ async def get_metadata_constants(self, block_hash=None) -> list[dict]:
34223362
"""
34233363

34243364
runtime = await self.init_runtime(block_hash=block_hash)
3425-
3426-
constant_list = []
3427-
3428-
for module_idx, module in enumerate(runtime.metadata.pallets):
3429-
for constant in module.constants or []:
3430-
constant_list.append(
3431-
self.serialize_constant(constant, module, runtime.runtime_version)
3432-
)
3433-
3434-
return constant_list
3365+
return self._get_metadata_constants(runtime)
34353366

34363367
async def get_metadata_constant(
34373368
self,
34383369
module_name: str,
34393370
constant_name: str,
34403371
block_hash: Optional[str] = None,
34413372
runtime: Optional[Runtime] = None,
3442-
):
3373+
) -> Optional[scalecodec.ScaleInfoModuleConstantMetadata]:
34433374
"""
34443375
Retrieves the details of a constant for given module name, call function name and block_hash
34453376
(or chaintip if block_hash is omitted)
@@ -3455,12 +3386,7 @@ async def get_metadata_constant(
34553386
"""
34563387
if runtime is None:
34573388
runtime = await self.init_runtime(block_hash=block_hash)
3458-
3459-
for module in runtime.metadata.pallets:
3460-
if module_name == module.name and module.constants:
3461-
for constant in module.constants:
3462-
if constant_name == constant.value["name"]:
3463-
return constant
3389+
return self._get_metadata_constant(module_name, constant_name, runtime)
34643390

34653391
async def get_constant(
34663392
self,
@@ -3604,21 +3530,7 @@ async def get_metadata_modules(self, block_hash=None) -> list[dict[str, Any]]:
36043530
List of metadata modules
36053531
"""
36063532
runtime = await self.init_runtime(block_hash=block_hash)
3607-
3608-
return [
3609-
{
3610-
"metadata_index": idx,
3611-
"module_id": module.get_identifier(),
3612-
"name": module.name,
3613-
"spec_version": runtime.runtime_version,
3614-
"count_call_functions": len(module.calls or []),
3615-
"count_storage_functions": len(module.storage or []),
3616-
"count_events": len(module.events or []),
3617-
"count_constants": len(module.constants or []),
3618-
"count_errors": len(module.errors or []),
3619-
}
3620-
for idx, module in enumerate(runtime.metadata.pallets)
3621-
]
3533+
return self._get_metadata_modules(runtime)
36223534

36233535
async def get_metadata_module(self, name, block_hash=None) -> ScaleType:
36243536
"""
@@ -4081,7 +3993,7 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
40813993

40823994
async def get_metadata_call_functions(
40833995
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
4084-
):
3996+
) -> dict[str, dict[str, dict[str, dict[str, Union[str, int, list]]]]]:
40853997
"""
40863998
Retrieves calls functions for the metadata at the specified block_hash or runtime. If neither are specified,
40873999
the metadata at chaintip is used.
@@ -4133,12 +4045,9 @@ async def get_metadata_call_function(
41334045
"""
41344046
runtime = await self.init_runtime(block_hash=block_hash)
41354047

4136-
for pallet in runtime.metadata.pallets:
4137-
if pallet.name == module_name and pallet.calls:
4138-
for call in pallet.calls:
4139-
if call.name == call_function_name:
4140-
return call
4141-
return None
4048+
return self._get_metadata_call_function(
4049+
module_name, call_function_name, runtime
4050+
)
41424051

41434052
async def get_metadata_events(self, block_hash=None) -> list[dict]:
41444053
"""
@@ -4152,17 +4061,7 @@ async def get_metadata_events(self, block_hash=None) -> list[dict]:
41524061
"""
41534062

41544063
runtime = await self.init_runtime(block_hash=block_hash)
4155-
4156-
event_list = []
4157-
4158-
for event_index, (module, event) in runtime.metadata.event_index.items():
4159-
event_list.append(
4160-
self.serialize_module_event(
4161-
module, event, runtime.runtime_version, event_index
4162-
)
4163-
)
4164-
4165-
return event_list
4064+
return self._get_metadata_events(runtime)
41664065

41674066
async def get_metadata_event(
41684067
self, module_name, event_name, block_hash=None
@@ -4182,12 +4081,7 @@ async def get_metadata_event(
41824081
"""
41834082

41844083
runtime = await self.init_runtime(block_hash=block_hash)
4185-
4186-
for pallet in runtime.metadata.pallets:
4187-
if pallet.name == module_name and pallet.events:
4188-
for event in pallet.events:
4189-
if event.name == event_name:
4190-
return event
4084+
return self._get_metadata_event(module_name, event_name, runtime)
41914085

41924086
async def get_block_number(self, block_hash: Optional[str] = None) -> int:
41934087
"""Async version of `substrateinterface.base.get_block_number` method."""

0 commit comments

Comments
 (0)