Skip to content
Merged
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
19 changes: 10 additions & 9 deletions pallets/dapp-staking/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use super::{Pallet as DappStaking, *};

use astar_primitives::{dapp_staking::FIXED_TIER_SLOTS_ARGS, Balance};
use astar_primitives::{
dapp_staking::{FIXED_NUMBER_OF_TIER_SLOTS, FIXED_TIER_SLOTS_ARGS},
Balance,
};

use frame_system::Pallet as System;
use sp_arithmetic::Permill;
Expand Down Expand Up @@ -116,7 +119,7 @@ pub(super) const UNIT: Balance = 1_000_000_000_000_000_000;
pub(super) const MIN_TIER_THRESHOLD: Balance = 10 * UNIT;

/// Number of slots in the tier system.
pub(super) const NUMBER_OF_SLOTS: u32 = 16;
pub(super) const NUMBER_OF_SLOTS: u32 = FIXED_NUMBER_OF_TIER_SLOTS as u32;

/// Random seed.
pub(super) const SEED: u32 = 9000;
Expand Down Expand Up @@ -207,13 +210,11 @@ pub(super) fn init_tier_settings<T: Config>() {
.expect("Invalid number of tier thresholds provided.");

// Init tier config, based on the initial params
let init_tier_config =
TiersConfiguration::<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice> {
slots_per_tier: BoundedVec::try_from(vec![0, 6, 10, 0]).unwrap(),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds,
_phantom: Default::default(),
};
let init_tier_config = TiersConfiguration::<T::NumberOfTiers> {
slots_per_tier: BoundedVec::try_from(vec![0, 6, 10, 0]).unwrap(),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds,
};

assert!(tier_params.is_valid());
assert!(init_tier_config.is_valid());
Expand Down
49 changes: 12 additions & 37 deletions pallets/dapp-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ use frame_support::{
weights::Weight,
};
use frame_system::pallet_prelude::*;
use sp_arithmetic::fixed_point::FixedU128;
use sp_runtime::{
traits::{One, Saturating, UniqueSaturatedInto, Zero},
Perbill, Permill, SaturatedConversion,
Expand All @@ -58,9 +57,8 @@ use astar_primitives::{
dapp_staking::{
AccountCheck, CycleConfiguration, DAppId, EraNumber, Observer as DAppStakingObserver,
PeriodNumber, Rank, RankedTier, SmartContractHandle, StakingRewardHandler, TierId,
TierSlots as TierSlotFunc, FIXED_TIER_SLOTS_ARGS,
FIXED_TIER_SLOTS_ARGS,
},
oracle::PriceProvider,
Balance, BlockNumber,
};

Expand Down Expand Up @@ -144,9 +142,6 @@ pub mod pallet {
/// Privileged origin for managing dApp staking pallet.
type ManagerOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;

/// Used to provide price information about the native token.
type NativePriceProvider: PriceProvider;

/// Used to handle reward payouts & reward pool amount fetching.
type StakingRewardHandler: StakingRewardHandler<Self::AccountId>;

Expand All @@ -159,20 +154,6 @@ pub mod pallet {
/// Used to check whether an account is allowed to participate in dApp staking.
type AccountCheck: AccountCheck<Self::AccountId>;

/// Used to calculate total number of tier slots for some price.
type TierSlots: TierSlotFunc;

/// Base native currency price used to calculate base number of slots.
/// This is used to adjust tier configuration, tier thresholds specifically, based on the native token price changes.
///
/// When dApp staking thresholds were modeled, a base price was set from which the initial configuration is derived.
/// E.g. for a price of 0.05$, we get 100 slots, and certain tier thresholds.
/// Using these values as the base, we can adjust the configuration based on the current price.
///
/// This is connected with the `TierSlots` associated type, since it's used to calculate the total number of slots for the given price.
#[pallet::constant]
type BaseNativeCurrencyPrice: Get<FixedU128>;

/// Maximum length of a single era reward span length entry.
#[pallet::constant]
type EraRewardSpanLength: Get<u32>;
Expand Down Expand Up @@ -502,11 +483,8 @@ pub mod pallet {

/// Tier configuration user for current & preceding eras.
#[pallet::storage]
pub type TierConfig<T: Config> = StorageValue<
_,
TiersConfiguration<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice>,
ValueQuery,
>;
pub type TierConfig<T: Config> =
StorageValue<_, TiersConfiguration<T::NumberOfTiers>, ValueQuery>;

/// Information about which tier a dApp belonged to in a specific era.
#[pallet::storage]
Expand Down Expand Up @@ -619,16 +597,14 @@ pub mod pallet {
.try_into()
.expect("Invalid number of tier thresholds provided.");

let tier_config =
TiersConfiguration::<T::NumberOfTiers, T::TierSlots, T::BaseNativeCurrencyPrice> {
slots_per_tier: BoundedVec::<u16, T::NumberOfTiers>::try_from(
self.slots_per_tier.clone(),
)
.expect("Invalid number of slots per tier entries provided."),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds,
_phantom: Default::default(),
};
let tier_config = TiersConfiguration::<T::NumberOfTiers> {
slots_per_tier: BoundedVec::<u16, T::NumberOfTiers>::try_from(
self.slots_per_tier.clone(),
)
.expect("Invalid number of slots per tier entries provided."),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds,
};
assert!(
tier_config.is_valid(),
"Invalid tier config values provided."
Expand Down Expand Up @@ -2178,11 +2154,10 @@ pub mod pallet {

// Re-calculate tier configuration for the upcoming new era
let tier_params = StaticTierParams::<T>::get();
let average_price = T::NativePriceProvider::average_price();
let total_issuance = T::Currency::total_issuance();

let new_tier_config =
TierConfig::<T>::get().calculate_new(&tier_params, average_price, total_issuance);
TierConfig::<T>::get().calculate_new(&tier_params, total_issuance);

// Validate new tier configuration
if new_tier_config.is_valid() {
Expand Down
34 changes: 4 additions & 30 deletions pallets/dapp-staking/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ use frame_support::{
traits::{fungible::Mutate as FunMutate, ConstBool, ConstU128, ConstU32, EitherOfDiverse},
weights::Weight,
};
use sp_arithmetic::fixed_point::FixedU128;
use sp_io::TestExternalities;
use sp_runtime::{BuildStorage, Permill};
use sp_std::cell::RefCell;

use astar_primitives::{
dapp_staking::{
Observer as DappStakingObserver, SmartContract, StandardTierSlots, FIXED_TIER_SLOTS_ARGS,
},
dapp_staking::{Observer as DappStakingObserver, SmartContract, FIXED_TIER_SLOTS_ARGS},
Balance, BlockNumber,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
Expand Down Expand Up @@ -94,17 +91,9 @@ impl pallet_migrations::Config for Test {
type MaxServiceWeight = MaxServiceWeight;
}

pub struct DummyPriceProvider;
impl PriceProvider for DummyPriceProvider {
fn average_price() -> FixedU128 {
NATIVE_PRICE.with(|v| v.borrow().clone())
}
}

thread_local! {
pub(crate) static DOES_PAYOUT_SUCCEED: RefCell<bool> = RefCell::new(false);
pub(crate) static BLOCK_BEFORE_NEW_ERA: RefCell<EraNumber> = RefCell::new(0);
pub(crate) static NATIVE_PRICE: RefCell<FixedU128> = RefCell::new(BaseNativeCurrencyPrice::get());
pub(crate) static MAX_BONUS_SAFE_MOVES: RefCell<u8> = RefCell::new(0);
}

Expand Down Expand Up @@ -192,9 +181,6 @@ impl Get<u8> for DynamicMaxBonusSafeMovesPerPeriod {
}
}

parameter_types! {
pub const BaseNativeCurrencyPrice: FixedU128 = FixedU128::from_rational(5, 100);
}
ord_parameter_types! {
pub const ContractRegisterAccount: AccountId = 1337;
pub const ContractUnregisterAccount: AccountId = 1779;
Expand All @@ -214,13 +200,10 @@ impl pallet_dapp_staking::Config for Test {
>;
type ManagerOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, EnsureSignedBy<ManagerAccount, AccountId>>;
type NativePriceProvider = DummyPriceProvider;
type StakingRewardHandler = DummyStakingRewardHandler;
type CycleConfiguration = DummyCycleConfiguration;
type Observers = DummyDappStakingObserver;
type AccountCheck = DummyAccountCheck;
type TierSlots = StandardTierSlots;
type BaseNativeCurrencyPrice = BaseNativeCurrencyPrice;
type EraRewardSpanLength = ConstU32<8>;
type RewardRetentionInPeriods = ConstU32<2>;
type MaxNumberOfContracts = ConstU32<10>;
Expand Down Expand Up @@ -354,22 +337,13 @@ impl ExtBuilder {
.try_into()
.expect("Invalid number of tier thresholds provided.");

// Init tier config, based on the initial params. Needs to be adjusted to the init price.
let init_tier_config = TiersConfiguration::<
<Test as Config>::NumberOfTiers,
<Test as Config>::TierSlots,
<Test as Config>::BaseNativeCurrencyPrice,
> {
// Init tier config based on the initial params.
let init_tier_config = TiersConfiguration::<<Test as Config>::NumberOfTiers> {
slots_per_tier: BoundedVec::try_from(vec![2, 5, 13, 20]).unwrap(),
reward_portion: tier_params.reward_portion.clone(),
tier_thresholds,
_phantom: Default::default(),
}
.calculate_new(
&tier_params,
NATIVE_PRICE.with(|v| v.borrow().clone()),
total_issuance,
);
.calculate_new(&tier_params, total_issuance);

pallet_dapp_staking::StaticTierParams::<Test>::put(tier_params);
pallet_dapp_staking::TierConfig::<Test>::put(init_tier_config.clone());
Expand Down
Loading
Loading