Skip to content

Commit c311332

Browse files
committed
Merge branch 'ankan/02-pallet-delegated-staking' into ankan/03-np-delegation-integration
2 parents 8f2d412 + 0bf4dba commit c311332

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

substrate/frame/delegated-staking/src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

substrate/frame/delegated-staking/src/tests.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,8 @@ mod staking_integration {
590590
Balances::balance_on_hold(&HoldReason::Delegating.into(), &proxy_delegator),
591591
expected_proxy_delegated_amount
592592
);
593-
// ED + stake amount is transferred from delegate to proxy delegator account.
594-
assert_eq!(
595-
Balances::free_balance(200),
596-
5000 - staked_amount - ExistentialDeposit::get()
597-
);
593+
// stake amount is transferred from delegate to proxy delegator account.
594+
assert_eq!(Balances::free_balance(200), 5000 - staked_amount);
598595
assert_eq!(Staking::stake(&200).unwrap(), init_stake);
599596
assert_eq!(get_agent(&200).ledger.effective_balance(), 4000);
600597
assert_eq!(get_agent(&200).available_to_bond(), 0);

0 commit comments

Comments
 (0)