Skip to content

Commit

Permalink
pallet-interest-accrual ported
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 5, 2024
1 parent bd654e2 commit 40cd54b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
3 changes: 3 additions & 0 deletions pallets/interest-accrual/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cfg-types = { workspace = true }

[dev-dependencies]
bitflags = { workspace = true }
cfg-mocks = { workspace = true }

[features]
default = ["std"]
Expand Down Expand Up @@ -60,6 +61,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
]
try-runtime = [
"cfg-primitives/try-runtime",
Expand All @@ -69,4 +71,5 @@ try-runtime = [
"frame-system/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
"cfg-mocks/try-runtime",
]
4 changes: 2 additions & 2 deletions pallets/interest-accrual/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ benchmarks! {
// and returns a reasonably-precise weight for the pow.
calculate_accumulated_rate {
let n in 1..25;
let now: Seconds = (1 << n) - 1;
let now = Seconds::from((1 << n) - 1);
let rate = interest_rate_per_sec(T::Rate::saturating_from_rational(10, 100)).unwrap();
}: { Pallet::<T>::calculate_accumulated_rate(rate, One::one(), 0, now).unwrap() }
}: { Pallet::<T>::calculate_accumulated_rate(rate, One::one(), Zero::zero(), now).unwrap() }
verify {
}
}
Expand Down
14 changes: 6 additions & 8 deletions pallets/interest-accrual/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@
#![cfg_attr(not(feature = "std"), no_std)]

use cfg_primitives::SECONDS_PER_YEAR;
use cfg_primitives::{Seconds, SECONDS_PER_YEAR};
use cfg_traits::{
interest::{InterestAccrual, InterestRate, RateCollection},
Seconds, TimeAsSecs,
time::UnixTimeSecs,
};
use cfg_types::adjustments::Adjustment;
use frame_support::{pallet_prelude::RuntimeDebug, BoundedVec};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_arithmetic::traits::{checked_pow, One, Zero};
use sp_arithmetic::traits::{ensure_pow, One, Zero};
use sp_runtime::{
traits::{
AtLeast32BitUnsigned, CheckedAdd, CheckedSub, EnsureAdd, EnsureAddAssign, EnsureDiv,
Expand Down Expand Up @@ -211,7 +211,7 @@ pub mod pallet {
+ FixedPointNumber<Inner = Self::Balance>
+ MaxEncodedLen;

type Time: TimeAsSecs;
type Time: UnixTimeSecs;

type MaxRateCount: Get<u32>;

Expand Down Expand Up @@ -315,8 +315,7 @@ pub mod pallet {
Ordering::Less => {
let delta = now.ensure_sub(when)?;
let rate_adjustment =
checked_pow(rate.interest_rate_per_sec, delta.ensure_into()?)
.ok_or(ArithmeticError::Overflow)?;
ensure_pow(rate.interest_rate_per_sec, delta.ensure_into()?)?;
rate.accumulated_rate.ensure_div(rate_adjustment)?
}
Ordering::Greater => {
Expand Down Expand Up @@ -389,8 +388,7 @@ pub mod pallet {
) -> Result<Rate, ArithmeticError> {
// accumulated_rate * interest_rate_per_sec ^ (now - last_updated)
let time_difference_secs = now.ensure_sub(last_updated)?;
checked_pow(interest_rate_per_sec, time_difference_secs as usize)
.ok_or(ArithmeticError::Overflow)? // TODO: This line can be remove once #1241 be merged
ensure_pow(interest_rate_per_sec, time_difference_secs.ensure_into()?)?
.ensure_mul(accumulated_rate)
}

Expand Down
36 changes: 11 additions & 25 deletions pallets/interest-accrual/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use cfg_traits::Millis;
use frame_support::{derive_impl, parameter_types, traits::Hooks};
use sp_io::TestExternalities;
use sp_runtime::BuildStorage;
use frame_support::{derive_impl, parameter_types};

use crate::*;

pub const START_DATE: Seconds = Seconds::from(1640995200);

pub type Balance = u128;
pub type Rate = sp_arithmetic::fixed_point::FixedU128;

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
Timestamp: pallet_timestamp,
Timer: cfg_mocks::time::pallet,
InterestAccrual: crate,
}
);
Expand All @@ -21,11 +20,8 @@ impl frame_system::Config for Runtime {
type Block = frame_system::mocking::MockBlock<Runtime>;
}

impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = ();
type Moment = Millis;
type OnTimestampSet = ();
type WeightInfo = ();
impl cfg_mocks::time::pallet::Config for Runtime {
type Moment = Seconds;
}

parameter_types! {
Expand All @@ -37,25 +33,15 @@ impl Config for Runtime {
type MaxRateCount = MaxRateCount;
type Rate = Rate;
type RuntimeEvent = RuntimeEvent;
type Time = Timestamp;
type Time = Timer;
type Weights = ();
}

#[allow(unused)]
pub fn new_test_ext() -> sp_io::TestExternalities {
const SECONDS: u64 = 1000;
const START_DATE: u64 = 1640995200;

let storage = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();

let mut externalities = TestExternalities::new(storage);
externalities.execute_with(|| {
System::set_block_number(1);
System::on_initialize(System::block_number());
Timestamp::on_initialize(System::block_number());
Timestamp::set(RuntimeOrigin::none(), START_DATE * SECONDS).unwrap();
let mut ext = System::externalities();
ext.execute_with(|| {
Timer::mock_now(|| START_DATE);
});
externalities
ext
}

0 comments on commit 40cd54b

Please sign in to comment.