Skip to content

Release/1.2.0 #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
86f899e
Support async key implementations
immortalizzy Apr 10, 2025
d926cc7
Merge branch 'main' into main
immortalizzy Apr 10, 2025
d6aa47d
[WIP] moving retry to async-substrate-interface
thewhaleking Apr 25, 2025
743f708
moved around some stuff
thewhaleking Apr 25, 2025
5e3d6f5
moved around some stuff
thewhaleking Apr 25, 2025
36e6e68
Update async_substrate_interface/protocols.py
immortalizzy Apr 26, 2025
d181207
Update async_substrate_interface/protocols.py
immortalizzy Apr 26, 2025
8fdbc19
Update async_substrate_interface/protocols.py
immortalizzy Apr 26, 2025
f3b721c
Annotate return value to allow async function
immortalizzy Apr 26, 2025
be2e1ed
Ruff
thewhaleking Apr 29, 2025
045b22b
subscript_storage added to asyncsubstrate
thewhaleking Apr 29, 2025
58e850f
subscript_storage added to sync
thewhaleking Apr 29, 2025
19223ac
Metadata methods
thewhaleking Apr 29, 2025
d2c1ed9
Other methods added.
thewhaleking Apr 29, 2025
b771d7f
Merge pull request #104 from opentensor/feat/thewhaleking/add-missing…
thewhaleking May 2, 2025
c3f6006
Max connections semaphore object added.
thewhaleking May 2, 2025
8a04ec0
Exposes `_get_block_handler` publicly in both async and sync substrat…
thewhaleking May 2, 2025
7ffbff9
Merge pull request #107 from opentensor/feat/thewhaleking/max-connect…
thewhaleking May 2, 2025
ffde983
Merge pull request #108 from opentensor/feat/thewhaleking/expose-get_…
thewhaleking May 2, 2025
b544fb9
Merge remote-tracking branch 'origin/staging' into feat/thewhaleking/…
thewhaleking May 5, 2025
d80c1e6
New direction
thewhaleking May 5, 2025
a64def5
Add properties as well.
thewhaleking May 5, 2025
d0cf9a3
Fixes #109
thewhaleking May 6, 2025
9953486
Merge pull request #110 from opentensor/feat/thewhaleking/safe-del
thewhaleking May 6, 2025
7cf1164
Merge remote-tracking branch 'origin/staging' into tensorshield/main
thewhaleking May 6, 2025
f3d82ae
Make protocol runtime checkable, ruff
thewhaleking May 6, 2025
b4e85d8
Merge pull request #111 from opentensor/tensorshield/main
thewhaleking May 6, 2025
d13d282
Merge remote-tracking branch 'origin/staging' into feat/thewhaleking/…
thewhaleking May 6, 2025
951d558
Update.
thewhaleking May 6, 2025
91bfe7a
Sync substrate retry working.
thewhaleking May 6, 2025
3f50aaf
Async also working.
thewhaleking May 6, 2025
a83f57e
Add MetadataAtVersionNotFound exception.
thewhaleking May 6, 2025
fab6fc9
Merge pull request #113 from opentensor/feat/thewhaleking/add-metadat…
thewhaleking May 6, 2025
767e122
[WIP] tests
thewhaleking May 6, 2025
d3f6473
improved test a bit
thewhaleking May 6, 2025
1baab0f
Add `chain_endpoint` and `url` prior to super init.
thewhaleking May 7, 2025
c9a13ff
More tests.
thewhaleking May 7, 2025
c009d95
Merge pull request #100 from opentensor/feat/thewhaleking/fallback-ch…
thewhaleking May 7, 2025
18f165a
Bumps version and changelog
ibraheem-abe May 7, 2025
a6a2c27
Merge pull request #115 from opentensor/changelog/120
ibraheem-abe May 7, 2025
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
Prev Previous commit
Next Next commit
Metadata methods
  • Loading branch information
