Skip to content

Commit

Permalink
cfg-types compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 2, 2024
1 parent ff438f2 commit 7feb76e
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 112 deletions.
2 changes: 1 addition & 1 deletion libs/types/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::Seconds;
use cfg_primitives::Seconds;
use frame_support::pallet_prelude::RuntimeDebug;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand Down
40 changes: 19 additions & 21 deletions libs/types/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::{Properties, Seconds, TimeAsSecs};
use cfg_primitives::Seconds;
use cfg_traits::{Properties, TimeAsSecs};
use frame_support::{traits::Get, BoundedVec};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -185,7 +186,7 @@ where
/// care which Seconds is passed to the PoolRole::TrancheInvestor(TrancheId,
/// Seconds) variant. This UNION shall reflect that and explain to the reader
/// why it is passed here.
pub const UNION: Seconds = 0;
pub const UNION: Seconds = Seconds::from(0);

impl<Now, MinDelay, TrancheId, MaxTranches> Properties
for PermissionRoles<Now, MinDelay, TrancheId, MaxTranches>
Expand Down Expand Up @@ -459,36 +460,33 @@ mod tests {
use super::*;

parameter_types! {
pub const MinDelay: u64 = 4;
pub const MinDelay: Seconds = Seconds::from(4);
pub const MaxTranches: u32 = 5;
}

struct Now;
impl Now {
fn pass(delta: u64) {
fn pass(delta: Seconds) {
unsafe {
let current = NOW_HOLDER;
NOW_HOLDER = current + delta;
NOW_HOLDER = current.add(delta);
};
}

fn set(now: u64) {
fn set(now: Seconds) {
unsafe {
NOW_HOLDER = now;
};
}
}

static mut NOW_HOLDER: u64 = 0;
static mut NOW_HOLDER: Seconds = Seconds::from(0);
impl frame_support::traits::UnixTime for Now {
fn now() -> Duration {
unsafe { Duration::new(NOW_HOLDER, 0) }
unsafe { Duration::new(NOW_HOLDER.get(), 0) }
}
}

/// The exists call does not care what is passed as moment. This type shall
/// reflect that
const UNION: u64 = 0u64;
/// The tranceh id type we use in our runtime-common. But we don't want a
/// dependency here.
type TrancheId = [u8; 16];
Expand All @@ -507,19 +505,19 @@ mod tests {
assert!(roles
.add(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(30),
10
Seconds::from(10)
)))
.is_ok());
assert!(roles
.add(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(30),
9
Seconds::from(9)
)))
.is_err());
assert!(roles
.add(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(30),
11
Seconds::from(11)
)))
.is_ok());

Expand All @@ -543,10 +541,10 @@ mod tests {
assert!(roles
.rm(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(0),
0
Seconds::from(0)
)))
.is_err());
Now::pass(1);
Now::pass(Seconds::from(1));
assert!(roles.exists(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(0),
UNION
Expand All @@ -561,7 +559,7 @@ mod tests {
into_tranche_id(0),
UNION
))));
Now::set(0);
Now::set(Seconds::from(0));

// Removing after MinDelay works (i.e. this is after min_delay the account will
// be invalid)
Expand All @@ -571,12 +569,12 @@ mod tests {
MinDelay::get()
)))
.is_ok());
Now::pass(6);
Now::pass(Seconds::from(6));
assert!(!roles.exists(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(0),
UNION
))));
Now::set(0);
Now::set(Seconds::from(0));

// Multiple tranches work
assert!(roles
Expand Down Expand Up @@ -624,12 +622,12 @@ mod tests {
into_tranche_id(8),
UNION
))));
Now::pass(1);
Now::pass(Seconds::from(1));
assert!(!roles.exists(Role::PoolRole(PoolRole::TrancheInvestor(
into_tranche_id(8),
UNION
))));
Now::set(0);
Now::set(Seconds::from(0));

// Role must be added for at least min_delay
assert!(roles
Expand Down
89 changes: 22 additions & 67 deletions libs/types/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::{
fee::{FeeAmountProration, PoolFeeBucket},
Seconds,
};
use cfg_primitives::Seconds;
use cfg_traits::fee::{FeeAmountProration, PoolFeeBucket};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_arithmetic::FixedPointOperand;
Expand Down Expand Up @@ -224,27 +222,15 @@ where
}
}

/// Converts an annual balance amount into its proratio based on the given
/// period duration.
pub fn saturated_balance_proration<
Balance: From<Seconds> + FixedPointOperand + sp_std::ops::Div<Output = Balance>,
>(
annual_amount: Balance,
period: Seconds,
) -> Balance {
let amount = annual_amount.saturating_mul(period.into());
amount.div(cfg_primitives::SECONDS_PER_YEAR.into())
}

