diff --git a/substrate/frame/delegated-staking/src/impls.rs b/substrate/frame/delegated-staking/src/impls.rs index d4a0806a17eda..72884718dfde9 100644 --- a/substrate/frame/delegated-staking/src/impls.rs +++ b/substrate/frame/delegated-staking/src/impls.rs @@ -212,11 +212,7 @@ impl DelegatedStakeInterface for Pallet { )?; // Delegate the funds from who to the `Agent` account. - Pallet::::delegate_to_agent( - RawOrigin::Signed(who.clone()).into(), - agent.clone(), - amount, - ) + Pallet::::delegate_to_agent(RawOrigin::Signed(who.clone()).into(), agent.clone(), amount) } /// Add more delegation to the `Agent` account. @@ -225,11 +221,7 @@ impl DelegatedStakeInterface for Pallet { agent: &Self::AccountId, amount: Self::Balance, ) -> DispatchResult { - Pallet::::delegate_to_agent( - RawOrigin::Signed(who.clone()).into(), - agent.clone(), - amount, - ) + Pallet::::delegate_to_agent(RawOrigin::Signed(who.clone()).into(), agent.clone(), amount) } /// Withdraw delegation of `delegator` to `Agent`. diff --git a/substrate/frame/delegated-staking/src/lib.rs b/substrate/frame/delegated-staking/src/lib.rs index 0bc2c5c5959af..2de5ae44b85b1 100644 --- a/substrate/frame/delegated-staking/src/lib.rs +++ b/substrate/frame/delegated-staking/src/lib.rs @@ -30,8 +30,8 @@ //! behalf for staking these delegated funds. Also, sometimes referred as `Delegatee`. //! - **Delegator**: An account who delegates their funds to an `agent` and authorises them to use //! it for staking. -//! - **AgentLedger**: A data structure that holds important information about the `agent` such -//! as total delegations they have received, any slashes posted to them, etc. +//! - **AgentLedger**: A data structure that holds important information about the `agent` such as +//! total delegations they have received, any slashes posted to them, etc. //! - **Delegation**: A data structure that stores the amount of funds delegated to an `agent` by a //! `delegator`. //! @@ -434,10 +434,7 @@ pub mod pallet { impl Pallet { /// Derive a (keyless) pot account from the given agent account and account type. - pub(crate) fn sub_account( - account_type: AccountType, - agent: T::AccountId, - ) -> T::AccountId { + pub(crate) fn sub_account(account_type: AccountType, agent: T::AccountId) -> T::AccountId { T::PalletId::get().into_sub_account_truncating((account_type, agent.clone())) } @@ -654,8 +651,7 @@ impl Pallet { ); // update delegations - Delegation::::from(&source_delegation.agent, amount) - .save_or_kill(destination_delegator); + Delegation::::from(&source_delegation.agent, amount).save_or_kill(destination_delegator); source_delegation .decrease_delegation(amount) diff --git a/substrate/frame/delegated-staking/src/mock.rs b/substrate/frame/delegated-staking/src/mock.rs index d550c2c9829b2..f98e0c5843bbe 100644 --- a/substrate/frame/delegated-staking/src/mock.rs +++ b/substrate/frame/delegated-staking/src/mock.rs @@ -264,10 +264,7 @@ pub(crate) fn setup_delegation_stake( increment: Balance, ) -> Balance { fund(&agent, 100); - assert_ok!(DelegatedStaking::register_agent( - RawOrigin::Signed(agent).into(), - reward_acc - )); + assert_ok!(DelegatedStaking::register_agent(RawOrigin::Signed(agent).into(), reward_acc)); let mut delegated_amount: Balance = 0; for (index, delegator) in delegators.iter().enumerate() { let amount_to_delegate = base_delegate_amount + increment * index as Balance; diff --git a/substrate/frame/delegated-staking/src/tests.rs b/substrate/frame/delegated-staking/src/tests.rs index 422cfa54eda1c..715436153081d 100644 --- a/substrate/frame/delegated-staking/src/tests.rs +++ b/substrate/frame/delegated-staking/src/tests.rs @@ -156,11 +156,7 @@ fn agent_restrictions() { // agent one tries to delegate to agent 2 assert_noop!( - DelegatedStaking::delegate_to_agent( - RawOrigin::Signed(agent_one).into(), - agent_two, - 10 - ), + DelegatedStaking::delegate_to_agent(RawOrigin::Signed(agent_one).into(), agent_two, 10), Error::::InvalidDelegation ); @@ -245,10 +241,7 @@ mod staking_integration { assert_eq!(agent_obj.bonded_stake(), delegated_balance); } - assert_eq!( - Staking::stake(&agent).unwrap(), - Stake { total: 50 * 100, active: 50 * 100 } - ) + assert_eq!(Staking::stake(&agent).unwrap(), Stake { total: 50 * 100, active: 50 * 100 }) }); } @@ -272,8 +265,8 @@ mod staking_integration { DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 301, 50, 0), Error::::NotEnoughFunds ); - // assert_noop!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 200, 50, - // 0), Error::::NotAllowed); active and total stake remains same + // assert_noop!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), + // 200, 50, 0), Error::::NotAllowed); active and total stake remains same assert!(eq_stake(agent, total_staked, total_staked)); // 305 wants to unbond 50 in era 2, withdrawable in era 5. @@ -307,8 +300,18 @@ mod staking_integration { Error::::NotEnoughFunds ); // less is possible - assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 305, 30, 0)); - assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 305, 20, 0)); + assert_ok!(DelegatedStaking::release_delegation( + RawOrigin::Signed(agent).into(), + 305, + 30, + 0 + )); + assert_ok!(DelegatedStaking::release_delegation( + RawOrigin::Signed(agent).into(), + 305, + 20, + 0 + )); // Lets go to future era where everything is unbonded. Withdrawable amount: 100 + 200 start_era(7); @@ -329,9 +332,19 @@ mod staking_integration { Error::::NotEnoughFunds ); // but can withdraw all its delegation amount. - assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 310, 100, 0)); + assert_ok!(DelegatedStaking::release_delegation( + RawOrigin::Signed(agent).into(), + 310, + 100, + 0 + )); // 320 can withdraw all its delegation amount. - assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 320, 200, 0)); + assert_ok!(DelegatedStaking::release_delegation( + RawOrigin::Signed(agent).into(), + 320, + 200, + 0 + )); // cannot withdraw anything more.. assert_noop!( @@ -363,15 +376,15 @@ mod staking_integration { // withdrawn and test its claimed from there first. // fund(&300, 1000); - // assert_ok!(DelegatedStaking::delegate_to_agent(RawOrigin::Signed(300.into()), delegate, - // 100)); + // assert_ok!(DelegatedStaking::delegate_to_agent(RawOrigin::Signed(300.into()), + // delegate, 100)); // // // verify unbonded balance // assert_eq!(get_agent(&agent).available_to_bond(), 100); // // // withdraw works now without unbonding - // assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 100, - // 0)); assert_eq!(get_agent(&agent).available_to_bond(), 0); + // assert_ok!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, + // 100, 0)); assert_eq!(get_agent(&agent).available_to_bond(), 0); }); } @@ -392,7 +405,11 @@ mod staking_integration { assert_ok!(DelegatedStaking::register_agent(RawOrigin::Signed(200).into(), 201)); // add some delegations to it fund(&300, 1000); - assert_ok!(DelegatedStaking::delegate_to_agent(RawOrigin::Signed(300).into(), 200, 100)); + assert_ok!(DelegatedStaking::delegate_to_agent( + RawOrigin::Signed(300).into(), + 200, + 100 + )); // if delegate calls Staking pallet directly with a different reward destination, it // fails. @@ -441,10 +458,7 @@ mod staking_integration { Error::::AlreadyStaking ); assert_noop!( - DelegatedStaking::register_agent( - RawOrigin::Signed(GENESIS_VALIDATOR).into(), - 201 - ), + DelegatedStaking::register_agent(RawOrigin::Signed(GENESIS_VALIDATOR).into(), 201), Error::::AlreadyStaking ); }); diff --git a/substrate/frame/delegated-staking/src/types.rs b/substrate/frame/delegated-staking/src/types.rs index 521cc4533c1c0..70965374305cf 100644 --- a/substrate/frame/delegated-staking/src/types.rs +++ b/substrate/frame/delegated-staking/src/types.rs @@ -212,10 +212,7 @@ impl Agent { .defensive_ok_or(ArithmeticError::Overflow)?; Ok(Agent { - ledger: AgentLedger { - unclaimed_withdrawals: new_unclaimed_withdrawals, - ..self.ledger - }, + ledger: AgentLedger { unclaimed_withdrawals: new_unclaimed_withdrawals, ..self.ledger }, ..self }) } @@ -241,10 +238,7 @@ impl Agent { let pending_slash = self.ledger.pending_slash.defensive_saturating_sub(amount); let total_delegated = self.ledger.total_delegated.defensive_saturating_sub(amount); - Agent { - ledger: AgentLedger { pending_slash, total_delegated, ..self.ledger }, - ..self - } + Agent { ledger: AgentLedger { pending_slash, total_delegated, ..self.ledger }, ..self } } /// Get the total stake of agent bonded in [`Config::CoreStaking`].