pallet_on_demand_para/
types.rs1use crate::{
21 Config, Error, RelayBlockNumberOf, DEFAULT_BASE_FEE, DEFAULT_DRAIN_RATE_PER_BLOCK,
22 DEFAULT_ORDER_CAP, DEFAULT_PRICE_STEP,
23};
24use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
25use fp_coretime::TaskId;
26use frame_support::{
27 pallet_prelude::*,
28 traits::{fungible::Inspect, tokens::Balance},
29};
30use frame_system::Config as SConfig;
31use scale_info::TypeInfo;
32use sp_arithmetic::{
33 traits::{ensure_pow, One, Saturating},
34 FixedPointNumber, FixedU128, Perbill,
35};
36use sp_runtime::DispatchError;
37
38pub type BalanceOf<T> = <<T as Config>::Currency as Inspect<<T as SConfig>::AccountId>>::Balance;
39
40#[derive(
42 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
43)]
44pub struct PriceParameters<Balance> {
45 pub order_cap: u32,
47 pub drain_rate_per_block: u32,
51 pub price_step: Perbill,
53 pub base_fee: Balance,
55}
56
57impl<T> Default for PriceParameters<T>
58where
59 T: From<u32>,
60{
61 fn default() -> Self {
62 Self {
63 order_cap: DEFAULT_ORDER_CAP,
64 drain_rate_per_block: DEFAULT_DRAIN_RATE_PER_BLOCK,
65 price_step: Perbill::from_percent(DEFAULT_PRICE_STEP),
66 base_fee: T::from(DEFAULT_BASE_FEE),
67 }
68 }
69}
70
71impl<Balance> PriceParameters<Balance> {
72 pub(crate) fn validate<T>(&self) -> Result<(), Error<T>>
73 where
74 T: Config,
75 T::Currency: Inspect<<T as SConfig>::AccountId, Balance = Balance>,
76 Balance: frame_support::traits::tokens::Balance,
77 {
78 ensure!(
79 T::PricingProvider::spot_price(self, self.order_cap).is_ok(),
80 Error::<T>::OrderPriceCanOverflow
81 );
82 Ok(())
83 }
84}
85
86pub type PriceParametersOf<T> = PriceParameters<BalanceOf<T>>;
87
88#[derive(
90 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
91)]
92pub struct EnqueuedOrder<RelayBlockNumber> {
93 pub para_id: TaskId,
95 pub ordered_at: RelayBlockNumber,
97}
98
99#[derive(
101 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
102)]
103pub struct QueueTracker<RelayBlockNumber> {
104 pub outstanding_orders: u32,
106 pub last_updated: RelayBlockNumber,
108}
109
110pub type QueueTrackerOf<T> = QueueTracker<RelayBlockNumberOf<T>>;
111
112pub trait PoolCapacityProvider {
114 fn pool_cores() -> u32;
116
117 #[cfg(feature = "runtime-benchmarks")]
121 fn ensure_pool_cores(cores: u32);
122}
123
124pub trait PricingProvider<T: Balance> {
126 fn spot_price(price_config: &PriceParameters<T>, queue_depth: u32) -> Result<T, DispatchError>;
128}
129
130pub struct DefaultPricingProvider;
132
133impl<T: Balance> PricingProvider<T> for DefaultPricingProvider {
134 fn spot_price(price_config: &PriceParameters<T>, queue_depth: u32) -> Result<T, DispatchError> {
135 let price_adjustment = ensure_pow(
137 FixedU128::one().saturating_add(FixedU128::from_perbill(price_config.price_step)),
138 queue_depth as usize,
139 )?;
140 Ok(price_adjustment.saturating_mul_int(price_config.base_fee))
141 }
142}