1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819use core::marker::PhantomData;2021use frame_support::{22 pallet,23 weights::{WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient, Weight},24 traits::Get,25};26use parity_scale_codec::{Decode, Encode, MaxEncodedLen};27use scale_info::TypeInfo;28use sp_arithmetic::{29 per_things::{Perbill, PerThing},30 traits::{BaseArithmetic, Unsigned},31};32use smallvec::smallvec;3334pub use pallet::*;35use sp_core::U256;3637#[pallet]38mod pallet {39 use super::*;40 use frame_support::{41 traits::{Get, ReservableCurrency, Currency},42 pallet_prelude::{StorageValue, ValueQuery, DispatchResult, IsType, OptionQuery},43 BoundedVec, log,44 };45 use frame_system::{pallet_prelude::OriginFor, ensure_root, Config as SystemConfig};46 use xcm::v1::MultiLocation;4748 pub type BalanceOf<T> =49 <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;5051 #[pallet::config]52 pub trait Config: frame_system::Config {53 54 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;5556 57 type Currency: ReservableCurrency<Self::AccountId>;5859 #[pallet::constant]60 type DefaultWeightToFeeCoefficient: Get<u64>;61 #[pallet::constant]62 type DefaultMinGasPrice: Get<u64>;6364 #[pallet::constant]65 type MaxXcmAllowedLocations: Get<u32>;66 #[pallet::constant]67 type AppPromotionDailyRate: Get<Perbill>;68 #[pallet::constant]69 type DayRelayBlocks: Get<Self::BlockNumber>;7071 #[pallet::constant]72 type DefaultCollatorSelectionMaxCollators: Get<u32>;73 #[pallet::constant]74 type DefaultCollatorSelectionLicenseBond: Get<BalanceOf<Self>>;75 #[pallet::constant]76 type DefaultCollatorSelectionKickThreshold: Get<Self::BlockNumber>;77 }7879 #[pallet::event]80 #[pallet::generate_deposit(pub(super) fn deposit_event)]81 pub enum Event<T: Config> {82 NewDesiredCollators {83 desired_collators: Option<u32>,84 },85 NewCollatorLicenseBond {86 bond_cost: Option<BalanceOf<T>>,87 },88 NewCollatorKickThreshold {89 length_in_blocks: Option<T::BlockNumber>,90 },91 }9293 #[pallet::error]94 pub enum Error<T> {95 InconsistentConfiguration,96 }9798 #[pallet::storage]99 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<100 Value = u64,101 QueryKind = ValueQuery,102 OnEmpty = T::DefaultWeightToFeeCoefficient,103 >;104105 #[pallet::storage]106 pub type MinGasPriceOverride<T: Config> =107 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;108109 #[pallet::storage]110 pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<111 Value = BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>,112 QueryKind = OptionQuery,113 >;114115 #[pallet::storage]116 pub type AppPromomotionConfigurationOverride<T: Config> =117 StorageValue<Value = AppPromotionConfiguration<T::BlockNumber>, QueryKind = ValueQuery>;118119 #[pallet::storage]120 pub type CollatorSelectionDesiredCollatorsOverride<T: Config> = StorageValue<121 Value = u32,122 QueryKind = ValueQuery,123 OnEmpty = T::DefaultCollatorSelectionMaxCollators,124 >;125126 #[pallet::storage]127 pub type CollatorSelectionLicenseBondOverride<T: Config> = StorageValue<128 Value = BalanceOf<T>,129 QueryKind = ValueQuery,130 OnEmpty = T::DefaultCollatorSelectionLicenseBond,131 >;132133 #[pallet::storage]134 pub type CollatorSelectionKickThresholdOverride<T: Config> = StorageValue<135 Value = T::BlockNumber,136 QueryKind = ValueQuery,137 OnEmpty = T::DefaultCollatorSelectionKickThreshold,138 >;139140 #[pallet::call]141 impl<T: Config> Pallet<T> {142 #[pallet::call_index(0)]143 #[pallet::weight(T::DbWeight::get().writes(1))]144 pub fn set_weight_to_fee_coefficient_override(145 origin: OriginFor<T>,146 coeff: Option<u64>,147 ) -> DispatchResult {148 ensure_root(origin)?;149 if let Some(coeff) = coeff {150 <WeightToFeeCoefficientOverride<T>>::set(coeff);151 } else {152 <WeightToFeeCoefficientOverride<T>>::kill();153 }154 Ok(())155 }156157 #[pallet::call_index(1)]158 #[pallet::weight(T::DbWeight::get().writes(1))]159 pub fn set_min_gas_price_override(160 origin: OriginFor<T>,161 coeff: Option<u64>,162 ) -> DispatchResult {163 ensure_root(origin)?;164 if let Some(coeff) = coeff {165 <MinGasPriceOverride<T>>::set(coeff);166 } else {167 <MinGasPriceOverride<T>>::kill();168 }169 Ok(())170 }171172 #[pallet::call_index(2)]173 #[pallet::weight(T::DbWeight::get().writes(1))]174 pub fn set_xcm_allowed_locations(175 origin: OriginFor<T>,176 locations: Option<BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>>,177 ) -> DispatchResult {178 ensure_root(origin)?;179 <XcmAllowedLocationsOverride<T>>::set(locations);180 Ok(())181 }182183 #[pallet::call_index(3)]184 #[pallet::weight(T::DbWeight::get().writes(1))]185 pub fn set_app_promotion_configuration_override(186 origin: OriginFor<T>,187 mut configuration: AppPromotionConfiguration<T::BlockNumber>,188 ) -> DispatchResult {189 ensure_root(origin)?;190 if configuration.interval_income.is_some() {191 return Err(<Error<T>>::InconsistentConfiguration.into());192 }193194 configuration.interval_income = configuration.recalculation_interval.map(|b| {195 Perbill::from_rational(b, T::DayRelayBlocks::get())196 * T::AppPromotionDailyRate::get()197 });198199 <AppPromomotionConfigurationOverride<T>>::set(configuration);200201 Ok(())202 }203204 #[pallet::weight(T::DbWeight::get().writes(1))]205 pub fn set_collator_selection_desired_collators(206 origin: OriginFor<T>,207 max: Option<u32>,208 ) -> DispatchResult {209 ensure_root(origin)?;210 if let Some(max) = max {211 212 if max > T::DefaultCollatorSelectionMaxCollators::get() {213 log::warn!("max > T::DefaultCollatorSelectionMaxCollators; you might need to run benchmarks again");214 }215 <CollatorSelectionDesiredCollatorsOverride<T>>::set(max);216 } else {217 <CollatorSelectionDesiredCollatorsOverride<T>>::kill();218 }219 Self::deposit_event(Event::NewDesiredCollators { desired_collators: max });220 Ok(())221 }222223 #[pallet::weight(T::DbWeight::get().writes(1))]224 pub fn set_collator_selection_license_bond(225 origin: OriginFor<T>,226 amount: Option<BalanceOf<T>>,227 ) -> DispatchResult {228 ensure_root(origin)?;229 if let Some(amount) = amount {230 <CollatorSelectionLicenseBondOverride<T>>::set(amount);231 } else {232 <CollatorSelectionLicenseBondOverride<T>>::kill();233 }234 Self::deposit_event(Event::NewCollatorLicenseBond { bond_cost: amount });235 Ok(())236 }237238 #[pallet::weight(T::DbWeight::get().writes(1))]239 pub fn set_collator_selection_kick_threshold(240 origin: OriginFor<T>,241 threshold: Option<T::BlockNumber>,242 ) -> DispatchResult {243 ensure_root(origin)?;244 if let Some(threshold) = threshold {245 <CollatorSelectionKickThresholdOverride<T>>::set(threshold);246 } else {247 <CollatorSelectionKickThresholdOverride<T>>::kill();248 }249 Self::deposit_event(Event::NewCollatorKickThreshold { length_in_blocks: threshold });250 Ok(())251 }252 }253254 #[pallet::pallet]255 #[pallet::generate_store(pub(super) trait Store)]256 pub struct Pallet<T>(_);257}258259pub struct WeightToFee<T, B>(PhantomData<(T, B)>);260261impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>262where263 T: Config,264 B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,265{266 type Balance = B;267268 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {269 smallvec!(WeightToFeeCoefficient {270 coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)271 .into(),272 coeff_frac: Perbill::from_parts(273 (<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32274 ),275 negative: false,276 degree: 1,277 })278 }279}280281pub struct FeeCalculator<T>(PhantomData<T>);282impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {283 fn min_gas_price() -> (U256, Weight) {284 (285 <MinGasPriceOverride<T>>::get().into(),286 T::DbWeight::get().reads(1),287 )288 }289}290291#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]292pub struct AppPromotionConfiguration<BlockNumber> {293 294 pub recalculation_interval: Option<BlockNumber>,295 296 pub pending_interval: Option<BlockNumber>,297 298 pub interval_income: Option<Perbill>,299 300 pub max_stakers_per_calculation: Option<u8>,301}