Skip to content

Commit

Permalink
implementation for interest-accrual
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Oct 9, 2023
1 parent 8e47ea2 commit 3892c27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
6 changes: 4 additions & 2 deletions libs/traits/src/interest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cfg_primitives::{Moment, SECONDS_PER_YEAR};
use cfg_primitives::SECONDS_PER_YEAR;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchResult, scale_info::TypeInfo, Parameter, RuntimeDebug};
use sp_arithmetic::{
Expand All @@ -10,6 +10,8 @@ use sp_runtime::{
DispatchError,
};

use crate::Seconds;

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub enum CompoundingSchedule {
/// Interest compounds every second
Expand Down Expand Up @@ -80,7 +82,7 @@ pub trait InterestAccrual<Rate, Balance, Adjustment> {
fn calculate_debt(
interest_rate: &InterestRate<Rate>,
normalized_debt: Self::NormalizedDebt,
when: Moment,
when: Seconds,
) -> Result<Balance, DispatchError>;

/// Increase or decrease the normalized debt
Expand Down
30 changes: 15 additions & 15 deletions pallets/interest-accrual/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@
//! regarding zero-age rates.
#![cfg_attr(not(feature = "std"), no_std)]
use cfg_primitives::{Moment, SECONDS_PER_YEAR};
use cfg_traits::interest::{InterestAccrual, InterestRate, RateCollection};

use cfg_primitives::SECONDS_PER_YEAR;
use cfg_traits::{
interest::{InterestAccrual, InterestRate, RateCollection},
Seconds, TimeAsSecs,
};
use cfg_types::adjustments::Adjustment;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{traits::UnixTime, BoundedVec, RuntimeDebug};
use frame_support::{BoundedVec, RuntimeDebug};
use scale_info::TypeInfo;
use sp_arithmetic::traits::{checked_pow, One, Zero};
use sp_runtime::{
Expand Down Expand Up @@ -207,7 +211,7 @@ pub mod pallet {
+ FixedPointNumber<Inner = Self::Balance>
+ MaxEncodedLen;

type Time: UnixTime;
type Time: TimeAsSecs;

type MaxRateCount: Get<u32>;

Expand All @@ -221,7 +225,7 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn last_updated)]
pub(super) type LastUpdated<T: Config> = StorageValue<_, Moment, ValueQuery>;
pub(super) type LastUpdated<T: Config> = StorageValue<_, Seconds, ValueQuery>;

#[pallet::event]
pub enum Event<T: Config> {}
Expand All @@ -244,10 +248,10 @@ pub mod pallet {
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight {
let then = LastUpdated::<T>::get();
let now = Self::now();
let now = T::Time::now();
LastUpdated::<T>::set(now);
let delta = now - then;
let bits = Moment::BITS - delta.leading_zeros();
let bits = Seconds::BITS - delta.leading_zeros();

// reads: timestamp, last updated, rates vec
// writes: last updated, rates vec
Expand Down Expand Up @@ -301,7 +305,7 @@ pub mod pallet {
pub fn get_debt(
interest_rate_per_year: &InterestRate<T::Rate>,
normalized_debt: T::Balance,
when: Moment,
when: Seconds,
) -> Result<T::Balance, DispatchError> {
let rate = Self::get_rate(interest_rate_per_year)?;
let now = LastUpdated::<T>::get();
Expand Down Expand Up @@ -380,8 +384,8 @@ pub mod pallet {
pub fn calculate_accumulated_rate<Rate: FixedPointNumber>(
interest_rate_per_sec: Rate,
accumulated_rate: Rate,
last_updated: Moment,
now: Moment,
last_updated: Seconds,
now: Seconds,
) -> Result<Rate, ArithmeticError> {
// accumulated_rate * interest_rate_per_sec ^ (now - last_updated)
let time_difference_secs = now.ensure_sub(last_updated)?;
Expand All @@ -390,10 +394,6 @@ pub mod pallet {
.ensure_mul(accumulated_rate)
}

pub fn now() -> Moment {
T::Time::now().as_secs()
}

pub fn reference_interest_rate(
interest_rate_per_year: &InterestRate<T::Rate>,
) -> DispatchResult {
Expand Down Expand Up @@ -493,7 +493,7 @@ impl<T: Config> InterestAccrual<T::Rate, T::Balance, Adjustment<T::Balance>> for
fn calculate_debt(
interest_rate_per_year: &InterestRate<T::Rate>,
normalized_debt: Self::NormalizedDebt,
when: Moment,
when: Seconds,
) -> Result<T::Balance, DispatchError> {
Pallet::<T>::get_debt(interest_rate_per_year, normalized_debt, when)
}
Expand Down

0 comments on commit 3892c27

Please sign in to comment.