thewhaleking committed Apr 29, 2025
commit 19223acffb46cecb83d3d9958e28473f0823ac7c
122 changes: 122 additions & 0 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,41 @@ async def get_metadata_runtime_call_function(

return runtime_call_def_obj

async def get_metadata_runtime_call_function(
self, api: str, method: str
) -> GenericRuntimeCallDefinition:
"""
Get details of a runtime API call

Args:
api: Name of the runtime API e.g. 'TransactionPaymentApi'
method: Name of the method e.g. 'query_fee_details'

Returns:
GenericRuntimeCallDefinition
"""
await self.init_runtime(block_hash=block_hash)

try:
runtime_call_def = self.runtime_config.type_registry["runtime_api"][api][
"methods"
][method]
runtime_call_def["api"] = api
runtime_call_def["method"] = method
runtime_api_types = self.runtime_config.type_registry["runtime_api"][
api
].get("types", {})
except KeyError:
raise ValueError(f"Runtime API Call '{api}.{method}' not found in registry")

# Add runtime API types to registry
self.runtime_config.update_type_registry_types(runtime_api_types)

runtime_call_def_obj = await self.create_scale_object("RuntimeCallDefinition")
runtime_call_def_obj.encode(runtime_call_def)

return runtime_call_def_obj

async def _get_block_handler(
self,
block_hash: str,
Expand Down Expand Up @@ -1767,6 +1802,21 @@ def convert_event_data(data):
events.append(convert_event_data(item))
return events

async def get_metadata(self, block_hash=None):
"""
Returns `MetadataVersioned` object for given block_hash or chaintip if block_hash is omitted


Args:
block_hash

Returns:
MetadataVersioned
"""
runtime = await self.init_runtime(block_hash=block_hash)

return runtime.metadata

@a.lru_cache(maxsize=512)
async def get_parent_block_hash(self, block_hash):
return await self._get_parent_block_hash(block_hash)
Expand Down Expand Up @@ -2725,6 +2775,29 @@ async def get_account_next_index(self, account_address: str) -> int:
self._nonces[account_address] += 1
return self._nonces[account_address]

async def get_metadata_constants(self, block_hash=None) -> list[dict]:
"""
Retrieves a list of all constants in metadata active at given block_hash (or chaintip if block_hash is omitted)

Args:
block_hash: hash of the block

Returns:
list of constants
"""

runtime = await self.init_runtime(block_hash=block_hash)

constant_list = []

for module_idx, module in enumerate(self.metadata.pallets):
for constant in module.constants or []:
constant_list.append(
self.serialize_constant(constant, module, runtime.runtime_version)
)

return constant_list

async def get_metadata_constant(self, module_name, constant_name, block_hash=None):
"""
Retrieves the details of a constant for given module name, call function name and block_hash
Expand Down Expand Up @@ -3234,6 +3307,55 @@ async def get_metadata_call_function(
return call
return None

async def get_metadata_events(self, block_hash=None) -> list[dict]:
"""
Retrieves a list of all events in metadata active for given block_hash (or chaintip if block_hash is omitted)

Args:
block_hash

Returns:
list of module events
"""

runtime = await self.init_runtime(block_hash=block_hash)

event_list = []

for event_index, (module, event) in self.metadata.event_index.items():
event_list.append(
self.serialize_module_event(
module, event, runtime.runtime_version, event_index
)
)

return event_list

async def get_metadata_event(
self, module_name, event_name, block_hash=None
) -> Optional[Any]:
"""
Retrieves the details of an event for given module name, call function name and block_hash
(or chaintip if block_hash is omitted)

Args:
module_name: name of the module to call
event_name: name of the event
block_hash: hash of the block

Returns:
Metadata event

"""

runtime = await self.init_runtime(block_hash=block_hash)

for pallet in runtime.metadata.pallets:
if pallet.name == module_name and pallet.events:
for event in pallet.events:
if event.name == event_name:
return event

async def get_block_number(self, block_hash: Optional[str] = None) -> int:
"""Async version of `substrateinterface.base.get_block_number` method."""
response = await self.rpc_request("chain_getHeader", [block_hash])
Expand Down
124 changes: 123 additions & 1 deletion async_substrate_interface/sync_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def subscription_handler(storage_key, obj, subscription_id):

Args:
storage_keys: StorageKey list of storage keys to subscribe to
subscription_handler: coroutine function to handle value changes of subscription
subscription_handler: function to handle value changes of subscription

"""
self.init_runtime()
Expand Down Expand Up @@ -1040,6 +1040,41 @@ def get_metadata_runtime_call_function(

return runtime_call_def_obj

def get_metadata_runtime_call_function(
self, api: str, method: str
) -> GenericRuntimeCallDefinition:
"""
Get details of a runtime API call

Args:
api: Name of the runtime API e.g. 'TransactionPaymentApi'
method: Name of the method e.g. 'query_fee_details'

Returns:
GenericRuntimeCallDefinition
"""
self.init_runtime()

try:
runtime_call_def = self.runtime_config.type_registry["runtime_api"][api][
"methods"
][method]
runtime_call_def["api"] = api
runtime_call_def["method"] = method
runtime_api_types = self.runtime_config.type_registry["runtime_api"][
api
].get("types", {})
except KeyError:
raise ValueError(f"Runtime API Call '{api}.{method}' not found in registry")

# Add runtime API types to registry
self.runtime_config.update_type_registry_types(runtime_api_types)

runtime_call_def_obj = self.create_scale_object("RuntimeCallDefinition")
runtime_call_def_obj.encode(runtime_call_def)

return runtime_call_def_obj

def _get_block_handler(
self,
block_hash: str,
Expand Down Expand Up @@ -1510,6 +1545,21 @@ def convert_event_data(data):
events.append(convert_event_data(item))
return events

def get_metadata(self, block_hash=None):
"""
Returns `MetadataVersioned` object for given block_hash or chaintip if block_hash is omitted


Args:
block_hash

Returns:
MetadataVersioned
"""
runtime = self.init_runtime(block_hash=block_hash)

return runtime.metadata

@functools.lru_cache(maxsize=512)
def get_parent_block_hash(self, block_hash):
block_header = self.rpc_request("chain_getHeader", [block_hash])
Expand Down Expand Up @@ -2428,6 +2478,29 @@ def get_account_next_index(self, account_address: str) -> int:
nonce_obj = self.rpc_request("account_nextIndex", [account_address])
return nonce_obj["result"]

def get_metadata_constants(self, block_hash=None) -> list[dict]:
"""
Retrieves a list of all constants in metadata active at given block_hash (or chaintip if block_hash is omitted)

Args:
block_hash: hash of the block

Returns:
list of constants
"""

runtime = self.init_runtime(block_hash=block_hash)

constant_list = []

for module_idx, module in enumerate(self.metadata.pallets):
for constant in module.constants or []:
constant_list.append(
self.serialize_constant(constant, module, runtime.runtime_version)
)

return constant_list

def get_metadata_constant(self, module_name, constant_name, block_hash=None):
"""
Retrieves the details of a constant for given module name, call function name and block_hash
Expand Down Expand Up @@ -2926,6 +2999,55 @@ def get_metadata_call_function(
return call
return None

def get_metadata_events(self, block_hash=None) -> list[dict]:
"""
Retrieves a list of all events in metadata active for given block_hash (or chaintip if block_hash is omitted)

Args:
block_hash

Returns:
list of module events
"""

runtime = self.init_runtime(block_hash=block_hash)

event_list = []

for event_index, (module, event) in self.metadata.event_index.items():
event_list.append(
self.serialize_module_event(
module, event, runtime.runtime_version, event_index
)
)

return event_list

def get_metadata_event(
self, module_name, event_name, block_hash=None
) -> Optional[Any]:
"""
Retrieves the details of an event for given module name, call function name and block_hash
(or chaintip if block_hash is omitted)

Args:
module_name: name of the module to call
event_name: name of the event
block_hash: hash of the block

Returns:
Metadata event

"""

runtime = self.init_runtime(block_hash=block_hash)

for pallet in runtime.metadata.pallets:
if pallet.name == module_name and pallet.events:
for event in pallet.events:
if event.name == event_name:
return event

def get_block_number(self, block_hash: Optional[str] = None) -> int:
"""Async version of `substrateinterface.base.get_block_number` method."""
response = self.rpc_request("chain_getHeader", [block_hash])
Expand Down
Loading