Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reduce coupling between salary and core-fellowship pallet #5159

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl pallet_core_fellowship::Config<AmbassadorCoreInstance> for Runtime {
type FastPromoteOrigin = Self::PromoteOrigin;
type EvidenceSize = ConstU32<65536>;
type MaxRank = ConstU32<9>;
type PayoutPeriodDuration = ConstU32<1>;
}

pub type AmbassadorSalaryInstance = pallet_salary::Instance2;
Expand Down
34 changes: 25 additions & 9 deletions substrate/frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ pub struct ParamsType<
Balance: Clone + Eq + PartialEq + Debug,
BlockNumber: Clone + Eq + PartialEq + Debug,
Ranks: Get<u32>,
PayoutPeriodDuration: Get<BlockNumber>,
> {
/// The amounts to be paid when a member of a given rank (-1) is active.
pub active_salary: BoundedVec<Balance, Ranks>,
/// The amounts to be paid when a member of a given rank (-1) is passive.
pub passive_salary: BoundedVec<Balance, Ranks>,
/// The amounts to be paid per `PayoutPeriod` when a member of a given rank (-1) is active.
pub active_salary_per_period: BoundedVec<Balance, Ranks>,
/// The amounts to be paid per `PayoutPeriod` when a member of a given rank (-1) is passive.
pub passive_salary_per_period: BoundedVec<Balance, Ranks>,
/// The number of blocks between two payout periods.
pub payout_period_duration: PayoutPeriodDuration,
/// The period between which unproven members become demoted.
pub demotion_period: BoundedVec<BlockNumber, Ranks>,
/// The period between which members must wait before they may proceed to this rank.
Expand All @@ -140,8 +143,9 @@ impl<
{
fn default() -> Self {
Self {
active_salary: Default::default(),
passive_salary: Default::default(),
active_salary_per_period: Default::default(),
passive_salary_per_period: Default::default(),
payout_period_duration: Default::default(),
demotion_period: Default::default(),
min_promotion_period: Default::default(),
offboard_timeout: BlockNumber::default(),
Expand Down Expand Up @@ -225,6 +229,12 @@ pub mod pallet {
/// Increasing this value is supported, but decreasing it may lead to a broken state.
#[pallet::constant]
type MaxRank: Get<u32>;

/// The number of blocks between two payout periods.
///
/// This is used to calculate the payout amount for each period.
#[pallet::constant]
type PayoutPeriodDuration: Get<BlockNumberFor<Self>>;
}

pub type ParamsOf<T, I> =
Expand Down Expand Up @@ -629,6 +639,9 @@ pub mod pallet {
&mut p.passive_salary,
partial_params.passive_salary,
);
if let Some(new_payout_period) = partial_params.payout_period_duration {
p.payout_period = new_payout_period;
}
Self::set_partial_params_slice(
&mut p.demotion_period,
partial_params.demotion_period,
Expand Down Expand Up @@ -691,9 +704,12 @@ pub mod pallet {
None => return Zero::zero(),
};
let params = Params::<T, I>::get();
let salary =
if member.is_active { params.active_salary } else { params.passive_salary };
salary[index]
let salary_per_period = if member.is_active {
params.active_salary_per_period
} else {
params.passive_salary_per_period
};
salary_per_period[index]
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions substrate/frame/core-fellowship/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ impl<T: Config<I>, I: 'static> UncheckedOnRuntimeUpgrade for MigrateToV1<T, I> {
let old_value = v0::Params::<T, I>::take();
// Write the new value to storage
let new = crate::ParamsType {
active_salary: BoundedVec::defensive_truncate_from(old_value.active_salary.to_vec()),
passive_salary: BoundedVec::defensive_truncate_from(old_value.passive_salary.to_vec()),
active_salary_per_period: BoundedVec::defensive_truncate_from(
old_value.active_salary.to_vec(),
),
passive_salary_per_period: BoundedVec::defensive_truncate_from(
old_value.passive_salary.to_vec(),
),
payout_period_duration: old_value.payout_period,
demotion_period: BoundedVec::defensive_truncate_from(
old_value.demotion_period.to_vec(),
),
Expand Down
10 changes: 9 additions & 1 deletion substrate/frame/salary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ pub mod pallet {
pub fn cycle_period() -> BlockNumberFor<T> {
T::RegistrationPeriod::get() + T::PayoutPeriod::get()
}
fn calculate_periods_to_pay(last_payout: BlockNumberFor<T>, now: BlockNumberFor<T>) -> u32 {
// TODO: retrieve payout period from core-fellowship config?
let payout_period = T::PayoutPeriod::get();
let elapsed = now.saturating_sub(last_payout);
(elapsed / payout_period) as u32
}
fn do_payout(who: T::AccountId, beneficiary: T::AccountId) -> DispatchResult {
let mut status = Status::<T, I>::get().ok_or(Error::<T, I>::NotStarted)?;
let mut claimant = Claimant::<T, I>::get(&who).ok_or(Error::<T, I>::NotInducted)?;
Expand All @@ -418,7 +424,9 @@ pub mod pallet {
Nothing | Attempted { .. } if claimant.last_active < status.cycle_index => {
// Not registered for this cycle. Pay from whatever is left.
let rank = T::Members::rank_of(&who).ok_or(Error::<T, I>::NotMember)?;
let ideal_payout = T::Salary::get_salary(rank, &who);
let salary_per_period = T::Salary::get_salary(rank, &who);
let periods_to_pay = Self::calculate_periods_to_pay(status.cycle_start, now);
let ideal_payout = salary_per_period.saturating_mul(periods_to_pay.into());

let pot = status
.budget
Expand Down