git.delta.rocks / unique-network / refs/commits / c8f3a189a783

difftreelog

source

pallets/configuration/src/lib.rs3.9 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![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	}5556	#[pallet::storage]57	pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<58		Value = u32,59		QueryKind = ValueQuery,60		OnEmpty = T::DefaultWeightToFeeCoefficient,61	>;6263	#[pallet::storage]64	pub type MinGasPriceOverride<T: Config> =65		StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;6667	#[pallet::storage]68	pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<69		Value = BoundedVec<MultiLocation, T::MaxOverridedAllowedLocations>,70		QueryKind = OptionQuery,71	>;7273	#[pallet::call]74	impl<T: Config> Pallet<T> {75		#[pallet::weight(T::DbWeight::get().writes(1))]76		pub fn set_weight_to_fee_coefficient_override(77			origin: OriginFor<T>,78			coeff: Option<u32>,79		) -> DispatchResult {80			let _sender = ensure_root(origin)?;81			if let Some(coeff) = coeff {82				<WeightToFeeCoefficientOverride<T>>::set(coeff);83			} else {84				<WeightToFeeCoefficientOverride<T>>::kill();85			}86			Ok(())87		}8889		#[pallet::weight(T::DbWeight::get().writes(1))]90		pub fn set_min_gas_price_override(91			origin: OriginFor<T>,92			coeff: Option<u64>,93		) -> DispatchResult {94			let _sender = ensure_root(origin)?;95			if let Some(coeff) = coeff {96				<MinGasPriceOverride<T>>::set(coeff);97			} else {98				<MinGasPriceOverride<T>>::kill();99			}100			Ok(())101		}102103		#[pallet::weight(T::DbWeight::get().writes(1))]104		pub fn set_xcm_allowed_locations(105			origin: OriginFor<T>,106			locations: Option<BoundedVec<MultiLocation, T::MaxOverridedAllowedLocations>>,107		) -> DispatchResult {108			let _sender = ensure_root(origin)?;109			<XcmAllowedLocationsOverride<T>>::set(locations);110			Ok(())111		}112	}113114	#[pallet::pallet]115	#[pallet::generate_store(pub(super) trait Store)]116	pub struct Pallet<T>(_);117}118119pub struct WeightToFee<T, B>(PhantomData<(T, B)>);120121impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>122where123	T: Config,124	B: BaseArithmetic + From<u32> + Copy + Unsigned,125{126	type Balance = B;127128	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {129		smallvec!(WeightToFeeCoefficient {130			coeff_integer: <WeightToFeeCoefficientOverride<T>>::get().into(),131			coeff_frac: Perbill::zero(),132			negative: false,133			degree: 1,134		})135	}136}137138pub struct FeeCalculator<T>(PhantomData<T>);139impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {140	fn min_gas_price() -> (U256, Weight) {141		(142			<MinGasPriceOverride<T>>::get().into(),143			T::DbWeight::get().reads(1),144		)145	}146}