Skip to content

Commit 5c9961f

Browse files
author
Roman
committed
add extrinsics
1 parent a10be9e commit 5c9961f

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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, indicating that the caller wishes to begin responding
20+
to a specific challenge on the specified subnet (`netuid`).
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
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.subtensor import Subtensor
9+
10+
11+
def start_call_extrinsic(
12+
subtensor: "Subtensor",
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, indicating that the caller wishes to begin responding
20+
to a specific challenge on the specified subnet (`netuid`).
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+
with subtensor.substrate as substrate:
39+
start_call = substrate.compose_call(
40+
call_module="SubtensorModule",
41+
call_function="start_call",
42+
call_params={"netuid": netuid},
43+
)
44+
45+
signed_ext = substrate.create_signed_extrinsic(
46+
call=start_call,
47+
keypair=wallet.coldkey,
48+
)
49+
50+
response = substrate.submit_extrinsic(
51+
extrinsic=signed_ext,
52+
wait_for_inclusion=wait_for_inclusion,
53+
wait_for_finalization=wait_for_finalization,
54+
)
55+
56+
if not wait_for_finalization and not wait_for_inclusion:
57+
return True, "Not waiting for finalization or inclusion."
58+
59+
if response.is_success:
60+
return True, "Success with `start_call` response."
61+
62+
return False, response.error_message

0 commit comments

Comments
 (0)