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,42 pallet_prelude::{StorageValue, ValueQuery, DispatchResult, OptionQuery},43 BoundedVec,44 };45 use frame_system::{pallet_prelude::OriginFor, ensure_root};46 use xcm::v1::MultiLocation;4748 #[pallet::config]49 pub trait Config: frame_system::Config {50 #[pallet::constant]51 type DefaultWeightToFeeCoefficient: Get<u64>;52 #[pallet::constant]53 type DefaultMinGasPrice: Get<u64>;5455 #[pallet::constant]56 type MaxXcmAllowedLocations: Get<u32>;57 #[pallet::constant]58 type AppPromotionDailyRate: Get<Perbill>;59 #[pallet::constant]60 type DayRelayBlocks: Get<Self::BlockNumber>;61 }6263 #[pallet::error]64 pub enum Error<T> {65 InconsistentConfiguration,66 }6768 #[pallet::storage]69 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<70 Value = u64,71 QueryKind = ValueQuery,72 OnEmpty = T::DefaultWeightToFeeCoefficient,73 >;7475 #[pallet::storage]76 pub type MinGasPriceOverride<T: Config> =77 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;7879 #[pallet::storage]80 pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<81 Value = BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>,82 QueryKind = OptionQuery,83 >;8485 #[pallet::storage]86 pub type AppPromomotionConfigurationOverride<T: Config> =87 StorageValue<Value = AppPromotionConfiguration<T::BlockNumber>, QueryKind = ValueQuery>;8889 #[pallet::call]90 impl<T: Config> Pallet<T> {91 #[pallet::weight(T::DbWeight::get().writes(1))]92 pub fn set_weight_to_fee_coefficient_override(93 origin: OriginFor<T>,94 coeff: Option<u64>,95 ) -> DispatchResult {96 ensure_root(origin)?;97 if let Some(coeff) = coeff {98 <WeightToFeeCoefficientOverride<T>>::set(coeff);99 } else {100 <WeightToFeeCoefficientOverride<T>>::kill();101 }102 Ok(())103 }104105 #[pallet::weight(T::DbWeight::get().writes(1))]106 pub fn set_min_gas_price_override(107 origin: OriginFor<T>,108 coeff: Option<u64>,109 ) -> DispatchResult {110 ensure_root(origin)?;111 if let Some(coeff) = coeff {112 <MinGasPriceOverride<T>>::set(coeff);113 } else {114 <MinGasPriceOverride<T>>::kill();115 }116 Ok(())117 }118119 #[pallet::weight(T::DbWeight::get().writes(1))]120 pub fn set_xcm_allowed_locations(121 origin: OriginFor<T>,122 locations: Option<BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>>,123 ) -> DispatchResult {124 ensure_root(origin)?;125 <XcmAllowedLocationsOverride<T>>::set(locations);126 Ok(())127 }128129 #[pallet::weight(T::DbWeight::get().writes(1))]130 pub fn set_app_promotion_configuration_override(131 origin: OriginFor<T>,132 mut configuration: AppPromotionConfiguration<T::BlockNumber>,133 ) -> DispatchResult {134 ensure_root(origin)?;135 if configuration.interval_income.is_some() {136 return Err(<Error<T>>::InconsistentConfiguration.into());137 }138139 configuration.interval_income = configuration.recalculation_interval.map(|b| {140 Perbill::from_rational(b, T::DayRelayBlocks::get())141 * T::AppPromotionDailyRate::get()142 });143144 <AppPromomotionConfigurationOverride<T>>::set(configuration);145146 Ok(())147 }148 }149150 #[pallet::pallet]151 #[pallet::generate_store(pub(super) trait Store)]152 pub struct Pallet<T>(_);153}154155pub struct WeightToFee<T, B>(PhantomData<(T, B)>);156157impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>158where159 T: Config,160 B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,161{162 type Balance = B;163164 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {165 smallvec!(WeightToFeeCoefficient {166 coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)167 .into(),168 coeff_frac: Perbill::from_parts(169 (<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32170 ),171 negative: false,172 degree: 1,173 })174 }175}176177pub struct FeeCalculator<T>(PhantomData<T>);178impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {179 fn min_gas_price() -> (U256, Weight) {180 (181 <MinGasPriceOverride<T>>::get().into(),182 T::DbWeight::get().reads(1),183 )184 }185}186187#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]188pub struct AppPromotionConfiguration<BlockNumber> {189 190 pub recalculation_interval: Option<BlockNumber>,191 192 pub pending_interval: Option<BlockNumber>,193 194 pub interval_income: Option<Perbill>,195 196 pub max_stakers_per_calculation: Option<u8>,197}