diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index d55a22fb15..2827ee51cc 100644 --- a/bittensor/core/async_subtensor.py +++ b/bittensor/core/async_subtensor.py @@ -1395,8 +1395,25 @@ async def get_all_commitments( A mapping of the ss58:commitment with the commitment as a string. Example: + Retrieve and process commitment data for all neurons in a subnet:: - # TODO add example of how to handle realistic commitment data + from bittensor import AsyncSubtensor + + async with AsyncSubtensor(network="finney") as subtensor: + netuid = 1 # Target subnet + + # Get all commitments for the subnet + commitments = await subtensor.get_all_commitments(netuid=netuid) + + # Process the commitment data + for hotkey_ss58, commitment_data in commitments.items(): + if commitment_data: + print(f"Hotkey: {hotkey_ss58[:16]}...") + print(f"Commitment: {commitment_data}") + + # Check how many neurons have active commitments + active_commitments = {k: v for k, v in commitments.items() if v} + print(f"Active commitments: {len(active_commitments)}/{len(commitments)}") """ query = await self.query_map( module="Commitments", @@ -1955,10 +1972,24 @@ async def get_commitment( reuse_block: Whether to reuse the last-used block hash. Do not set if using `block_hash` or `block`. Returns: - The commitment data as a string. + The commitment data as a string, or empty string if no commitment exists or decoding fails. + + Example: + Retrieve commitment data for a specific neuron:: + + from bittensor import AsyncSubtensor + async with AsyncSubtensor(network="finney") as subtensor: + netuid = 1 # Target subnet + uid = 0 # Neuron UID - # TODO: add a real example of how to handle realistic commitment data, or chop example + # Get the commitment for this neuron + commitment = await subtensor.get_commitment(netuid=netuid, uid=uid) + + if commitment: + print(f"Neuron {uid} has commitment: {commitment}") + else: + print(f"Neuron {uid} has no active commitment") Notes: - @@ -1992,7 +2023,6 @@ async def get_commitment_metadata( block_hash: Optional[str] = None, reuse_block: bool = False, ) -> Union[str, dict]: - # TODO: how to handle return data? need good example @roman """Fetches raw commitment metadata from specific subnet for given hotkey. Parameters: @@ -2006,6 +2036,25 @@ async def get_commitment_metadata( The raw commitment metadata. Returns a dict when commitment data exists, or an empty string when no commitment is found for the given hotkey on the subnet. + Example: + Fetch and handle raw commitment metadata:: + + from bittensor import AsyncSubtensor + + async with AsyncSubtensor(network="finney") as subtensor: + netuid = 1 + hotkey_ss58 = "5D..." # Replace with actual hotkey + + metadata = await subtensor.get_commitment_metadata(netuid, hotkey_ss58) + + # Handle the return type + if isinstance(metadata, dict) and metadata: + # Commitment exists - metadata contains raw blockchain data + print(f"Raw metadata: {metadata}") + else: + # No commitment found for this hotkey + print("No commitment found") + Notes: - """ diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 996401ea92..93be69a634 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1101,8 +1101,25 @@ def get_all_commitments( A mapping of the ss58:commitment with the commitment as a string. Example: + Retrieve and process commitment data for all neurons in a subnet:: - # TODO add example of how to handle realistic commitment data + from bittensor import Subtensor + + subtensor = Subtensor(network="finney") + netuid = 1 # Target subnet + + # Get all commitments for the subnet + commitments = subtensor.get_all_commitments(netuid=netuid) + + # Process the commitment data + for hotkey_ss58, commitment_data in commitments.items(): + if commitment_data: + print(f"Hotkey: {hotkey_ss58[:16]}...") + print(f"Commitment: {commitment_data}") + + # Check how many neurons have active commitments + active_commitments = {k: v for k, v in commitments.items() if v} + print(f"Active commitments: {len(active_commitments)}/{len(commitments)}") """ query = self.query_map( module="Commitments", @@ -1567,10 +1584,24 @@ def get_commitment(self, netuid: int, uid: int, block: Optional[int] = None) -> block: The block number to query. If `None`, queries the current chain head. Returns: - The commitment data as a string. + The commitment data as a string, or empty string if no commitment exists or decoding fails. + + Example: + Retrieve commitment data for a specific neuron:: + + from bittensor import Subtensor + subtensor = Subtensor(network="finney") + netuid = 1 # Target subnet + uid = 0 # Neuron UID - # TODO: add a real example of how to handle realistic commitment data, or chop example + # Get the commitment for this neuron + commitment = subtensor.get_commitment(netuid=netuid, uid=uid) + + if commitment: + print(f"Neuron {uid} has commitment: {commitment}") + else: + print(f"Neuron {uid} has no active commitment") Notes: - @@ -1594,7 +1625,6 @@ def get_commitment(self, netuid: int, uid: int, block: Optional[int] = None) -> def get_commitment_metadata( self, netuid: int, hotkey_ss58: str, block: Optional[int] = None ) -> Union[str, dict]: - # TODO: how to handle return data? need good example @roman """Fetches raw commitment metadata from specific subnet for given hotkey. Parameters: @@ -1606,6 +1636,25 @@ def get_commitment_metadata( The raw commitment metadata. Returns a dict when commitment data exists, or an empty string when no commitment is found for the given hotkey on the subnet. + Example: + Fetch and handle raw commitment metadata:: + + from bittensor import Subtensor + + subtensor = Subtensor(network="finney") + netuid = 1 + hotkey_ss58 = "5D..." # Replace with actual hotkey + + metadata = subtensor.get_commitment_metadata(netuid, hotkey_ss58) + + # Handle the return type + if isinstance(metadata, dict) and metadata: + # Commitment exists - metadata contains raw blockchain data + print(f"Raw metadata: {metadata}") + else: + # No commitment found for this hotkey + print("No commitment found") + Notes: - """