Skip to content

Commit f03f2af

Browse files
committed
Improves how we fetch events
1 parent 3b1a9c4 commit f03f2af

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

bittensor_cli/src/commands/wallets.py

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
unlock_key,
4747
WalletLike,
4848
blocks_to_duration,
49+
decode_account_id,
4950
)
5051

5152

@@ -1549,39 +1550,36 @@ async def schedule_coldkey_swap(
15491550
":white_heavy_check_mark: [green]Successfully scheduled coldkey swap"
15501551
)
15511552

1552-
(block_num, dest_coldkey), schedule_duration = await asyncio.gather(
1553-
find_coldkey_swap_extrinsic(
1554-
subtensor=subtensor,
1555-
start_block=block_pre_call,
1556-
end_block=block_post_call,
1557-
wallet_ss58=wallet.coldkeypub.ss58_address,
1558-
),
1559-
subtensor.get_coldkey_swap_schedule_duration(),
1553+
swap_info = await find_coldkey_swap_extrinsic(
1554+
subtensor=subtensor,
1555+
start_block=block_pre_call,
1556+
end_block=block_post_call,
1557+
wallet_ss58=wallet.coldkeypub.ss58_address,
15601558
)
15611559

1562-
if block_num is not None:
1563-
console.print(
1564-
f"\n[green]Coldkey swap details:[/green]"
1565-
f"\nBlock number: {block_num}"
1566-
f"\nOriginal address: [{COLORS.G.CK}]{wallet.coldkeypub.ss58_address}[/{COLORS.G.CK}]"
1567-
f"\nDestination address: [{COLORS.G.CK}]{dest_coldkey}[/{COLORS.G.CK}]"
1568-
f"\nThe swap will be completed in [green]{blocks_to_duration(schedule_duration)} (Block: {block_num+schedule_duration})[/green] from now."
1569-
f"\n[dim]You can provide the block number to `btcli wallet swap-check`[/dim]"
1570-
)
1571-
else:
1560+
if not swap_info:
15721561
console.print(
15731562
"[yellow]Warning: Could not find the swap extrinsic in recent blocks"
15741563
)
15751564
return True
15761565

1566+
console.print(
1567+
"\n[green]Coldkey swap details:[/green]"
1568+
f"\nBlock number: {swap_info['block_num']}"
1569+
f"\nOriginal address: [{COLORS.G.CK}]{wallet.coldkeypub.ss58_address}[/{COLORS.G.CK}]"
1570+
f"\nDestination address: [{COLORS.G.CK}]{swap_info['dest_coldkey']}[/{COLORS.G.CK}]"
1571+
f"\nThe swap will be completed at block: [green]{swap_info['execution_block']}[/green]"
1572+
f"\n[dim]You can provide the block number to `btcli wallet swap-check`[/dim]"
1573+
)
1574+
15771575

15781576
async def find_coldkey_swap_extrinsic(
15791577
subtensor: SubtensorInterface,
15801578
start_block: int,
15811579
end_block: int,
15821580
wallet_ss58: str,
1583-
) -> tuple[Optional[int], Optional[str]]:
1584-
"""Search for a coldkey swap extrinsic in a range of blocks.
1581+
) -> dict:
1582+
"""Search for a coldkey swap event in a range of blocks.
15851583
15861584
Args:
15871585
subtensor: SubtensorInterface for chain queries
@@ -1590,24 +1588,42 @@ async def find_coldkey_swap_extrinsic(
15901588
wallet_ss58: SS58 address of the signing wallet
15911589
15921590
Returns:
1593-
tuple[Optional[int], Optional[str]]:
1594-
(block number, destination coldkey ss58) if found,
1595-
(None, None) if not found
1591+
dict: Contains the following keys if found:
1592+
- block_num: Block number where swap was scheduled
1593+
- dest_coldkey: SS58 address of destination coldkey
1594+
- execution_block: Block number when swap will execute
1595+
Empty dict if not found
15961596
"""
1597-
for block_num in range(start_block, end_block + 1):
1598-
block_data = await subtensor.substrate.get_block(block_number=block_num)
1599-
for extrinsic in block_data["extrinsics"]:
1600-
extrinsic_data = extrinsic.value
1597+
block_hashes = await asyncio.gather(
1598+
*[
1599+
subtensor.substrate.get_block_hash(block_num)
1600+
for block_num in range(start_block, end_block + 1)
1601+
]
1602+
)
1603+
block_events = await asyncio.gather(
1604+
*[
1605+
subtensor.substrate.get_events(block_hash=block_hash)
1606+
for block_hash in block_hashes
1607+
]
1608+
)
1609+
1610+
for block_num, events in zip(range(start_block, end_block + 1), block_events):
1611+
for event in events:
16011612
if (
1602-
"call" in extrinsic_data
1603-
and extrinsic_data["call"].get("call_function")
1604-
== "schedule_swap_coldkey"
1605-
and extrinsic_data.get("address") == wallet_ss58
1613+
event.get("event", {}).get("module_id") == "SubtensorModule"
1614+
and event.get("event", {}).get("event_id") == "ColdkeySwapScheduled"
16061615
):
1607-
new_coldkey_ss58 = extrinsic_data["call"]["call_args"][0]["value"]
1608-
return block_num, new_coldkey_ss58
1616+
attributes = event["event"].get("attributes", {})
1617+
old_coldkey = decode_account_id(attributes["old_coldkey"][0])
16091618

1610-
return None, None
1619+
if old_coldkey == wallet_ss58:
1620+
return {
1621+
"block_num": block_num,
1622+
"dest_coldkey": decode_account_id(attributes["new_coldkey"][0]),
1623+
"execution_block": attributes["execution_block"],
1624+
}
1625+
1626+
return {}
16111627

16121628

16131629
async def check_swap_status(
@@ -1637,10 +1653,7 @@ async def check_swap_status(
16371653
style=COLOR_PALETTE["GENERAL"]["SUBHEADING_MAIN"],
16381654
no_wrap=True,
16391655
),
1640-
Column(
1641-
"Status",
1642-
style="dark_sea_green3"
1643-
),
1656+
Column("Status", style="dark_sea_green3"),
16441657
title=f"\n[{COLOR_PALETTE['GENERAL']['HEADER']}]Pending Coldkey Swaps\n",
16451658
show_header=True,
16461659
show_edge=False,
@@ -1653,10 +1666,7 @@ async def check_swap_status(
16531666
)
16541667

16551668
for coldkey in scheduled_swaps:
1656-
table.add_row(
1657-
coldkey,
1658-
"Pending"
1659-
)
1669+
table.add_row(coldkey, "Pending")
16601670

16611671
console.print(table)
16621672
console.print(
@@ -1680,36 +1690,31 @@ async def check_swap_status(
16801690
return
16811691

16821692
# Find the swap extrinsic details
1683-
block_num, dest_coldkey = await find_coldkey_swap_extrinsic(
1693+
swap_info = await find_coldkey_swap_extrinsic(
16841694
subtensor=subtensor,
16851695
start_block=expected_block_number,
16861696
end_block=expected_block_number,
16871697
wallet_ss58=origin_ss58,
16881698
)
16891699

1690-
if block_num is None:
1700+
if not swap_info:
16911701
console.print(
16921702
f"[yellow]Warning: Could not find swap extrinsic at block {expected_block_number}[/yellow]"
16931703
)
16941704
return
16951705

1696-
current_block, schedule_duration = await asyncio.gather(
1697-
subtensor.substrate.get_block_number(),
1698-
subtensor.get_coldkey_swap_schedule_duration(),
1699-
)
1700-
1701-
completion_block = block_num + schedule_duration
1702-
remaining_blocks = completion_block - current_block
1706+
current_block = await subtensor.substrate.get_block_number()
1707+
remaining_blocks = swap_info["execution_block"] - current_block
17031708

17041709
if remaining_blocks <= 0:
17051710
console.print("[green]Swap period has completed![/green]")
17061711
return
17071712

17081713
console.print(
17091714
"\n[green]Coldkey swap details:[/green]"
1710-
f"\nScheduled at block: {block_num}"
1715+
f"\nScheduled at block: {swap_info['block_num']}"
17111716
f"\nOriginal address: [{COLORS.G.CK}]{origin_ss58}[/{COLORS.G.CK}]"
1712-
f"\nDestination address: [{COLORS.G.CK}]{dest_coldkey}[/{COLORS.G.CK}]"
1713-
f"\nCompletion block: {completion_block}"
1717+
f"\nDestination address: [{COLORS.G.CK}]{swap_info['dest_coldkey']}[/{COLORS.G.CK}]"
1718+
f"\nCompletion block: {swap_info['execution_block']}"
17141719
f"\nTime remaining: {blocks_to_duration(remaining_blocks)}"
17151720
)

0 commit comments

Comments
 (0)