Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed Mar 31, 2024
1 parent 8d3a607 commit cca4319
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
12 changes: 2 additions & 10 deletions substrate/frame/delegated-staking/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ impl<T: Config> DelegatedStakeInterface for Pallet<T> {
)?;

// Delegate the funds from who to the `Agent` account.
Pallet::<T>::delegate_to_agent(
RawOrigin::Signed(who.clone()).into(),
agent.clone(),
amount,
)
Pallet::<T>::delegate_to_agent(RawOrigin::Signed(who.clone()).into(), agent.clone(), amount)
}

/// Add more delegation to the `Agent` account.
Expand All @@ -225,11 +221,7 @@ impl<T: Config> DelegatedStakeInterface for Pallet<T> {
agent: &Self::AccountId,
amount: Self::Balance,
) -> DispatchResult {
Pallet::<T>::delegate_to_agent(
RawOrigin::Signed(who.clone()).into(),
agent.clone(),
amount,
)
Pallet::<T>::delegate_to_agent(RawOrigin::Signed(who.clone()).into(), agent.clone(), amount)
}

/// Withdraw delegation of `delegator` to `Agent`.
Expand Down
12 changes: 4 additions & 8 deletions substrate/frame/delegated-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
//!
Expand Down Expand Up @@ -434,10 +434,7 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
/// 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()))
}

Expand Down Expand Up @@ -654,8 +651,7 @@ impl<T: Config> Pallet<T> {
);

// update delegations
Delegation::<T>::from(&source_delegation.agent, amount)
.save_or_kill(destination_delegator);
Delegation::<T>::from(&source_delegation.agent, amount).save_or_kill(destination_delegator);

source_delegation
.decrease_delegation(amount)
Expand Down
5 changes: 1 addition & 4 deletions substrate/frame/delegated-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 38 additions & 24 deletions substrate/frame/delegated-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>::InvalidDelegation
);

Expand Down Expand Up @@ -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 })
});
}

Expand All @@ -272,8 +265,8 @@ mod staking_integration {
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 301, 50, 0),
Error::<T>::NotEnoughFunds
);
// assert_noop!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 200, 50,
// 0), Error::<T>::NotAllowed); active and total stake remains same
// assert_noop!(DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(),
// 200, 50, 0), Error::<T>::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.
Expand Down Expand Up @@ -307,8 +300,18 @@ mod staking_integration {
Error::<T>::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);
Expand All @@ -329,9 +332,19 @@ mod staking_integration {
Error::<T>::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!(
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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.
Expand Down Expand Up @@ -441,10 +458,7 @@ mod staking_integration {
Error::<T>::AlreadyStaking
);
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(GENESIS_VALIDATOR).into(),
201
),
DelegatedStaking::register_agent(RawOrigin::Signed(GENESIS_VALIDATOR).into(), 201),
Error::<T>::AlreadyStaking
);
});
Expand Down
10 changes: 2 additions & 8 deletions substrate/frame/delegated-staking/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ impl<T: Config> Agent<T> {
.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
})
}
Expand All @@ -241,10 +238,7 @@ impl<T: Config> Agent<T> {
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`].
Expand Down

0 comments on commit cca4319

Please sign in to comment.