Skip to content

Commit

Permalink
review feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Aug 6, 2024
1 parent 352eaaf commit d2d4287
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub(super) fn init_tier_settings<T: Config>() {
.unwrap(),
};

let total_issuance = SEED as u128 * UNIT;
let total_issuance = 1000 * MIN_TIER_THRESHOLD;
let tier_thresholds = tier_params
.tier_thresholds
.iter()
Expand Down
1 change: 1 addition & 0 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ pub mod pallet {
reward_portion: vec![Permill::from_percent(100 / num_tiers); num_tiers as usize],
slot_distribution: vec![Permill::from_percent(100 / num_tiers); num_tiers as usize],
tier_thresholds: (0..num_tiers)
.rev()
.map(|i| TierThreshold::FixedPercentage {
required_percentage: Perbill::from_percent(i),
})
Expand Down
10 changes: 10 additions & 0 deletions pallets/dapp-staking-v3/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ mod v8 {

if result.is_err() {
log::error!("Failed to translate StaticTierParams from previous V7 type to current V8 type. Check TierParametersV7 decoding.");
// Enable maintenance mode.
ActiveProtocolState::<T>::mutate(|state| {
state.maintenance = true;
});
log::warn!("Maintenance mode enabled.");
return T::DbWeight::get().reads_writes(1, 0);
}

Expand Down Expand Up @@ -133,6 +138,11 @@ mod v8 {

if result.is_err() {
log::error!("Failed to translate TierConfig from previous V7 type to current V8 type. Check TiersConfigurationV7 decoding.");
// Enable maintenance mode.
ActiveProtocolState::<T>::mutate(|state| {
state.maintenance = true;
});
log::warn!("Maintenance mode enabled.");
return T::DbWeight::get().reads_writes(2, 1);
}

Expand Down
104 changes: 44 additions & 60 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,17 +1662,54 @@ impl<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>> TiersConfiguration<NT, T
)
};

// Update tier thresholds.
// In case number of slots increase, we decrease thresholds required to enter the tier.
// In case number of slots decrease, we increase the threshold required to enter the tier.
//
// According to formula: %delta_threshold = (100% / (100% - delta_%_slots) - 1) * 100%
//
// where delta_%_slots is simply: (base_num_slots - new_num_slots) / base_num_slots
//
// `base_num_slots` is the number of slots at the base native currency price.
//
// When these entries are put into the threshold formula, we get:
// = 1 / ( 1 - (base_num_slots - new_num_slots) / base_num_slots ) - 1
// = 1 / ( new / base) - 1
// = base / new - 1
// = (base - new) / new
//
// This number can be negative. In order to keep all operations in unsigned integer domain,
// formulas are adjusted like:
//
// 1. Number of slots has increased, threshold is expected to decrease
// %delta_threshold = (new_num_slots - base_num_slots) / new_num_slots
// new_threshold = base_threshold * (1 - %delta_threshold)
//
// 2. Number of slots has decreased, threshold is expected to increase
// %delta_threshold = (base_num_slots - new_num_slots) / new_num_slots
// new_threshold = base_threshold * (1 + %delta_threshold)
//
let new_tier_thresholds: BoundedVec<Balance, NT> = params
.tier_thresholds
.clone()
.iter()
.map(|threshold| {
Self::adjust_threshold(
threshold,
delta_threshold,
total_issuance,
new_number_of_slots >= base_number_of_slots,
)
.map(|threshold| match threshold {
TierThreshold::DynamicPercentage {
percentage,
minimum_required_percentage,
} => {
let amount = *percentage * total_issuance;
let adjusted_amount = if new_number_of_slots >= base_number_of_slots {
amount.saturating_sub(delta_threshold.saturating_mul_int(amount))
} else {
amount.saturating_add(delta_threshold.saturating_mul_int(amount))
};
let minimum_amount = *minimum_required_percentage * total_issuance;
adjusted_amount.max(minimum_amount)
}
TierThreshold::FixedPercentage {
required_percentage,
} => *required_percentage * total_issuance,
})
.collect::<Vec<_>>()
.try_into()
Expand All @@ -1685,59 +1722,6 @@ impl<NT: Get<u32>, T: TierSlotsFunc, P: Get<FixedU128>> TiersConfiguration<NT, T
_phantom: Default::default(),
}
}

// Update tier thresholds.
// In case number of slots increase, we decrease thresholds required to enter the tier.
// In case number of slots decrease, we increase the threshold required to enter the tier.
//
// According to formula: %delta_threshold = (100% / (100% - delta_%_slots) - 1) * 100%
//
// where delta_%_slots is simply: (base_num_slots - new_num_slots) / base_num_slots
//
// `base_num_slots` is the number of slots at the base native currency price.
//
// When these entries are put into the threshold formula, we get:
// = 1 / ( 1 - (base_num_slots - new_num_slots) / base_num_slots ) - 1
// = 1 / ( new / base) - 1
// = base / new - 1
// = (base - new) / new
//
// This number can be negative. In order to keep all operations in unsigned integer domain,
// formulas are adjusted like:
//
// 1. Number of slots has increased, threshold is expected to decrease
// %delta_threshold = (new_num_slots - base_num_slots) / new_num_slots
// new_threshold = base_threshold * (1 - %delta_threshold)
//
// 2. Number of slots has decreased, threshold is expected to increase
// %delta_threshold = (base_num_slots - new_num_slots) / new_num_slots
// new_threshold = base_threshold * (1 + %delta_threshold)
//
fn adjust_threshold(
threshold: &TierThreshold,
delta_threshold: FixedU128,
total_issuance: Balance,
slots_increased: bool,
) -> Balance {
match threshold {
TierThreshold::DynamicPercentage {
percentage,
minimum_required_percentage,
} => {
let amount = *percentage * total_issuance;
let adjusted_amount = if slots_increased {
amount.saturating_sub(delta_threshold.saturating_mul_int(amount))
} else {
amount.saturating_add(delta_threshold.saturating_mul_int(amount))
};
let minimum_amount = *minimum_required_percentage * total_issuance;
adjusted_amount.max(minimum_amount)
}
TierThreshold::FixedPercentage {
required_percentage,
} => *required_percentage * total_issuance,
}
}
}

/// Information about all of the dApps that got into tiers, and tier rewards
Expand Down

0 comments on commit d2d4287

Please sign in to comment.