1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819use core::marker::PhantomData;2021use frame_support::{22 pallet,23 weights::{WeightToFeePolynomial, WeightToFeeCoefficients, WeightToFeeCoefficient},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},39 };40 use frame_system::{pallet_prelude::OriginFor, ensure_root};4142 #[pallet::config]43 pub trait Config: frame_system::Config {44 #[pallet::constant]45 type DefaultWeightToFeeCoefficient: Get<u32>;46 #[pallet::constant]47 type DefaultMinGasPrice: Get<u64>;48 }4950 #[pallet::storage]51 pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<52 Value = u32,53 QueryKind = ValueQuery,54 OnEmpty = T::DefaultWeightToFeeCoefficient,55 >;5657 #[pallet::storage]58 pub type MinGasPriceOverride<T: Config> =59 StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;6061 #[pallet::call]62 impl<T: Config> Pallet<T> {63 #[pallet::weight(T::DbWeight::get().writes(1))]64 pub fn set_weight_to_fee_coefficient_override(65 origin: OriginFor<T>,66 coeff: Option<u32>,67 ) -> DispatchResult {68 let _sender = ensure_root(origin)?;69 if let Some(coeff) = coeff {70 <WeightToFeeCoefficientOverride<T>>::set(coeff);71 } else {72 <WeightToFeeCoefficientOverride<T>>::kill();73 }74 Ok(())75 }7677 #[pallet::weight(T::DbWeight::get().writes(1))]78 pub fn set_min_gas_price_override(79 origin: OriginFor<T>,80 coeff: Option<u64>,81 ) -> DispatchResult {82 let _sender = ensure_root(origin)?;83 if let Some(coeff) = coeff {84 <MinGasPriceOverride<T>>::set(coeff);85 } else {86 <MinGasPriceOverride<T>>::kill();87 }88 Ok(())89 }90 }9192 #[pallet::pallet]93 #[pallet::generate_store(pub(super) trait Store)]94 pub struct Pallet<T>(_);95}9697pub struct WeightToFee<T, B>(PhantomData<(T, B)>);9899impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>100where101 T: Config,102 B: BaseArithmetic + From<u32> + Copy + Unsigned,103{104 type Balance = B;105106 fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {107 smallvec!(WeightToFeeCoefficient {108 coeff_integer: <WeightToFeeCoefficientOverride<T>>::get().into(),109 coeff_frac: Perbill::zero(),110 negative: false,111 degree: 1,112 })113 }114}115116pub struct FeeCalculator<T>(PhantomData<T>);117impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {118 fn min_gas_price() -> (U256, u64) {119 (120 <MinGasPriceOverride<T>>::get().into(),121 T::DbWeight::get().reads(1),122 )123 }124}