Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loans: Moment as Seconds & Millis explicitly #1575

Merged
merged 14 commits into from
Oct 16, 2023
Prev Previous commit
Next Next commit
liquidity-pools updated
  • Loading branch information
lemunozm committed Oct 9, 2023
commit 5b0aff1ba51c0f09854fa39b54d6713572818075
3 changes: 2 additions & 1 deletion pallets/liquidity-pools/src/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use cfg_traits::{
investments::ForeignInvestment, liquidity_pools::OutboundQueue, Permissions, PoolInspect,
TimeAsSecs,
};
use cfg_types::{
domain_address::{Domain, DomainAddress},
Expand Down Expand Up @@ -69,7 +70,7 @@ where
T::Permission::has(
PermissionScope::Pool(pool_id),
receiver.clone(),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Self::now())),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, T::Time::now())),
),
Error::<T>::UnauthorizedTransfer
);
Expand Down
19 changes: 7 additions & 12 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,17 @@ pub type GeneralCurrencyIndexOf<T> =

#[frame_support::pallet]
pub mod pallet {
use cfg_primitives::Moment;
use cfg_traits::{
investments::{ForeignInvestment, TrancheCurrency},
CurrencyInspect, Permissions, PoolInspect, TrancheTokenPrice,
CurrencyInspect, Permissions, PoolInspect, Seconds, TimeAsSecs, TrancheTokenPrice,
};
use cfg_types::{
permissions::{PermissionScope, PoolRole, Role},
tokens::{CustomMetadata, LiquidityPoolsWrappedToken},
EVMChainId,
};
use codec::HasCompact;
use frame_support::{pallet_prelude::*, traits::UnixTime};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::{traits::Zero, DispatchError};
use xcm::latest::MultiLocation;
Expand Down Expand Up @@ -200,13 +199,13 @@ pub mod pallet {
type Permission: Permissions<
Self::AccountId,
Scope = PermissionScope<Self::PoolId, CurrencyIdOf<Self>>,
Role = Role<Self::TrancheId, Moment>,
Role = Role<Self::TrancheId>,
Error = DispatchError,
>;

/// The UNIX timestamp provider type required for checking the validity
/// of investments.
type Time: UnixTime;
type Time: TimeAsSecs;

/// The type for handling transfers, burning and minting of
/// multi-assets.
Expand Down Expand Up @@ -490,7 +489,7 @@ pub mod pallet {
pool_id: T::PoolId,
tranche_id: T::TrancheId,
domain_address: DomainAddress,
valid_until: Moment,
valid_until: Seconds,
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

Expand All @@ -503,7 +502,7 @@ pub mod pallet {
Error::<T>::TrancheNotFound
);
ensure!(
valid_until > Self::now(),
valid_until > T::Time::now(),
Error::<T>::InvalidTrancheInvestorValidity
);

Expand Down Expand Up @@ -554,7 +553,7 @@ pub mod pallet {
T::Permission::has(
PermissionScope::Pool(pool_id),
T::DomainAddressToAccountId::convert(domain_address.clone()),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, Self::now()))
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, T::Time::now()))
),
Error::<T>::UnauthorizedTransfer
);
Expand Down Expand Up @@ -811,10 +810,6 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub(crate) fn now() -> Moment {
T::Time::now().as_secs()
}

/// Returns the `u128` general index of a currency as the concatenation
/// of the configured `GeneralCurrencyPrefix` and its local currency
/// identifier.
Expand Down
25 changes: 12 additions & 13 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ impl pallet_randomness_collective_flip::Config for Runtime {}
impl parachain_info::Config for Runtime {}

parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
pub const MinimumPeriod: Millis = SLOT_DURATION / 2;
}
impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = MinimumPeriod;
/// A timestamp: milliseconds since the unix epoch.
type Moment = Moment;
type Moment = Millis;
type OnTimestampSet = Aura;
type WeightInfo = weights::pallet_timestamp::WeightInfo<Runtime>;
}
Expand Down Expand Up @@ -1017,7 +1017,7 @@ parameter_types! {

// How much time should lapse before a tranche investor can be removed
#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MinDelay: Moment = 7 * SECONDS_PER_DAY;
pub const MinDelay: Seconds = 7 * SECONDS_PER_DAY;

#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MaxRolesPerPool: u32 = 10_000;
Expand All @@ -1027,29 +1027,28 @@ impl pallet_permissions::Config for Runtime {
type AdminOrigin = EnsureRootOr<HalfOfCouncil>;
type Editors = Editors;
type MaxRolesPerScope = MaxRolesPerPool;
type Role = Role<TrancheId, Moment>;
type Role = Role<TrancheId>;
type RuntimeEvent = RuntimeEvent;
type Scope = PermissionScope<PoolId, CurrencyId>;
type Storage =
PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches, Moment>;
type Storage = PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches>;
type WeightInfo = weights::pallet_permissions::WeightInfo<Self>;
}

pub struct Editors;
impl
Contains<(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
)> for Editors
{
fn contains(
t: &(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
),
) -> bool {
let (_editor, maybe_role, _pool, role) = t;
Expand Down Expand Up @@ -1258,7 +1257,7 @@ parameter_types! {
#[derive(scale_info::TypeInfo)]
pub const MaxGroups: u32 = 20;
pub const LiquidityRewardsPalletId: PalletId = cfg_types::ids::LIQUIDITY_REWARDS_PALLET_ID;
pub const InitialEpochDuration: Moment = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
pub const InitialEpochDuration: Millis = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
}

impl pallet_rewards::mechanism::gap::Config for Runtime {
Expand Down Expand Up @@ -1355,7 +1354,7 @@ impl pallet_data_collector::Config for Runtime {
runtime_common::oracle::DataProviderBridge<PriceOracle, OrmlAssetRegistry, PoolSystem>;
type MaxCollectionSize = MaxCollectionSize;
type MaxCollections = MaxPoolsWithExternalPrices;
type Moment = Moment;
type Moment = Millis;
}

parameter_types! {
Expand Down Expand Up @@ -1623,7 +1622,7 @@ impl Contains<CurrencyId> for PoolCurrency {

pub struct UpdateGuard;
impl PoolUpdateGuard for UpdateGuard {
type Moment = Moment;
type Moment = Seconds;
type PoolDetails = PoolDetails<
CurrencyId,
TrancheCurrency,
Expand Down
25 changes: 12 additions & 13 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,12 @@ impl pallet_randomness_collective_flip::Config for Runtime {}
impl parachain_info::Config for Runtime {}

parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
pub const MinimumPeriod: Millis = SLOT_DURATION / 2;
}
impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = MinimumPeriod;
/// A timestamp: milliseconds since the unix epoch.
type Moment = Moment;
type Moment = Millis;
type OnTimestampSet = Aura;
type WeightInfo = weights::pallet_timestamp::WeightInfo<Runtime>;
}
Expand Down Expand Up @@ -1408,7 +1408,7 @@ parameter_types! {
#[derive(scale_info::TypeInfo)]
pub const MaxGroups: u32 = 20;
pub const LiquidityRewardsPalletId: PalletId = cfg_types::ids::LIQUIDITY_REWARDS_PALLET_ID;
pub const InitialEpochDuration: Moment = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
pub const InitialEpochDuration: Millis = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
}

impl pallet_rewards::mechanism::gap::Config for Runtime {
Expand Down Expand Up @@ -1513,7 +1513,7 @@ impl Contains<CurrencyId> for PoolCurrency {

pub struct UpdateGuard;
impl PoolUpdateGuard for UpdateGuard {
type Moment = Moment;
type Moment = Seconds;
type PoolDetails = PoolDetails<
CurrencyId,
TrancheCurrency,
Expand Down Expand Up @@ -1643,7 +1643,7 @@ parameter_types! {

// How much time should lapse before a tranche investor can be removed
#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MinDelay: Moment = 7 * SECONDS_PER_DAY;
pub const MinDelay: Seconds = 7 * SECONDS_PER_DAY;

#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MaxRolesPerPool: u32 = 10_000;
Expand All @@ -1653,17 +1653,17 @@ pub struct Editors;
impl
Contains<(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
)> for Editors
{
fn contains(
t: &(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
),
) -> bool {
let (_editor, maybe_role, _scope, role) = t;
Expand Down Expand Up @@ -1696,11 +1696,10 @@ impl pallet_permissions::Config for Runtime {
type AdminOrigin = EnsureRootOr<HalfOfCouncil>;
type Editors = Editors;
type MaxRolesPerScope = MaxRolesPerPool;
type Role = Role<TrancheId, Moment>;
type Role = Role<TrancheId>;
type RuntimeEvent = RuntimeEvent;
type Scope = PermissionScope<PoolId, CurrencyId>;
type Storage =
PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches, Moment>;
type Storage = PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches>;
type WeightInfo = weights::pallet_permissions::WeightInfo<Runtime>;
}

Expand Down Expand Up @@ -1832,7 +1831,7 @@ impl pallet_data_collector::Config for Runtime {
runtime_common::oracle::DataProviderBridge<PriceOracle, OrmlAssetRegistry, PoolSystem>;
type MaxCollectionSize = MaxCollectionSize;
type MaxCollections = MaxPoolsWithExternalPrices;
type Moment = Moment;
type Moment = Millis;
}

impl pallet_interest_accrual::Config for Runtime {
Expand Down
27 changes: 13 additions & 14 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ impl pallet_randomness_collective_flip::Config for Runtime {}
impl parachain_info::Config for Runtime {}

parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
pub const MinimumPeriod: Millis = SLOT_DURATION / 2;
}
impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = MinimumPeriod;
/// A timestamp: milliseconds since the unix epoch.
type Moment = Moment;
type Moment = Millis;
type OnTimestampSet = Aura;
type WeightInfo = weights::pallet_timestamp::WeightInfo<Runtime>;
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ impl Contains<CurrencyId> for PoolCurrency {

pub struct UpdateGuard;
impl PoolUpdateGuard for UpdateGuard {
type Moment = Moment;
type Moment = Seconds;
type PoolDetails = PoolDetails<
CurrencyId,
TrancheCurrency,
Expand All @@ -1122,7 +1122,7 @@ impl PoolUpdateGuard for UpdateGuard {
fn released(
pool: &Self::PoolDetails,
update: &Self::ScheduledUpdateDetails,
_now: Self::Moment,
_now: Self::Seconds,
) -> bool {
// - We check whether between the submission of the update this call there has
// been an epoch close event.
Expand Down Expand Up @@ -1330,7 +1330,7 @@ impl pallet_data_collector::Config for Runtime {
runtime_common::oracle::DataProviderBridge<PriceOracle, OrmlAssetRegistry, PoolSystem>;
type MaxCollectionSize = MaxCollectionSize;
type MaxCollections = MaxPoolsWithExternalPrices;
type Moment = Moment;
type Moment = Millis;
}

impl pallet_interest_accrual::Config for Runtime {
Expand Down Expand Up @@ -1376,7 +1376,7 @@ parameter_types! {

// How much time should lapse before a tranche investor can be removed
#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MinDelay: Moment = 7 * SECONDS_PER_DAY;
pub const MinDelay: Seconds = 7 * SECONDS_PER_DAY;

#[derive(Debug, Eq, PartialEq, scale_info::TypeInfo, Clone)]
pub const MaxRolesPerPool: u32 = 1_000;
Expand All @@ -1386,29 +1386,28 @@ impl pallet_permissions::Config for Runtime {
type AdminOrigin = EnsureRootOr<HalfOfCouncil>;
type Editors = Editors;
type MaxRolesPerScope = MaxRolesPerPool;
type Role = Role<TrancheId, Moment>;
type Role = Role<TrancheId>;
type RuntimeEvent = RuntimeEvent;
type Scope = PermissionScope<PoolId, CurrencyId>;
type Storage =
PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches, Moment>;
type Storage = PermissionRoles<TimeProvider<Timestamp>, MinDelay, TrancheId, MaxTranches>;
type WeightInfo = weights::pallet_permissions::WeightInfo<Runtime>;
}

pub struct Editors;
impl
Contains<(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
)> for Editors
{
fn contains(
t: &(
AccountId,
Option<Role<TrancheId, Moment>>,
Option<Role<TrancheId>>,
PermissionScope<PoolId, CurrencyId>,
Role<TrancheId, Moment>,
Role<TrancheId>,
),
) -> bool {
let (_editor, maybe_role, _scope, role) = t;
Expand Down Expand Up @@ -1749,7 +1748,7 @@ parameter_types! {
pub const MaxGroups: u32 = 20;
#[derive(scale_info::TypeInfo, Debug, PartialEq, Eq, Clone)]
pub const MaxChangesPerEpoch: u32 = 50;
pub const InitialEpochDuration: Moment = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
pub const InitialEpochDuration: Millis = SECONDS_PER_MINUTE * 1000; // 1 min in milliseconds
}

impl pallet_rewards::mechanism::gap::Config for Runtime {
Expand Down