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

difftreelog

source

pallets/configuration/src/lib.rs5.5 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 parity_scale_codec::{Decode, Encode, MaxEncodedLen};27use scale_info::TypeInfo;28use sp_arithmetic::traits::{BaseArithmetic, Unsigned};29use smallvec::smallvec;3031pub use pallet::*;32use sp_core::U256;33use sp_runtime::Perbill;3435#[pallet]36mod pallet {37	use super::*;38	use frame_support::{39		traits::Get,40		pallet_prelude::{StorageValue, ValueQuery, DispatchResult, OptionQuery},41		BoundedVec,42	};43	use frame_system::{pallet_prelude::OriginFor, ensure_root};44	use xcm::v1::MultiLocation;4546	#[pallet::config]47	pub trait Config: frame_system::Config {48		#[pallet::constant]49		type DefaultWeightToFeeCoefficient: Get<u32>;5051		#[pallet::constant]52		type DefaultMinGasPrice: Get<u64>;5354		#[pallet::constant]55		type MaxXcmAllowedLocations: Get<u32>;56		#[pallet::constant]57		type AppPromotionDailyRate: Get<Perbill>;58		#[pallet::constant]59		type DayRelayBlocks: Get<Self::BlockNumber>;60	}6162	#[pallet::error]63	pub enum Error<T> {64		InconsistentConfiguration,65	}6667	#[pallet::storage]68	pub type WeightToFeeCoefficientOverride<T: Config> = StorageValue<69		Value = u32,70		QueryKind = ValueQuery,71		OnEmpty = T::DefaultWeightToFeeCoefficient,72	>;7374	#[pallet::storage]75	pub type MinGasPriceOverride<T: Config> =76		StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = T::DefaultMinGasPrice>;7778	#[pallet::storage]79	pub type XcmAllowedLocationsOverride<T: Config> = StorageValue<80		Value = BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>,81		QueryKind = OptionQuery,82	>;8384	#[pallet::storage]85	pub type AppPromomotionConfigurationOverride<T: Config> =86		StorageValue<Value = AppPromotionConfiguration<T::BlockNumber>, QueryKind = ValueQuery>;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			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			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::MaxXcmAllowedLocations>>,122		) -> DispatchResult {123			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			mut configuration: AppPromotionConfiguration<T::BlockNumber>,132		) -> DispatchResult {133			ensure_root(origin)?;134			if configuration.interval_income.is_some() {135				return Err(<Error<T>>::InconsistentConfiguration.into());136			}137138			configuration.interval_income = configuration.recalculation_interval.map(|b| {139				Perbill::from_rational(b, T::DayRelayBlocks::get())140					* T::AppPromotionDailyRate::get()141			});142143			<AppPromomotionConfigurationOverride<T>>::set(configuration);144145			Ok(())146		}147	}148149	#[pallet::pallet]150	#[pallet::generate_store(pub(super) trait Store)]151	pub struct Pallet<T>(_);152}153154pub struct WeightToFee<T, B>(PhantomData<(T, B)>);155156impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>157where158	T: Config,159	B: BaseArithmetic + From<u32> + Copy + Unsigned,160{161	type Balance = B;162163	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {164		smallvec!(WeightToFeeCoefficient {165			coeff_integer: <WeightToFeeCoefficientOverride<T>>::get().into(),166			coeff_frac: Perbill::zero(),167			negative: false,168			degree: 1,169		})170	}171}172173pub struct FeeCalculator<T>(PhantomData<T>);174impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {175	fn min_gas_price() -> (U256, Weight) {176		(177			<MinGasPriceOverride<T>>::get().into(),178			T::DbWeight::get().reads(1),179		)180	}181}182183#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]184pub struct AppPromotionConfiguration<BlockNumber> {185	/// In relay blocks.186	pub recalculation_interval: Option<BlockNumber>,187	/// In parachain blocks.188	pub pending_interval: Option<BlockNumber>,189	/// Value for `RecalculationInterval` based on 0.05% per 24h.190	pub interval_income: Option<Perbill>,191	/// Maximum allowable number of stakers calculated per call of the `app-promotion::PayoutStakers` extrinsic.192	pub max_stakers_per_calculation: Option<u8>,193}