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 sp_arithmetic::traits::{BaseArithmetic, Unsigned};27use smallvec::smallvec;2829pub use pallet::*;30use sp_core::U256;31use sp_runtime::Perbill;3233#[pallet]34mod pallet {35 use super::*;36 use frame_support::{37 traits::Get,38 pallet_prelude::{StorageValue, ValueQuery, DispatchResult, OptionQuery},39 BoundedVec,40 };41 use frame_system::{pallet_prelude::OriginFor, ensure_root};42 use xcm::v1::MultiLocation;4344 #[pallet::config]45 pub trait Config: frame_system::Config {46 #[pallet::constant]47 type DefaultWeightToFeeCoefficient: Get<u32>;4849 #[pallet::constant]50 type DefaultMinGasPrice: Get<u64>;5152 #[pallet::constant]53 type MaxOverridedAllowedLocations: Get<u32>;54 #[pallet::constant]55 type AppPromotionDailyRate: Get<Perbill>;56 #[pallet::constant]57 type DayRelayBlocks: Get<Self::BlockNumber>;58 }5960 #[pallet::storage]61 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<62 Value = u32,63 QueryKind = ValueQuery,64 OnEmpty = T::DefaultWeightToFeeCoefficient,65 >;6667 #[pallet::storage]68 pub type MinGasPriceOverride<T: Config> =69 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;7071 #[pallet::storage]72 pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<73 Value = BoundedVec<MultiLocation, T::MaxOverridedAllowedLocations>,74 QueryKind = OptionQuery,75 >;7677 #[pallet::storage]78 pub type AppPromomotionConfigurationOverride<T: Config> = StorageValue<79 Value = (80 Option<T::BlockNumber>,81 Option<Perbill>,82 Option<T::BlockNumber>,83 Option<u8>,84 ),85 QueryKind = OptionQuery,86 >;8788 #[pallet::call]89 impl<T: Config> Pallet<T> {90 #[pallet::weight(T::DbWeight::get().writes(1))]91 pub fn set_weight_to_fee_coefficient_override(92 origin: OriginFor<T>,93 coeff: Option<u32>,94 ) -> DispatchResult {95 let _sender = ensure_root(origin)?;96 if let Some(coeff) = coeff {97 <WeightToFeeCoefficientOverride<T>>::set(coeff);98 } else {99 <WeightToFeeCoefficientOverride<T>>::kill();100 }101 Ok(())102 }103104 #[pallet::weight(T::DbWeight::get().writes(1))]105 pub fn set_min_gas_price_override(106 origin: OriginFor<T>,107 coeff: Option<u64>,108 ) -> DispatchResult {109 let _sender = ensure_root(origin)?;110 if let Some(coeff) = coeff {111 <MinGasPriceOverride<T>>::set(coeff);112 } else {113 <MinGasPriceOverride<T>>::kill();114 }115 Ok(())116 }117118 #[pallet::weight(T::DbWeight::get().writes(1))]119 pub fn set_xcm_allowed_locations(120 origin: OriginFor<T>,121 locations: Option<BoundedVec<MultiLocation, T::MaxOverridedAllowedLocations>>,122 ) -> DispatchResult {123 let _sender = ensure_root(origin)?;124 <XcmAllowedLocationsOverride<T>>::set(locations);125 Ok(())126 }127128 #[pallet::weight(T::DbWeight::get().writes(1))]129 pub fn set_app_promotion_configuration_override(130 origin: OriginFor<T>,131 recalculation_interval: Option<T::BlockNumber>,132 pending_interval: Option<T::BlockNumber>,133 stakers_payout_limit: Option<u8>,134 ) -> DispatchResult {135 let _sender = ensure_root(origin)?;136137 if recalculation_interval.is_none()138 && pending_interval.is_none()139 && stakers_payout_limit.is_none()140 {141 <AppPromomotionConfigurationOverride<T>>::kill();142 } else {143 let mut current_config =144 <AppPromomotionConfigurationOverride<T>>::take().unwrap_or_default();145146 recalculation_interval.map(|b| {147 current_config.0 = Some(b);148 current_config.1 = Some(149 Perbill::from_rational(b, T::DayRelayBlocks::get())150 * T::AppPromotionDailyRate::get(),151 )152 });153 pending_interval.map(|b| current_config.2 = Some(b));154 stakers_payout_limit.map(|p| current_config.3 = Some(p));155 <AppPromomotionConfigurationOverride<T>>::set(Some(current_config));156 }157158 Ok(())159 }160 }161162 #[pallet::pallet]163 #[pallet::generate_store(pub(super) trait Store)]164 pub struct Pallet<T>(_);165}166167pub struct WeightToFee<T, B>(PhantomData<(T, B)>);168169impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>170where171 T: Config,172 B: BaseArithmetic + From<u32> + Copy + Unsigned,173{174 type Balance = B;175176 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {177 smallvec!(WeightToFeeCoefficient {178 coeff_integer: <WeightToFeeCoefficientOverride<T>>::get().into(),179 coeff_frac: Perbill::zero(),180 negative: false,181 degree: 1,182 })183 }184}185186pub struct FeeCalculator<T>(PhantomData<T>);187impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {188 fn min_gas_price() -> (U256, Weight) {189 (190 <MinGasPriceOverride<T>>::get().into(),191 T::DbWeight::get().reads(1),192 )193 }194}