use crate::constants::currency::CENTS;
use frame_support::{
sp_runtime::Perbill,
weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial},
};
use common_primitives::node::Balance;
use super::weights::extrinsic_weights::ExtrinsicBaseWeight;
use smallvec::smallvec;
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
#[cfg(test)]
mod test {
use super::WeightToFee;
use crate::{
constants::{
currency::{CENTS, DOLLARS, MILLICENTS},
MAXIMUM_BLOCK_WEIGHT,
},
fee::Balance,
weights::extrinsic_weights::ExtrinsicBaseWeight,
};
use frame_support::weights::WeightToFee as WeightToFeeT;
#[test]
fn full_block_fee_is_correct() {
let full_block = WeightToFee::weight_to_fee(&MAXIMUM_BLOCK_WEIGHT);
assert!(full_block >= 2 * 150 * CENTS);
assert!(full_block <= 50 * DOLLARS);
}
#[test]
fn extrinsic_base_fee_is_correct() {
let x = WeightToFee::weight_to_fee(&ExtrinsicBaseWeight::get());
let y = CENTS / 10;
assert!(x.max(y) - x.min(y) < MILLICENTS);
}
#[test]
fn check_weight() {
let p = CENTS / 10;
let q = Balance::from(ExtrinsicBaseWeight::get().ref_time());
assert_eq!(p, 100_000);
assert!(q >= 50_000_000);
assert!(q <= 100_000_000);
assert_eq!(p / q, Balance::from(0u128));
}
}