|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +from bittensor.utils import unlock_key |
| 4 | +from bittensor.utils.btlogging import logging |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from bittensor_wallet import Wallet |
| 8 | + from bittensor.core.async_subtensor import AsyncSubtensor |
| 9 | + |
| 10 | + |
| 11 | +async def start_call_extrinsic( |
| 12 | + subtensor: "AsyncSubtensor", |
| 13 | + wallet: "Wallet", |
| 14 | + netuid: int, |
| 15 | + wait_for_inclusion: bool = True, |
| 16 | + wait_for_finalization: bool = False, |
| 17 | +) -> tuple[bool, str]: |
| 18 | + """ |
| 19 | + Submits a start_call extrinsic to the blockchain, to trigger the start call process for a subnet (used to start a |
| 20 | + new subnet's emission mechanism). |
| 21 | +
|
| 22 | + Args: |
| 23 | + subtensor (Subtensor): The Subtensor client instance used for blockchain interaction. |
| 24 | + wallet (Wallet): The wallet used to sign the extrinsic (must be unlocked). |
| 25 | + netuid (int): The UID of the target subnet for which the call is being initiated. |
| 26 | + wait_for_inclusion (bool, optional): Whether to wait for the extrinsic to be included in a block. Defaults to True. |
| 27 | + wait_for_finalization (bool, optional): Whether to wait for finalization of the extrinsic. Defaults to False. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Tuple[bool, str]: |
| 31 | + - True and a success message if the extrinsic is successfully submitted or processed. |
| 32 | + - False and an error message if the submission fails or the wallet cannot be unlocked. |
| 33 | + """ |
| 34 | + if not (unlock := unlock_key(wallet)).success: |
| 35 | + logging.error(unlock.message) |
| 36 | + return False, unlock.message |
| 37 | + |
| 38 | + async with subtensor.substrate as substrate: |
| 39 | + start_call = await substrate.compose_call( |
| 40 | + call_module="SubtensorModule", |
| 41 | + call_function="start_call", |
| 42 | + call_params={"netuid": netuid}, |
| 43 | + ) |
| 44 | + signed_ext = await substrate.create_signed_extrinsic( |
| 45 | + call=start_call, |
| 46 | + keypair=wallet.coldkey, |
| 47 | + ) |
| 48 | + |
| 49 | + response = await substrate.submit_extrinsic( |
| 50 | + extrinsic=signed_ext, |
| 51 | + wait_for_inclusion=wait_for_inclusion, |
| 52 | + wait_for_finalization=wait_for_finalization, |
| 53 | + ) |
| 54 | + |
| 55 | + if not wait_for_finalization and not wait_for_inclusion: |
| 56 | + return True, "Not waiting for finalization or inclusion." |
| 57 | + |
| 58 | + if await response.is_success: |
| 59 | + return True, "Success with `start_call` response." |
| 60 | + |
| 61 | + return False, await response.error_message |
0 commit comments