Skip to content

Commit a815792

Browse files
committed
adds netuid support in swap_hotkey
1 parent 92aec72 commit a815792

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

bittensor_cli/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,8 @@ def wallet_swap_hotkey(
18971897
wallet_name: Optional[str] = Options.wallet_name,
18981898
wallet_path: Optional[str] = Options.wallet_path,
18991899
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
1900+
netuid: Optional[int] = Options.netuid_not_req,
1901+
all_netuids: bool = Options.all_netuids,
19001902
network: Optional[list[str]] = Options.network,
19011903
destination_hotkey_name: Optional[str] = typer.Argument(
19021904
None, help="Destination hotkey name."
@@ -1917,12 +1919,14 @@ def wallet_swap_hotkey(
19171919
19181920
- Make sure that your original key pair (coldkeyA, hotkeyA) is already registered.
19191921
- Make sure that you use a newly created hotkeyB in this command. A hotkeyB that is already registered cannot be used in this command.
1922+
- You can specify the netuid for which you want to swap the hotkey for. If it is not defined, the swap will be initiated for all subnets.
19201923
- Finally, note that this command requires a fee of 1 TAO for recycling and this fee is taken from your wallet (coldkeyA).
19211924
19221925
EXAMPLE
19231926
1924-
[green]$[/green] btcli wallet swap_hotkey destination_hotkey_name --wallet-name your_wallet_name --wallet-hotkey original_hotkey
1927+
[green]$[/green] btcli wallet swap_hotkey destination_hotkey_name --wallet-name your_wallet_name --wallet-hotkey original_hotkey --netuid 1
19251928
"""
1929+
netuid = get_optional_netuid(netuid, all_netuids)
19261930
self.verbosity_handler(quiet, verbose, json_output)
19271931
original_wallet = self.wallet_ask(
19281932
wallet_name,
@@ -1946,7 +1950,7 @@ def wallet_swap_hotkey(
19461950
self.initialize_chain(network)
19471951
return self._run_command(
19481952
wallets.swap_hotkey(
1949-
original_wallet, new_wallet, self.subtensor, prompt, json_output
1953+
original_wallet, new_wallet, self.subtensor, netuid, prompt, json_output
19501954
)
19511955
)
19521956

bittensor_cli/src/bittensor/extrinsics/registration.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,8 @@ def _update_curr_block(
16111611
"""
16121612
Update the current block data with the provided block information and difficulty.
16131613
1614-
This function updates the current block and its difficulty in a thread-safe manner. It sets the current block
1614+
This function updates the current block
1615+
and its difficulty in a thread-safe manner. It sets the current block
16151616
number, hashes the block with the hotkey, updates the current block bytes, and packs the difficulty.
16161617
16171618
:param curr_diff: Shared array to store the current difficulty.
@@ -1745,6 +1746,7 @@ async def swap_hotkey_extrinsic(
17451746
subtensor: "SubtensorInterface",
17461747
wallet: Wallet,
17471748
new_wallet: Wallet,
1749+
netuid: Optional[int] = None,
17481750
prompt: bool = False,
17491751
) -> bool:
17501752
"""
@@ -1756,37 +1758,76 @@ async def swap_hotkey_extrinsic(
17561758
netuids_registered = await subtensor.get_netuids_for_hotkey(
17571759
wallet.hotkey.ss58_address, block_hash=block_hash
17581760
)
1759-
if not len(netuids_registered) > 0:
1761+
netuids_registered_new_hotkey = await subtensor.get_netuids_for_hotkey(
1762+
new_wallet.hotkey.ss58_address, block_hash=block_hash
1763+
)
1764+
1765+
if netuid is not None and netuid not in netuids_registered:
1766+
err_console.print(
1767+
f":cross_mark: [red]Failed[/red]: Original hotkey {wallet.hotkey.ss58_address} is not registered on subnet {netuid}"
1768+
)
1769+
return False
1770+
1771+
elif not len(netuids_registered) > 0:
17601772
err_console.print(
1761-
f"Destination hotkey [dark_orange]{new_wallet.hotkey.ss58_address}[/dark_orange] is not registered. "
1773+
f"Original hotkey [dark_orange]{wallet.hotkey.ss58_address}[/dark_orange] is not registered on any subnet. "
17621774
f"Please register and try again"
17631775
)
17641776
return False
17651777

1778+
if netuid is not None:
1779+
if netuid in netuids_registered_new_hotkey:
1780+
err_console.print(
1781+
f":cross_mark: [red]Failed[/red]: New hotkey {new_wallet.hotkey.ss58_address} "
1782+
f"is already registered on subnet {netuid}"
1783+
)
1784+
return False
1785+
else:
1786+
if len(netuids_registered_new_hotkey) > 0:
1787+
err_console.print(
1788+
f":cross_mark: [red]Failed[/red]: New hotkey {new_wallet.hotkey.ss58_address} "
1789+
f"is already registered on subnet(s) {netuids_registered_new_hotkey}"
1790+
)
1791+
return False
1792+
17661793
if not unlock_key(wallet).success:
17671794
return False
17681795

17691796
if prompt:
17701797
# Prompt user for confirmation.
1771-
if not Confirm.ask(
1772-
f"Do you want to swap [dark_orange]{wallet.name}[/dark_orange] hotkey \n\t"
1773-
f"[dark_orange]{wallet.hotkey.ss58_address}[/dark_orange] with hotkey \n\t"
1774-
f"[dark_orange]{new_wallet.hotkey.ss58_address}[/dark_orange]\n"
1775-
"This operation will cost [bold cyan]1 TAO t (recycled)[/bold cyan]"
1776-
):
1798+
if netuid is not None:
1799+
confirm_message = (
1800+
f"Do you want to swap [dark_orange]{wallet.name}[/dark_orange] hotkey \n\t"
1801+
f"[dark_orange]{wallet.hotkey.ss58_address}[/dark_orange] with hotkey \n\t"
1802+
f"[dark_orange]{new_wallet.hotkey.ss58_address}[/dark_orange] on subnet {netuid}\n"
1803+
"This operation will cost [bold cyan]1 TAO (recycled)[/bold cyan]"
1804+
)
1805+
else:
1806+
confirm_message = (
1807+
f"Do you want to swap [dark_orange]{wallet.name}[/dark_orange] hotkey \n\t"
1808+
f"[dark_orange]{wallet.hotkey.ss58_address}[/dark_orange] with hotkey \n\t"
1809+
f"[dark_orange]{new_wallet.hotkey.ss58_address}[/dark_orange] on all subnets\n"
1810+
"This operation will cost [bold cyan]1 TAO (recycled)[/bold cyan]"
1811+
)
1812+
1813+
if not Confirm.ask(confirm_message):
17771814
return False
17781815
print_verbose(
17791816
f"Swapping {wallet.name}'s hotkey ({wallet.hotkey.ss58_address}) with "
1780-
f"{new_wallet.name}s hotkey ({new_wallet.hotkey.ss58_address})"
1817+
f"{new_wallet.name}'s hotkey ({new_wallet.hotkey.ss58_address})"
17811818
)
17821819
with console.status(":satellite: Swapping hotkeys...", spinner="aesthetic"):
1820+
call_params = {
1821+
"hotkey": wallet.hotkey.ss58_address,
1822+
"new_hotkey": new_wallet.hotkey.ss58_address,
1823+
}
1824+
if netuid is not None:
1825+
call_params["netuid"] = netuid
1826+
17831827
call = await subtensor.substrate.compose_call(
17841828
call_module="SubtensorModule",
17851829
call_function="swap_hotkey",
1786-
call_params={
1787-
"hotkey": wallet.hotkey.ss58_address,
1788-
"new_hotkey": new_wallet.hotkey.ss58_address,
1789-
},
1830+
call_params=call_params,
17901831
)
17911832
success, err_msg = await subtensor.sign_and_send_extrinsic(call, wallet)
17921833

bittensor_cli/src/commands/wallets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ async def swap_hotkey(
16321632
original_wallet: Wallet,
16331633
new_wallet: Wallet,
16341634
subtensor: SubtensorInterface,
1635+
netuid: Optional[int],
16351636
prompt: bool,
16361637
json_output: bool,
16371638
):
@@ -1640,6 +1641,7 @@ async def swap_hotkey(
16401641
subtensor,
16411642
original_wallet,
16421643
new_wallet,
1644+
netuid=netuid,
16431645
prompt=prompt,
16441646
)
16451647
if json_output:

0 commit comments

Comments
 (0)