git.delta.rocks / unique-network / refs/commits / 0af2be845e4b

difftreelog

source

pallets/configuration/src/lib.rs5.7 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::{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::call_index(0)]92		#[pallet::weight(T::DbWeight::get().writes(1))]93		pub fn set_weight_to_fee_coefficient_override(94			origin: OriginFor<T>,95			coeff: Option<u64>,96		) -> DispatchResult {97			ensure_root(origin)?;98			if let Some(coeff) = coeff {99				<WeightToFeeCoefficientOverride<T>>::set(coeff);100			} else {101				<WeightToFeeCoefficientOverride<T>>::kill();102			}103			Ok(())104		}105106		#[pallet::call_index(1)]107		#[pallet::weight(T::DbWeight::get().writes(1))]108		pub fn set_min_gas_price_override(109			origin: OriginFor<T>,110			coeff: Option<u64>,111		) -> DispatchResult {112			ensure_root(origin)?;113			if let Some(coeff) = coeff {114				<MinGasPriceOverride<T>>::set(coeff);115			} else {116				<MinGasPriceOverride<T>>::kill();117			}118			Ok(())119		}120121		#[pallet::call_index(2)]122		#[pallet::weight(T::DbWeight::get().writes(1))]123		pub fn set_xcm_allowed_locations(124			origin: OriginFor<T>,125			locations: Option<BoundedVec<MultiLocation, T::MaxXcmAllowedLocations>>,126		) -> DispatchResult {127			ensure_root(origin)?;128			<XcmAllowedLocationsOverride<T>>::set(locations);129			Ok(())130		}131132		#[pallet::call_index(3)]133		#[pallet::weight(T::DbWeight::get().writes(1))]134		pub fn set_app_promotion_configuration_override(135			origin: OriginFor<T>,136			mut configuration: AppPromotionConfiguration<T::BlockNumber>,137		) -> DispatchResult {138			ensure_root(origin)?;139			if configuration.interval_income.is_some() {140				return Err(<Error<T>>::InconsistentConfiguration.into());141			}142143			configuration.interval_income = configuration.recalculation_interval.map(|b| {144				Perbill::from_rational(b, T::DayRelayBlocks::get())145					* T::AppPromotionDailyRate::get()146			});147148			<AppPromomotionConfigurationOverride<T>>::set(configuration);149150			Ok(())151		}152	}153154	#[pallet::pallet]155	#[pallet::generate_store(pub(super) trait Store)]156	pub struct Pallet<T>(_);157}158159pub struct WeightToFee<T, B>(PhantomData<(T, B)>);160161impl<T, B> WeightToFeePolynomial for WeightToFee<T, B>162where163	T: Config,164	B: BaseArithmetic + From<u32> + From<u64> + Copy + Unsigned,165{166	type Balance = B;167168	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {169		smallvec!(WeightToFeeCoefficient {170			coeff_integer: (<WeightToFeeCoefficientOverride<T>>::get() / Perbill::ACCURACY as u64)171				.into(),172			coeff_frac: Perbill::from_parts(173				(<WeightToFeeCoefficientOverride<T>>::get() % Perbill::ACCURACY as u64) as u32174			),175			negative: false,176			degree: 1,177		})178	}179}180181pub struct FeeCalculator<T>(PhantomData<T>);182impl<T: Config> fp_evm::FeeCalculator for FeeCalculator<T> {183	fn min_gas_price() -> (U256, Weight) {184		(185			<MinGasPriceOverride<T>>::get().into(),186			T::DbWeight::get().reads(1),187		)188	}189}190191#[derive(Encode, Decode, Clone, Debug, Default, TypeInfo, MaxEncodedLen, PartialEq, PartialOrd)]192pub struct AppPromotionConfiguration<BlockNumber> {193	/// In relay blocks.194	pub recalculation_interval: Option<BlockNumber>,195	/// In parachain blocks.196	pub pending_interval: Option<BlockNumber>,197	/// Value for `RecalculationInterval` based on 0.05% per 24h.198	pub interval_income: Option<Perbill>,199	/// Maximum allowable number of stakers calculated per call of the `app-promotion::PayoutStakers` extrinsic.200	pub max_stakers_per_calculation: Option<u8>,201}