@@ -2,72 +2,57 @@ use super::*;
22use substrate_fixed:: types:: U64F64 ;
33
44impl < T : Config > Pallet < T > {
5- /// Logic for the coldkey swap operation. Checks for collisions, balances, and cooldowns
6- /// before executing the swap .
5+ /// Transfer all assets, stakes, subnet ownerships, and hotkey associations from `old_coldkey` to
6+ /// to `new_coldkey` .
77 pub fn do_swap_coldkey (
88 old_coldkey : & T :: AccountId ,
99 new_coldkey : & T :: AccountId ,
10- swap_cost : TaoCurrency ,
1110 ) -> DispatchResult {
1211 ensure ! (
13- StakingHotkeys :: <T >:: get( new_coldkey) . is_empty( ) ,
12+ StakingHotkeys :: <T >:: get( & new_coldkey) . is_empty( ) ,
1413 Error :: <T >:: ColdKeyAlreadyAssociated
1514 ) ;
1615 ensure ! (
17- !Self :: hotkey_account_exists( new_coldkey) ,
16+ !Self :: hotkey_account_exists( & new_coldkey) ,
1817 Error :: <T >:: NewColdKeyIsHotkey
1918 ) ;
19+
20+ // Remove and recycle the swap cost from the old coldkey's account
21+ let swap_cost = Self :: get_key_swap_cost ( ) ;
2022 ensure ! (
2123 Self :: can_remove_balance_from_coldkey_account( old_coldkey, swap_cost. into( ) ) ,
2224 Error :: <T >:: NotEnoughBalanceToPaySwapColdKey
2325 ) ;
26+ let burn_amount = Self :: remove_balance_from_coldkey_account ( old_coldkey, swap_cost. into ( ) ) ?;
27+ Self :: recycle_tao ( burn_amount) ;
2428
2529 // Swap the identity if the old coldkey has one
2630 if let Some ( identity) = IdentitiesV2 :: < T > :: take ( old_coldkey) {
27- IdentitiesV2 :: < T > :: insert ( new_coldkey, identity) ;
31+ IdentitiesV2 :: < T > :: insert ( new_coldkey. clone ( ) , identity) ;
2832 }
2933
30- // Remove and recycle the swap cost from the old coldkey's account
31- let burn_amount = Self :: remove_balance_from_coldkey_account ( old_coldkey, swap_cost. into ( ) ) ?;
32- Self :: recycle_tao ( burn_amount) ;
33-
34- Self :: perform_swap_coldkey ( old_coldkey, new_coldkey) ?;
35-
36- Self :: set_last_tx_block ( new_coldkey, Self :: get_current_block_as_u64 ( ) ) ;
37-
38- ColdkeySwapScheduled :: < T > :: remove ( old_coldkey) ;
39-
40- Self :: deposit_event ( Event :: ColdkeySwapped {
41- old_coldkey : old_coldkey. clone ( ) ,
42- new_coldkey : new_coldkey. clone ( ) ,
43- swap_cost,
44- } ) ;
45- Ok ( ( ) )
46- }
47-
48- /// Transfer all assets, stakes, subnet ownerships, and hotkey associations from `old_coldkey` to `new_coldkey`.
49- ///
50- /// # Warning
51- /// This function performs NO validation checks. It assumes the caller has done all the checks before calling it.
52- pub fn perform_swap_coldkey (
53- old_coldkey : & T :: AccountId ,
54- new_coldkey : & T :: AccountId ,
55- ) -> DispatchResult {
5634 for netuid in Self :: get_all_subnet_netuids ( ) {
57- Self :: transfer_subnet_ownership ( netuid, old_coldkey, new_coldkey) ;
58- Self :: transfer_coldkey_stake ( netuid, old_coldkey, new_coldkey) ;
35+ Self :: transfer_subnet_ownership ( netuid, old_coldkey, & new_coldkey) ;
36+ Self :: transfer_coldkey_stake ( netuid, old_coldkey, & new_coldkey) ;
5937 }
60-
61- Self :: transfer_staking_hotkeys ( old_coldkey, new_coldkey) ;
62- Self :: transfer_hotkeys_ownership ( old_coldkey, new_coldkey) ;
38+ Self :: transfer_staking_hotkeys ( old_coldkey, & new_coldkey) ;
39+ Self :: transfer_hotkeys_ownership ( old_coldkey, & new_coldkey) ;
6340
6441 // Transfer any remaining balance from old_coldkey to new_coldkey
6542 let remaining_balance = Self :: get_coldkey_balance ( old_coldkey) ;
6643 if remaining_balance > 0 {
6744 Self :: kill_coldkey_account ( old_coldkey, remaining_balance) ?;
68- Self :: add_balance_to_coldkey_account ( new_coldkey, remaining_balance) ;
45+ Self :: add_balance_to_coldkey_account ( & new_coldkey, remaining_balance) ;
6946 }
7047
48+ Self :: set_last_tx_block ( & new_coldkey, Self :: get_current_block_as_u64 ( ) ) ;
49+ ColdkeySwapAnnouncements :: < T > :: remove ( old_coldkey) ;
50+
51+ Self :: deposit_event ( Event :: ColdkeySwapped {
52+ old_coldkey : old_coldkey. clone ( ) ,
53+ new_coldkey : new_coldkey. clone ( ) ,
54+ swap_cost,
55+ } ) ;
7156 Ok ( ( ) )
7257 }
7358
0 commit comments