referrerpolicy=no-referrer-when-downgrade

snowbridge_core/
pricing.rs

1use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
2use scale_info::TypeInfo;
3use sp_arithmetic::traits::{BaseArithmetic, Unsigned, Zero};
4use sp_core::U256;
5use sp_runtime::{FixedU128, RuntimeDebug};
6use sp_std::prelude::*;
7
8#[derive(
9	Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo,
10)]
11pub struct PricingParameters<Balance> {
12	/// ETH/DOT exchange rate
13	pub exchange_rate: FixedU128,
14	/// Relayer rewards
15	pub rewards: Rewards<Balance>,
16	/// Ether (wei) fee per gas unit
17	pub fee_per_gas: U256,
18	/// Fee multiplier
19	pub multiplier: FixedU128,
20}
21
22#[derive(
23	Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo,
24)]
25pub struct Rewards<Balance> {
26	/// Local reward in DOT
27	pub local: Balance,
28	/// Remote reward in ETH (wei)
29	pub remote: U256,
30}
31
32#[derive(RuntimeDebug)]
33pub struct InvalidPricingParameters;
34
35impl<Balance> PricingParameters<Balance>
36where
37	Balance: BaseArithmetic + Unsigned + Copy,
38{
39	pub fn validate(&self) -> Result<(), InvalidPricingParameters> {
40		if self.exchange_rate == FixedU128::zero() {
41			return Err(InvalidPricingParameters)
42		}
43		if self.fee_per_gas == U256::zero() {
44			return Err(InvalidPricingParameters)
45		}
46		if self.rewards.local.is_zero() {
47			return Err(InvalidPricingParameters)
48		}
49		if self.rewards.remote.is_zero() {
50			return Err(InvalidPricingParameters)
51		}
52		if self.multiplier == FixedU128::zero() {
53			return Err(InvalidPricingParameters)
54		}
55		Ok(())
56	}
57}
58
59/// Holder for fixed point number implemented in <https://github.com/PaulRBerg/prb-math>
60#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
61#[cfg_attr(feature = "std", derive(PartialEq))]
62pub struct UD60x18(U256);
63
64impl From<FixedU128> for UD60x18 {
65	fn from(value: FixedU128) -> Self {
66		// Both FixedU128 and UD60x18 have 18 decimal places
67		let inner: u128 = value.into_inner();
68		UD60x18(inner.into())
69	}
70}
71
72impl UD60x18 {
73	pub fn into_inner(self) -> U256 {
74		self.0
75	}
76}