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,
50 pub price_step: Perbill,
52 pub base_fee: Balance,
54}
55
56impl<T> Default for PriceParameters<T>
57where
58 T: From<u32>,
59{
60 fn default() -> Self {
61 Self {
62 order_cap: DEFAULT_ORDER_CAP,
63 drain_rate_per_block: DEFAULT_DRAIN_RATE_PER_BLOCK,
64 price_step: Perbill::from_percent(DEFAULT_PRICE_STEP),
65 base_fee: T::from(DEFAULT_BASE_FEE),
66 }
67 }
68}
69
70impl<Balance> PriceParameters<Balance> {
71 pub(crate) fn validate<T>(&self) -> Result<(), Error<T>>
72 where
73 T: Config,
74 T::Currency: Inspect<<T as SConfig>::AccountId, Balance = Balance>,
75 Balance: frame_support::traits::tokens::Balance,
76 {
77 ensure!(
78 T::PricingProvider::spot_price(self, self.order_cap).is_ok(),
79 Error::<T>::OrderPriceCanOverflow
80 );
81 Ok(())
82 }
83}
84
85pub type PriceParametersOf<T> = PriceParameters<BalanceOf<T>>;
86
87#[derive(
89 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
90)]
91pub struct EnqueuedOrder<RelayBlockNumber> {
92 pub para_id: TaskId,
94 pub ordered_at: RelayBlockNumber,
96}
97
98#[derive(
100 Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
101)]
102pub struct QueueTracker<RelayBlockNumber> {
103 pub outstanding_orders: u32,
105 pub last_updated: RelayBlockNumber,
107}
108
109pub type QueueTrackerOf<T> = QueueTracker<RelayBlockNumberOf<T>>;
110
111pub trait PoolCapacityProvider {
113 fn pool_cores() -> u32;
115}
116
117pub trait PricingProvider<T: Balance> {
119 fn spot_price(price_config: &PriceParameters<T>, queue_depth: u32) -> Result<T, DispatchError>;
121}
122
123pub struct DefaultPricingProvider;
125
126impl<T: Balance> PricingProvider<T> for DefaultPricingProvider {
127 fn spot_price(price_config: &PriceParameters<T>, queue_depth: u32) -> Result<T, DispatchError> {
128 let price_adjustment = ensure_pow(
130 FixedU128::one().saturating_add(FixedU128::from_perbill(price_config.price_step)),
131 queue_depth as usize,
132 )?;
133 Ok(price_adjustment.saturating_mul_int(price_config.base_fee))
134 }
135}