Skip to content

Commit 30508d4

Browse files
committed
Add schedule_coldkey_swap
1 parent 04601d2 commit 30508d4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

bittensor_cli/src/commands/wallets.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from rich.table import Column, Table
1515
from rich.tree import Tree
1616
from rich.padding import Padding
17+
from rich.prompt import Confirm
1718

1819
from bittensor_cli.src import COLOR_PALETTE
1920
from bittensor_cli.src.bittensor import utils
@@ -1472,3 +1473,74 @@ async def sign(wallet: Wallet, message: str, use_hotkey: str):
14721473
signed_message = keypair.sign(message.encode("utf-8")).hex()
14731474
console.print("[dark_sea_green3]Message signed successfully:")
14741475
console.print(signed_message)
1476+
1477+
1478+
async def schedule_coldkey_swap(
1479+
wallet: Wallet,
1480+
subtensor: SubtensorInterface,
1481+
new_coldkey_ss58: str,
1482+
force_swap: bool = False,
1483+
) -> bool:
1484+
"""Schedules a coldkey swap operation to be executed at a future block.
1485+
1486+
Args:
1487+
wallet (Wallet): The wallet initiating the coldkey swap
1488+
subtensor (SubtensorInterface): Connection to the Bittensor network
1489+
new_coldkey_ss58 (str): SS58 address of the new coldkey
1490+
force_swap (bool, optional): Whether to force the swap even if the new coldkey is already scheduled for a swap. Defaults to False.
1491+
Returns:
1492+
bool: True if the swap was scheduled successfully, False otherwise
1493+
"""
1494+
if not is_valid_ss58_address(new_coldkey_ss58):
1495+
print_error(f"Invalid SS58 address format: {new_coldkey_ss58}")
1496+
return False
1497+
1498+
scheduled_coldkey_swap = await subtensor.get_scheduled_coldkey_swap()
1499+
if wallet.coldkeypub.ss58_address in scheduled_coldkey_swap:
1500+
print_error(
1501+
f"Coldkey {wallet.coldkeypub.ss58_address} is already scheduled for a swap."
1502+
)
1503+
console.print("[dim]Use the force_swap (--force) flag to override this.[/dim]")
1504+
if not force_swap:
1505+
return False
1506+
else:
1507+
console.print(
1508+
"[yellow]Continuing with the swap due to force_swap flag.[/yellow]\n"
1509+
)
1510+
1511+
prompt = (
1512+
"You are [red]swapping[/red] your [blue]coldkey[/blue] to a new address.\n"
1513+
f"Current ss58: [{COLOR_PALETTE['GENERAL']['COLDKEY']}]{wallet.coldkeypub.ss58_address}[/{COLOR_PALETTE['GENERAL']['COLDKEY']}]\n"
1514+
f"New ss58: [{COLOR_PALETTE['GENERAL']['COLDKEY']}]{new_coldkey_ss58}[/{COLOR_PALETTE['GENERAL']['COLDKEY']}]\n"
1515+
"Are you sure you want to continue?"
1516+
)
1517+
if not Confirm.ask(prompt):
1518+
return False
1519+
1520+
if not unlock_key(wallet).success:
1521+
return False
1522+
1523+
call = await subtensor.substrate.compose_call(
1524+
call_module="SubtensorModule",
1525+
call_function="schedule_swap_coldkey",
1526+
call_params={
1527+
"new_coldkey": new_coldkey_ss58,
1528+
},
1529+
)
1530+
1531+
with console.status(":satellite: Scheduling coldkey swap on-chain..."):
1532+
success, err_msg = await subtensor.sign_and_send_extrinsic(
1533+
call,
1534+
wallet,
1535+
wait_for_inclusion=True,
1536+
wait_for_finalization=True,
1537+
)
1538+
1539+
if not success:
1540+
print_error(f"Failed to schedule coldkey swap: {err_msg}")
1541+
return False
1542+
1543+
console.print(
1544+
":white_heavy_check_mark: [green]Successfully scheduled coldkey swap"
1545+
)
1546+
return True

0 commit comments

Comments
 (0)