Skip to content

Commit 89b83d2

Browse files
committed
Adds block calculation
1 parent dc9e692 commit 89b83d2

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

bittensor_cli/src/commands/wallets.py

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from rich.padding import Padding
1717
from rich.prompt import Confirm
1818

19-
from bittensor_cli.src import COLOR_PALETTE
19+
from bittensor_cli.src import COLOR_PALETTE, COLORS
2020
from bittensor_cli.src.bittensor import utils
2121
from bittensor_cli.src.bittensor.balances import Balance
2222
from bittensor_cli.src.bittensor.chain_data import (
@@ -1510,8 +1510,8 @@ async def schedule_coldkey_swap(
15101510

15111511
prompt = (
15121512
"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"
1513+
f"Current ss58: [{COLORS.G.CK}]{wallet.coldkeypub.ss58_address}[/{COLORS.G.CK}]\n"
1514+
f"New ss58: [{COLORS.G.CK}]{new_coldkey_ss58}[/{COLORS.G.CK}]\n"
15151515
"Are you sure you want to continue?"
15161516
)
15171517
if not Confirm.ask(prompt):
@@ -1520,12 +1520,15 @@ async def schedule_coldkey_swap(
15201520
if not unlock_key(wallet).success:
15211521
return False
15221522

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-
},
1523+
block_pre_call, call = await asyncio.gather(
1524+
subtensor.substrate.get_block_number(),
1525+
subtensor.substrate.compose_call(
1526+
call_module="SubtensorModule",
1527+
call_function="schedule_swap_coldkey",
1528+
call_params={
1529+
"new_coldkey": new_coldkey_ss58,
1530+
},
1531+
),
15291532
)
15301533

15311534
with console.status(":satellite: Scheduling coldkey swap on-chain..."):
@@ -1535,6 +1538,7 @@ async def schedule_coldkey_swap(
15351538
wait_for_inclusion=True,
15361539
wait_for_finalization=True,
15371540
)
1541+
block_post_call = await subtensor.substrate.get_block_number()
15381542

15391543
if not success:
15401544
print_error(f"Failed to schedule coldkey swap: {err_msg}")
@@ -1543,4 +1547,59 @@ async def schedule_coldkey_swap(
15431547
console.print(
15441548
":white_heavy_check_mark: [green]Successfully scheduled coldkey swap"
15451549
)
1550+
1551+
block_num, dest_coldkey = await find_coldkey_swap_extrinsic(
1552+
subtensor=subtensor,
1553+
start_block=block_pre_call,
1554+
end_block=block_post_call,
1555+
wallet_ss58=wallet.coldkeypub.ss58_address,
1556+
)
1557+
1558+
if block_num is not None:
1559+
console.print(
1560+
f"\n[green]Coldkey swap details:[/green]"
1561+
f"\nBlock number: {block_num}"
1562+
f"\nOriginal address: [{COLORS.G.CK}]{wallet.coldkeypub.ss58_address}[/{COLORS.G.CK}]"
1563+
f"\nDestination address: [{COLORS.G.CK}]{dest_coldkey}[/{COLORS.G.CK}]"
1564+
f"\n\nYou can provide this block number to `btcli wallet swap check`"
1565+
)
1566+
else:
1567+
console.print(
1568+
"[yellow]Warning: Could not find the swap extrinsic in recent blocks"
1569+
)
15461570
return True
1571+
1572+
1573+
async def find_coldkey_swap_extrinsic(
1574+
subtensor: SubtensorInterface,
1575+
start_block: int,
1576+
end_block: int,
1577+
wallet_ss58: str,
1578+
) -> tuple[Optional[int], Optional[str]]:
1579+
"""Search for a coldkey swap extrinsic in a range of blocks.
1580+
1581+
Args:
1582+
subtensor: SubtensorInterface for chain queries
1583+
start_block: Starting block number to search
1584+
end_block: Ending block number to search (inclusive)
1585+
wallet_ss58: SS58 address of the signing wallet
1586+
1587+
Returns:
1588+
tuple[Optional[int], Optional[str]]:
1589+
(block number, destination coldkey ss58) if found,
1590+
(None, None) if not found
1591+
"""
1592+
for block_num in range(start_block, end_block + 1):
1593+
block_data = await subtensor.substrate.get_block(block_number=block_num)
1594+
for extrinsic in block_data["extrinsics"]:
1595+
extrinsic_data = extrinsic.value
1596+
if (
1597+
"call" in extrinsic_data
1598+
and extrinsic_data["call"].get("call_function")
1599+
== "schedule_swap_coldkey"
1600+
and extrinsic_data.get("address") == wallet_ss58
1601+
):
1602+
new_coldkey_ss58 = extrinsic_data["call"]["call_args"][0]["value"]
1603+
return block_num, new_coldkey_ss58
1604+
1605+
return None, None

0 commit comments

Comments
 (0)