Skip to content

Commit 98fe876

Browse files
committed
Moved all the metadata retrievals to shared functionality in the superclass
1 parent 3445e5f commit 98fe876

File tree

3 files changed

+241
-254
lines changed

3 files changed

+241
-254
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 18 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,21 +1603,7 @@ async def get_metadata_storage_functions(
16031603
if runtime is None:
16041604
runtime = await self.init_runtime(block_hash=block_hash)
16051605

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
1606+
return self._get_metadata_storage_functions(runtime=runtime)
16211607

16221608
async def get_metadata_storage_function(
16231609
self,
@@ -1662,20 +1648,7 @@ async def get_metadata_errors(
16621648
if runtime is None:
16631649
runtime = await self.init_runtime(block_hash=block_hash)
16641650

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
1651+
return self._get_metadata_errors(runtime=runtime)
16791652

16801653
async def get_metadata_error(
16811654
self,
@@ -1699,16 +1672,13 @@ async def get_metadata_error(
16991672
"""
17001673
if runtime is None:
17011674
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
1675+
return self._get_metadata_error(
1676+
module_name=module_name, error_name=error_name, runtime=runtime
1677+
)
17081678

17091679
async def get_metadata_runtime_call_functions(
17101680
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
1711-
) -> list[GenericRuntimeCallDefinition]:
1681+
) -> list[ScaleType]:
17121682
"""
17131683
Get a list of available runtime API calls
17141684
@@ -1717,25 +1687,15 @@ async def get_metadata_runtime_call_functions(
17171687
"""
17181688
if runtime is None:
17191689
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
1690+
return self._get_metadata_runtime_call_functions(runtime=runtime)
17311691

17321692
async def get_metadata_runtime_call_function(
17331693
self,
17341694
api: str,
17351695
method: str,
17361696
block_hash: Optional[str] = None,
17371697
runtime: Optional[Runtime] = None,
1738-
) -> GenericRuntimeCallDefinition:
1698+
) -> ScaleType:
17391699
"""
17401700
Get details of a runtime API call. If not supplying `block_hash` or `runtime`, the runtime of the current block
17411701
will be used.
@@ -1751,28 +1711,7 @@ async def get_metadata_runtime_call_function(
17511711
"""
17521712
if runtime is None:
17531713
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
1714+
return self._get_metadata_runtime_call_function(api, method, runtime)
17761715

17771716
async def _get_block_handler(
17781717
self,
@@ -3422,16 +3361,7 @@ async def get_metadata_constants(self, block_hash=None) -> list[dict]:
34223361
"""
34233362

34243363
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
3364+
return self._get_metadata_constants(runtime)
34353365

34363366
async def get_metadata_constant(
34373367
self,
@@ -3455,12 +3385,7 @@ async def get_metadata_constant(
34553385
"""
34563386
if runtime is None:
34573387
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
3388+
return self._get_metadata_constant(module_name, constant_name, runtime)
34643389

34653390
async def get_constant(
34663391
self,
@@ -3604,21 +3529,7 @@ async def get_metadata_modules(self, block_hash=None) -> list[dict[str, Any]]:
36043529
List of metadata modules
36053530
"""
36063531
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-
]
3532+
return self._get_metadata_modules(runtime)
36223533

36233534
async def get_metadata_module(self, name, block_hash=None) -> ScaleType:
36243535
"""
@@ -4081,7 +3992,7 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
40813992

40823993
async def get_metadata_call_functions(
40833994
self, block_hash: Optional[str] = None, runtime: Optional[Runtime] = None
4084-
):
3995+
) -> dict[str, dict[str, dict[str, dict[str, Union[str, int, list]]]]]:
40853996
"""
40863997
Retrieves calls functions for the metadata at the specified block_hash or runtime. If neither are specified,
40873998
the metadata at chaintip is used.
@@ -4133,12 +4044,9 @@ async def get_metadata_call_function(
41334044
"""
41344045
runtime = await self.init_runtime(block_hash=block_hash)
41354046

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
4047+
return self._get_metadata_call_function(
4048+
module_name, call_function_name, runtime
4049+
)
41424050

41434051
async def get_metadata_events(self, block_hash=None) -> list[dict]:
41444052
"""
@@ -4152,17 +4060,7 @@ async def get_metadata_events(self, block_hash=None) -> list[dict]:
41524060
"""
41534061

41544062
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
4063+
return self._get_metadata_events(runtime)
41664064

41674065
async def get_metadata_event(
41684066
self, module_name, event_name, block_hash=None
@@ -4182,12 +4080,7 @@ async def get_metadata_event(
41824080
"""
41834081

41844082
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
4083+
return self._get_metadata_event(runtime)
41914084

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

0 commit comments

Comments
 (0)