snowbridge_core/
pricing.rs

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