/// Converts an annual rate into its proratio based on the given
/// period duration.
pub fn saturated_rate_proration<Rate: FixedPointNumberExtension>(
annual_rate: Rate,
period: Seconds,
) -> Rate {
let rate = annual_rate.saturating_mul(Rate::saturating_from_integer::<u64>(period));
let rate = annual_rate.saturating_mul(Rate::saturating_from_integer(period));

rate.saturating_div_ceil(&Rate::saturating_from_integer::<u64>(
rate.saturating_div_ceil(&Rate::saturating_from_integer(
cfg_primitives::SECONDS_PER_YEAR,
))
}
Expand All @@ -267,7 +253,7 @@ mod tests {
use super::*;

mod saturated_proration {
use cfg_primitives::{CFG, DAYS, SECONDS_PER_YEAR};
use cfg_primitives::{CFG, SECONDS_PER_DAY, SECONDS_PER_YEAR};
use sp_arithmetic::{
traits::{One, Zero},
FixedPointNumber,
Expand All @@ -278,49 +264,13 @@ mod tests {

type Balance = u128;

#[test]
fn balance_zero() {
assert_eq!(
saturated_balance_proration::<Balance>(SECONDS_PER_YEAR.into(), 0),
0
);
assert_eq!(
saturated_balance_proration::<Balance>(0u128, SECONDS_PER_YEAR),
0
);
assert_eq!(
saturated_balance_proration::<Balance>((SECONDS_PER_YEAR - 1).into(), 1),
0
);
assert_eq!(
saturated_balance_proration::<Balance>(1u128, SECONDS_PER_YEAR - 1),
0
);
}

#[test]
fn balance_one() {
assert_eq!(
saturated_balance_proration::<Balance>(SECONDS_PER_YEAR.into(), 1),
1u128
);
assert_eq!(
saturated_balance_proration::<Balance>(1u128, SECONDS_PER_YEAR),
1u128
);
}
#[test]
fn balance_overflow() {
assert_eq!(
saturated_balance_proration::<Balance>(u128::MAX, u64::MAX),
u128::MAX / u128::from(SECONDS_PER_YEAR)
);
}

#[test]
fn rate_zero() {
assert_eq!(
saturated_rate_proration::<Rate>(Rate::from_integer(SECONDS_PER_YEAR.into()), 0),
saturated_rate_proration::<Rate>(
Rate::from_integer(SECONDS_PER_YEAR.into()),
Seconds::from(0)
),
Rate::zero()
);
assert_eq!(
Expand All @@ -330,7 +280,7 @@ mod tests {
assert!(
saturated_rate_proration::<Rate>(
Rate::from_integer((SECONDS_PER_YEAR - 1).into()),
1
Seconds::from(1)
) > Rate::zero()
);
assert!(
Expand All @@ -341,7 +291,10 @@ mod tests {
#[test]
fn rate_one() {
assert_eq!(
saturated_rate_proration::<Rate>(Rate::from_integer(SECONDS_PER_YEAR.into()), 1),
saturated_rate_proration::<Rate>(
Rate::from_integer(SECONDS_PER_YEAR.into()),
Seconds::from(1)
),
Rate::one()
);
assert_eq!(
Expand All @@ -356,23 +309,25 @@ mod tests {

let rate = saturated_rate_proration::<Rate>(
Rate::from_integer(u128::from(u128::MAX / 10u128.pow(27))),
1,
Seconds::from(1),
);
assert!(left_bound < rate);
assert!(rate < right_bound);

assert!(saturated_rate_proration::<Rate>(Rate::one(), u64::MAX) > left_bound);
assert!(saturated_rate_proration::<Rate>(Rate::one(), u64::MAX) < right_bound);
assert!(saturated_rate_proration::<Rate>(Rate::one(), Seconds::MAX) > left_bound);
assert!(saturated_rate_proration::<Rate>(Rate::one(), Seconds::MAX) < right_bound);

assert!(saturated_rate_proration::<Rate>(Rate::from_integer(2), u64::MAX) > left_bound);
assert!(
saturated_rate_proration::<Rate>(Rate::from_integer(2), u64::MAX) < right_bound
saturated_rate_proration::<Rate>(Rate::from_integer(2), Seconds::MAX) > left_bound
);
assert!(
saturated_rate_proration::<Rate>(Rate::from_integer(2), Seconds::MAX) < right_bound
);
}

#[test]
fn precision_quantity_vs_rate() {
let period = (DAYS / 4) as Seconds;
let period = SECONDS_PER_DAY / 4;
let nav_multiplier = 1_000_000;
let nav = nav_multiplier * CFG;

Expand Down
5 changes: 3 additions & 2 deletions libs/types/src/portfolio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_traits::{Seconds, TimeAsSecs};
use cfg_primitives::Seconds;
use cfg_traits::TimeAsSecs;
use frame_support::{pallet_prelude::RuntimeDebug, traits::Get, BoundedVec};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -179,7 +180,7 @@ mod tests {

#[test]
fn general_usage() {
let mut portfolio = PortfolioValuation::<u128, u64, ConstU32<3>>::new(10);
let mut portfolio = PortfolioValuation::<u128, u64, ConstU32<3>>::new(Seconds::from(10));

assert_ok!(portfolio.insert_elem(1, 100));
assert_ok!(portfolio.insert_elem(2, 200));
Expand Down
Loading

0 comments on commit 7feb76e

Please sign in to comment.