Skip to content

Commit

Permalink
Add MaxCommission and CommissionUpdated (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Aug 21, 2023
1 parent 9450075 commit 4ba453c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions pallet/account-migration/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl darwinia_deposit::Config for Runtime {
impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = Dummy;
type MaxCommission = ();
type MaxDeposits = ();
type MaxUnstakings = ();
type MinStakingDuration = ();
Expand Down
25 changes: 22 additions & 3 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub mod pallet {
/// Deposit [`StakeExt`] interface.
type Deposit: StakeExt<AccountId = Self::AccountId, Amount = Balance>;

/// Maximum commission rate.
#[pallet::constant]
type MaxCommission: Get<Perbill>;

/// Minimum time to stake at least.
#[pallet::constant]
type MinStakingDuration: Get<Self::BlockNumber>;
Expand Down Expand Up @@ -147,14 +151,25 @@ pub mod pallet {
kton_amount: Balance,
deposits: Vec<DepositId<T>>,
},
CommissionUpdated {
who: T::AccountId,
commission: Perbill,
},
/// A payout has been made for the staker.
Payout { staker: T::AccountId, ring_amount: Balance },
Payout {
staker: T::AccountId,
ring_amount: Balance,
},
/// A new collator set has been elected.
Elected { collators: Vec<T::AccountId> },
Elected {
collators: Vec<T::AccountId>,
},
}

#[pallet::error]
pub enum Error<T> {
/// Commission rate must be less than maximum commission rate.
CommissionTooHigh,
/// Exceed maximum deposit count.
ExceedMaxDeposits,
/// Exceed maximum unstaking/unbonding count.
Expand Down Expand Up @@ -449,9 +464,13 @@ pub mod pallet {
pub fn collect(origin: OriginFor<T>, commission: Perbill) -> DispatchResult {
let who = ensure_signed(origin)?;

if commission > T::MaxCommission::get() {
Err(<Error<T>>::CommissionTooHigh)?;
}

<Collators<T>>::mutate(&who, |c| *c = Some(commission));

// TODO: event?
Self::deposit_event(Event::CommissionUpdated { who, commission });

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions pallet/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ impl darwinia_staking::Stake for KtonStaking {
}
}
frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(99);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}
impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = frame_support::traits::ConstU32<16>;
type MinStakingDuration = frame_support::traits::ConstU64<3>;
Expand Down
7 changes: 6 additions & 1 deletion pallet/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,17 @@ fn collect_should_work() {
assert!(Staking::collator_of(1).is_none());
assert_ok!(Staking::stake(RuntimeOrigin::signed(1), UNIT, 0, Vec::new()));

(0..=100).for_each(|c| {
(0..=99).for_each(|c| {
let c = Perbill::from_percent(c);

assert_ok!(Staking::collect(RuntimeOrigin::signed(1), c));
assert_eq!(Staking::collator_of(1).unwrap(), c);
});

assert_noop!(
Staking::collect(RuntimeOrigin::signed(1), Perbill::from_percent(100)),
<Error<Runtime>>::CommissionTooHigh
);
});
}

Expand Down
4 changes: 4 additions & 0 deletions precompile/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,13 @@ impl darwinia_staking::Stake for KtonStaking {
}
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(99);
}
impl darwinia_staking::Config for TestRuntime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = frame_support::traits::ConstU32<16>;
type MinStakingDuration = frame_support::traits::ConstU64<3>;
Expand Down
2 changes: 2 additions & 0 deletions runtime/crab/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = MinStakingDuration;
Expand Down
2 changes: 2 additions & 0 deletions runtime/darwinia/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = MinStakingDuration;
Expand Down
2 changes: 2 additions & 0 deletions runtime/pangolin/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = ConstU32<{ 10 * MINUTES }>;
Expand Down
2 changes: 2 additions & 0 deletions runtime/pangoro/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = ConstU32<{ 10 * MINUTES }>;
Expand Down

0 comments on commit 4ba453c

Please sign in to comment.