@@ -467,18 +467,14 @@ impl<T: Config> Pallet<T> {
467467 }
468468
469469 fn do_migrate_to_agent ( who : & T :: AccountId , reward_account : & T :: AccountId ) -> DispatchResult {
470+ Self :: do_register_agent ( who, reward_account) ;
471+
470472 // We create a proxy delegator that will keep all the delegation funds until funds are
471473 // transferred to actual delegator.
472474 let proxy_delegator = Self :: sub_account ( AccountType :: ProxyDelegator , who. clone ( ) ) ;
473475
474- // Transfer minimum balance to proxy delegator.
475- T :: Currency :: transfer (
476- who,
477- & proxy_delegator,
478- T :: Currency :: minimum_balance ( ) ,
479- Preservation :: Protect ,
480- )
481- . map_err ( |_| Error :: < T > :: NotEnoughFunds ) ?;
476+ // Keep proxy delegator alive until all funds are migrated.
477+ frame_system:: Pallet :: < T > :: inc_providers ( & proxy_delegator) ;
482478
483479 // Get current stake
484480 let stake = T :: CoreStaking :: stake ( who) ?;
@@ -488,12 +484,10 @@ impl<T: Config> Pallet<T> {
488484
489485 // transferring just released staked amount. This should never fail but if it does, it
490486 // indicates bad state and we abort.
491- T :: Currency :: transfer ( who, & proxy_delegator, stake. total , Preservation :: Protect )
487+ T :: Currency :: transfer ( who, & proxy_delegator, stake. total , Preservation :: Expendable )
492488 . map_err ( |_| Error :: < T > :: BadState ) ?;
493489
494- Self :: do_register_agent ( who, reward_account) ;
495490 T :: CoreStaking :: update_payee ( who, reward_account) ?;
496-
497491 Self :: do_delegate ( & proxy_delegator, who, stake. total )
498492 }
499493
@@ -624,7 +618,6 @@ impl<T: Config> Pallet<T> {
624618
625619 // update delegations
626620 Delegation :: < T > :: from ( & source_delegation. agent , amount) . save_or_kill ( destination_delegator) ;
627-
628621 source_delegation
629622 . decrease_delegation ( amount)
630623 . defensive_ok_or ( Error :: < T > :: BadState ) ?
@@ -641,15 +634,19 @@ impl<T: Config> Pallet<T> {
641634 defensive_assert ! ( released == amount, "hold should have been released fully" ) ;
642635
643636 // transfer the released amount to `destination_delegator`.
644- // Note: The source should have been funded ED in the beginning so it should not be dusted.
645- T :: Currency :: transfer (
637+ let post_balance = T :: Currency :: transfer (
646638 source_delegator,
647639 destination_delegator,
648640 amount,
649- Preservation :: Preserve ,
641+ Preservation :: Expendable ,
650642 )
651643 . map_err ( |_| Error :: < T > :: BadState ) ?;
652644
645+ // if balance is zero, clear provider for source (proxy) delegator.
646+ if post_balance == Zero :: zero ( ) {
647+ let _ = frame_system:: Pallet :: < T > :: dec_providers ( source_delegator) . defensive ( ) ;
648+ }
649+
653650 // hold the funds again in the new delegator account.
654651 T :: Currency :: hold ( & HoldReason :: Delegating . into ( ) , destination_delegator, amount) ?;
655652
0 commit comments