Skip to content

Commit

Permalink
Loans: Moment as Seconds & Millis explicitly (#1575)
Browse files Browse the repository at this point in the history
* base implementation for loans

* implementation for interest-accrual

* fix pallet-loans

* implementation for pool-system & tests

* modify pool permission types to use seconds

* liquidity-pools updated

* fix errors

* minor fix

* minor fix

* fix clippy

* fix clippy

* update integration-test generic
  • Loading branch information
lemunozm authored Oct 16, 2023
1 parent b8effb0 commit 470298d
Show file tree
Hide file tree
Showing 52 changed files with 388 additions and 391 deletions.
9 changes: 4 additions & 5 deletions libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_primitives::Moment;
use cfg_traits::{PoolInspect, PoolReserve, PriceValue, TrancheTokenPrice};
use cfg_traits::{PoolInspect, PoolReserve, PriceValue, Seconds, TrancheTokenPrice};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
Expand Down Expand Up @@ -85,7 +84,7 @@ pub mod pallet {
}

impl<T: Config> PoolInspect<T::AccountId, T::CurrencyId> for Pallet<T> {
type Moment = Moment;
type Moment = Seconds;
type PoolId = T::PoolId;
type TrancheId = T::TrancheId;

Expand All @@ -108,14 +107,14 @@ pub mod pallet {

impl<T: Config> TrancheTokenPrice<T::AccountId, T::CurrencyId> for Pallet<T> {
type BalanceRatio = T::BalanceRatio;
type Moment = Moment;
type Moment = Seconds;
type PoolId = T::PoolId;
type TrancheId = T::TrancheId;

fn get(
a: T::PoolId,
b: T::TrancheId,
) -> Option<PriceValue<T::CurrencyId, T::BalanceRatio, Moment>> {
) -> Option<PriceValue<T::CurrencyId, T::BalanceRatio, Seconds>> {
execute_call!((a, b))
}
}
Expand Down
3 changes: 0 additions & 3 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ pub mod types {
/// Aura consensus authority.
pub type AuraId = sp_consensus_aura::sr25519::AuthorityId;

/// Moment type
pub type Moment = u64;

// A vector of bytes, conveniently named like it is in Solidity.
pub type Bytes = Vec<u8>;

Expand Down
11 changes: 5 additions & 6 deletions libs/test-utils/src/mocks/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use cfg_primitives::Moment;
use cfg_traits::PoolNAV;
use cfg_traits::{PoolNAV, Seconds};
use codec::HasCompact;
use frame_support::pallet_prelude::*;
use sp_runtime::traits::{AtLeast32BitUnsigned, Zero};
Expand All @@ -40,7 +39,7 @@ pub mod pallet {
pub struct Pallet<T>(_);

#[pallet::storage]
pub type Nav<T: Config> = StorageMap<_, Blake2_128Concat, T::PoolId, (T::Balance, Moment)>;
pub type Nav<T: Config> = StorageMap<_, Blake2_128Concat, T::PoolId, (T::Balance, Seconds)>;

impl<T: Config> Pallet<T> {
pub fn value(pool_id: T::PoolId) -> T::Balance {
Expand All @@ -49,11 +48,11 @@ pub mod pallet {
.unwrap_or_else(T::Balance::zero)
}

pub fn update(pool_id: T::PoolId, balance: T::Balance, now: Moment) {
pub fn update(pool_id: T::PoolId, balance: T::Balance, now: Seconds) {
Nav::<T>::insert(pool_id, (balance, now));
}

pub fn latest(pool_id: T::PoolId) -> (T::Balance, Moment) {
pub fn latest(pool_id: T::PoolId) -> (T::Balance, Seconds) {
Nav::<T>::get(pool_id).unwrap_or((T::Balance::zero(), 0))
}
}
Expand All @@ -62,7 +61,7 @@ pub mod pallet {
type ClassId = T::ClassId;
type RuntimeOrigin = T::RuntimeOrigin;

fn nav(pool_id: T::PoolId) -> Option<(T::Balance, Moment)> {
fn nav(pool_id: T::PoolId) -> Option<(T::Balance, Seconds)> {
Some(Self::latest(pool_id))
}

Expand Down
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: 30 additions & 0 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
dispatch::{Codec, DispatchResult, DispatchResultWithPostInfo},
scale_info::TypeInfo,
traits::UnixTime,
Parameter, RuntimeDebug,
};
use impl_trait_for_tuples::impl_for_tuples;
Expand Down Expand Up @@ -631,3 +632,32 @@ pub trait ConversionFromAssetBalance<AssetBalance, AssetId, OutBalance> {
asset_id: AssetId,
) -> Result<OutBalance, Self::Error>;
}

// TODO: Probably these should be in a future cfg-utils.
// Issue: https://github.com/centrifuge/centrifuge-chain/issues/1380

/// Type to represent milliseconds
pub type Millis = u64;

/// Type to represent seconds
pub type Seconds = u64;

/// Trait to obtain the time as seconds
pub trait TimeAsSecs: UnixTime {
fn now() -> Seconds {
<Self as UnixTime>::now().as_secs()
}
}

impl<T: UnixTime> TimeAsSecs for T {}

/// Trait to convert into seconds
pub trait IntoSeconds {
fn into_seconds(self) -> Seconds;
}

impl IntoSeconds for Millis {
fn into_seconds(self) -> Seconds {
self / 1000
}
}
4 changes: 2 additions & 2 deletions 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_primitives::Moment;
use cfg_traits::Seconds;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
Expand All @@ -21,7 +21,7 @@ pub struct EpochState<EpochId> {
/// Current epoch that is ongoing.
pub current: EpochId,
/// Time when the last epoch was closed.
pub last_closed: Moment,
pub last_closed: Seconds,
/// Last epoch that was executed.
pub last_executed: EpochId,
}
Expand Down
Loading

0 comments on commit 470298d

Please sign in to comment.