46
46
unlock_key ,
47
47
WalletLike ,
48
48
blocks_to_duration ,
49
+ decode_account_id ,
49
50
)
50
51
51
52
@@ -1549,39 +1550,36 @@ async def schedule_coldkey_swap(
1549
1550
":white_heavy_check_mark: [green]Successfully scheduled coldkey swap"
1550
1551
)
1551
1552
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 ,
1560
1558
)
1561
1559
1562
- if block_num is not None :
1563
- console .print (
1564
- f"\n [green]Coldkey swap details:[/green]"
1565
- f"\n Block number: { block_num } "
1566
- f"\n Original address: [{ COLORS .G .CK } ]{ wallet .coldkeypub .ss58_address } [/{ COLORS .G .CK } ]"
1567
- f"\n Destination address: [{ COLORS .G .CK } ]{ dest_coldkey } [/{ COLORS .G .CK } ]"
1568
- f"\n The 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 :
1572
1561
console .print (
1573
1562
"[yellow]Warning: Could not find the swap extrinsic in recent blocks"
1574
1563
)
1575
1564
return True
1576
1565
1566
+ console .print (
1567
+ "\n [green]Coldkey swap details:[/green]"
1568
+ f"\n Block number: { swap_info ['block_num' ]} "
1569
+ f"\n Original address: [{ COLORS .G .CK } ]{ wallet .coldkeypub .ss58_address } [/{ COLORS .G .CK } ]"
1570
+ f"\n Destination address: [{ COLORS .G .CK } ]{ swap_info ['dest_coldkey' ]} [/{ COLORS .G .CK } ]"
1571
+ f"\n The 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
+
1577
1575
1578
1576
async def find_coldkey_swap_extrinsic (
1579
1577
subtensor : SubtensorInterface ,
1580
1578
start_block : int ,
1581
1579
end_block : int ,
1582
1580
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.
1585
1583
1586
1584
Args:
1587
1585
subtensor: SubtensorInterface for chain queries
@@ -1590,24 +1588,42 @@ async def find_coldkey_swap_extrinsic(
1590
1588
wallet_ss58: SS58 address of the signing wallet
1591
1589
1592
1590
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
1596
1596
"""
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 :
1601
1612
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"
1606
1615
):
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 ])
1609
1618
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 {}
1611
1627
1612
1628
1613
1629
async def check_swap_status (
@@ -1637,10 +1653,7 @@ async def check_swap_status(
1637
1653
style = COLOR_PALETTE ["GENERAL" ]["SUBHEADING_MAIN" ],
1638
1654
no_wrap = True ,
1639
1655
),
1640
- Column (
1641
- "Status" ,
1642
- style = "dark_sea_green3"
1643
- ),
1656
+ Column ("Status" , style = "dark_sea_green3" ),
1644
1657
title = f"\n [{ COLOR_PALETTE ['GENERAL' ]['HEADER' ]} ]Pending Coldkey Swaps\n " ,
1645
1658
show_header = True ,
1646
1659
show_edge = False ,
@@ -1653,10 +1666,7 @@ async def check_swap_status(
1653
1666
)
1654
1667
1655
1668
for coldkey in scheduled_swaps :
1656
- table .add_row (
1657
- coldkey ,
1658
- "Pending"
1659
- )
1669
+ table .add_row (coldkey , "Pending" )
1660
1670
1661
1671
console .print (table )
1662
1672
console .print (
@@ -1680,36 +1690,31 @@ async def check_swap_status(
1680
1690
return
1681
1691
1682
1692
# Find the swap extrinsic details
1683
- block_num , dest_coldkey = await find_coldkey_swap_extrinsic (
1693
+ swap_info = await find_coldkey_swap_extrinsic (
1684
1694
subtensor = subtensor ,
1685
1695
start_block = expected_block_number ,
1686
1696
end_block = expected_block_number ,
1687
1697
wallet_ss58 = origin_ss58 ,
1688
1698
)
1689
1699
1690
- if block_num is None :
1700
+ if not swap_info :
1691
1701
console .print (
1692
1702
f"[yellow]Warning: Could not find swap extrinsic at block { expected_block_number } [/yellow]"
1693
1703
)
1694
1704
return
1695
1705
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
1703
1708
1704
1709
if remaining_blocks <= 0 :
1705
1710
console .print ("[green]Swap period has completed![/green]" )
1706
1711
return
1707
1712
1708
1713
console .print (
1709
1714
"\n [green]Coldkey swap details:[/green]"
1710
- f"\n Scheduled at block: { block_num } "
1715
+ f"\n Scheduled at block: { swap_info [ ' block_num' ] } "
1711
1716
f"\n Original address: [{ COLORS .G .CK } ]{ origin_ss58 } [/{ COLORS .G .CK } ]"
1712
- f"\n Destination address: [{ COLORS .G .CK } ]{ dest_coldkey } [/{ COLORS .G .CK } ]"
1713
- f"\n Completion block: { completion_block } "
1717
+ f"\n Destination address: [{ COLORS .G .CK } ]{ swap_info [ ' dest_coldkey' ] } [/{ COLORS .G .CK } ]"
1718
+ f"\n Completion block: { swap_info [ 'execution_block' ] } "
1714
1719
f"\n Time remaining: { blocks_to_duration (remaining_blocks )} "
1715
1720
)
0 commit comments