Skip to content

Commit

Permalink
Refactor: Transform TrancheCurrency type in a tuple (#1926)
Browse files Browse the repository at this point in the history
* remove TrancheCurrency type

* fix benchmarks

* fix clippy
  • Loading branch information
lemunozm authored and gpmayorga committed Jul 29, 2024
1 parent 9a7b321 commit 7385ecb
Show file tree
Hide file tree
Showing 31 changed files with 303 additions and 496 deletions.
26 changes: 16 additions & 10 deletions libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod pallet {
type Balance;
type BalanceRatio;
type CurrencyId;
type TrancheCurrency;
}

#[pallet::pallet]
Expand Down Expand Up @@ -65,21 +64,28 @@ pub mod pallet {

pub fn mock_info(
f: impl Fn(
T::TrancheCurrency,
(T::PoolId, T::TrancheId),
) -> Result<
InvestmentInfo<T::AccountId, T::CurrencyId, T::TrancheCurrency>,
InvestmentInfo<T::AccountId, T::CurrencyId, (T::PoolId, T::TrancheId)>,
DispatchError,
> + 'static,
) {
register_call!(f);
}

pub fn mock_balance(f: impl Fn(T::TrancheCurrency, &T::AccountId) -> T::Balance + 'static) {
pub fn mock_balance(
f: impl Fn((T::PoolId, T::TrancheId), &T::AccountId) -> T::Balance + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_transfer(
f: impl Fn(T::TrancheCurrency, &T::AccountId, &T::AccountId, T::Balance) -> DispatchResult
f: impl Fn(
(T::PoolId, T::TrancheId),
&T::AccountId,
&T::AccountId,
T::Balance,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
Expand All @@ -93,21 +99,21 @@ pub mod pallet {

#[allow(non_snake_case)]
pub fn mock_InvestmentAccountant_deposit(
f: impl Fn(&T::AccountId, T::TrancheCurrency, T::Balance) -> DispatchResult + 'static,
f: impl Fn(&T::AccountId, (T::PoolId, T::TrancheId), T::Balance) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

#[allow(non_snake_case)]
pub fn mock_InvestmentAccountant_withdraw(
f: impl Fn(&T::AccountId, T::TrancheCurrency, T::Balance) -> DispatchResult + 'static,
f: impl Fn(&T::AccountId, (T::PoolId, T::TrancheId), T::Balance) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

#[cfg(feature = "runtime-benchmarks")]
pub fn mock_bench_default_investment_id(
f: impl Fn(T::PoolId) -> T::TrancheCurrency + 'static,
f: impl Fn(T::PoolId) -> (T::PoolId, T::TrancheId) + 'static,
) {
register_call!(f);
}
Expand Down Expand Up @@ -138,7 +144,7 @@ pub mod pallet {
impl<T: Config> InvestmentAccountant<T::AccountId> for Pallet<T> {
type Amount = T::Balance;
type Error = DispatchError;
type InvestmentId = T::TrancheCurrency;
type InvestmentId = (T::PoolId, T::TrancheId);
type InvestmentInfo = InvestmentInfo<T::AccountId, T::CurrencyId, Self::InvestmentId>;

fn info(a: Self::InvestmentId) -> Result<Self::InvestmentInfo, DispatchError> {
Expand Down Expand Up @@ -211,7 +217,7 @@ pub mod pallet {

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> cfg_traits::benchmarking::InvestmentIdBenchmarkHelper for Pallet<T> {
type InvestmentId = T::TrancheCurrency;
type InvestmentId = (T::PoolId, T::TrancheId);
type PoolId = T::PoolId;

fn bench_default_investment_id(a: Self::PoolId) -> Self::InvestmentId {
Expand Down
3 changes: 3 additions & 0 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub mod types {
/// A representation of a tranche identifier
pub type TrancheId = [u8; 16];

/// A representation of an investment
pub type InvestmentId = (PoolId, TrancheId);

/// A representation of a tranche weight, used to weight
/// importance of a tranche
#[derive(Encode, Decode, Copy, Debug, Default, Clone, PartialEq, Eq, TypeInfo, CompactAs)]
Expand Down
14 changes: 14 additions & 0 deletions libs/traits/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ pub trait TrancheCurrency<PoolId, TrancheId> {
fn of_tranche(&self) -> TrancheId;
}

impl<PoolId: Clone, TrancheId: Clone> TrancheCurrency<PoolId, TrancheId> for (PoolId, TrancheId) {
fn generate(pool_id: PoolId, tranche_id: TrancheId) -> Self {
(pool_id, tranche_id)
}

fn of_pool(&self) -> PoolId {
self.0.clone()
}

fn of_tranche(&self) -> TrancheId {
self.1.clone()
}
}

/// A trait, when implemented allows to invest into
/// investment classes
pub trait Investment<AccountId> {
Expand Down
57 changes: 7 additions & 50 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use cfg_primitives::{
types::{PoolId, TrancheId},
Balance,
};
use cfg_traits::{investments::TrancheCurrency as TrancheCurrencyT, HasLocalAssetRepresentation};
use cfg_traits::HasLocalAssetRepresentation;
use orml_traits::asset_registry;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -121,6 +121,12 @@ impl TryFrom<CurrencyId> for LocalAssetId {
}
}

impl From<(PoolId, TrancheId)> for CurrencyId {
fn from((pool_id, tranche_id): (PoolId, TrancheId)) -> Self {
CurrencyId::Tranche(pool_id, tranche_id)
}
}

#[derive(
Clone,
Copy,
Expand Down Expand Up @@ -230,55 +236,6 @@ where
}
}

/// A Currency that is solely used by tranches.
///
/// We distinguish here between the enum variant CurrencyId::Tranche(PoolId,
/// TranchId) in order to be able to have a clear separation of concerns. This
/// enables us to use the `TrancheCurrency` type separately where solely this
/// enum variant would be relevant. Most notably, in the `struct Tranche`.
#[derive(
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub struct TrancheCurrency {
pub pool_id: PoolId,
pub tranche_id: TrancheId,
}

impl From<TrancheCurrency> for CurrencyId {
fn from(x: TrancheCurrency) -> Self {
CurrencyId::Tranche(x.pool_id, x.tranche_id)
}
}

impl TrancheCurrencyT<PoolId, TrancheId> for TrancheCurrency {
fn generate(pool_id: PoolId, tranche_id: TrancheId) -> Self {
Self {
pool_id,
tranche_id,
}
}

fn of_pool(&self) -> PoolId {
self.pool_id
}

fn of_tranche(&self) -> TrancheId {
self.tranche_id
}
}

/// A type describing our custom additional metadata stored in the
/// OrmlAssetRegistry.
#[derive(
Expand Down
33 changes: 5 additions & 28 deletions pallets/foreign-investments/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use cfg_traits::investments::TrancheCurrency;
use cfg_types::investments::{ExecutedForeignCollect, ExecutedForeignDecreaseInvest};
use frame_support::derive_impl;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::FixedU128;

use crate::{pallet as pallet_foreign_investments, FulfilledSwapHook, SwapId};
Expand All @@ -15,25 +12,6 @@ pub type OrderId = u64;
pub type CurrencyId = u8;
pub type Ratio = FixedU128;

#[derive(
Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
pub struct InvestmentId(pub PoolId, pub TrancheId);

impl TrancheCurrency<PoolId, TrancheId> for InvestmentId {
fn generate(pool_id: PoolId, tranche_id: TrancheId) -> Self {
Self(pool_id, tranche_id)
}

fn of_pool(&self) -> PoolId {
self.0
}

fn of_tranche(&self) -> TrancheId {
self.1
}
}

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
Expand All @@ -56,7 +34,7 @@ impl frame_system::Config for Runtime {
impl cfg_mocks::investment::pallet::Config for Runtime {
type Amount = Balance;
type CurrencyId = CurrencyId;
type InvestmentId = InvestmentId;
type InvestmentId = (PoolId, TrancheId);
type TrancheAmount = Balance;
}

Expand All @@ -70,19 +48,19 @@ impl cfg_mocks::token_swaps::pallet::Config for Runtime {

type Hook1 = cfg_mocks::status_notification::pallet::Instance1;
impl cfg_mocks::status_notification::pallet::Config<Hook1> for Runtime {
type Id = (AccountId, InvestmentId);
type Id = (AccountId, (PoolId, TrancheId));
type Status = ExecutedForeignDecreaseInvest<Balance, CurrencyId>;
}

type Hook2 = cfg_mocks::status_notification::pallet::Instance2;
impl cfg_mocks::status_notification::pallet::Config<Hook2> for Runtime {
type Id = (AccountId, InvestmentId);
type Id = (AccountId, (PoolId, TrancheId));
type Status = ExecutedForeignCollect<Balance, Balance, Balance, CurrencyId>;
}

type Hook3 = cfg_mocks::status_notification::pallet::Instance3;
impl cfg_mocks::status_notification::pallet::Config<Hook3> for Runtime {
type Id = (AccountId, InvestmentId);
type Id = (AccountId, (PoolId, TrancheId));
type Status = ExecutedForeignCollect<Balance, Balance, Balance, CurrencyId>;
}

Expand All @@ -91,7 +69,6 @@ impl cfg_mocks::pools::pallet::Config for Runtime {
type BalanceRatio = Ratio;
type CurrencyId = CurrencyId;
type PoolId = PoolId;
type TrancheCurrency = InvestmentId;
type TrancheId = TrancheId;
}

Expand All @@ -111,7 +88,7 @@ impl pallet_foreign_investments::Config for Runtime {
type DecreasedForeignInvestOrderHook = MockDecreaseInvestHook;
type ForeignBalance = Balance;
type Investment = MockInvestment;
type InvestmentId = InvestmentId;
type InvestmentId = (PoolId, TrancheId);
type PoolBalance = Balance;
type PoolInspect = MockPools;
type RuntimeEvent = RuntimeEvent;
Expand Down
2 changes: 1 addition & 1 deletion pallets/foreign-investments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
};

const USER: AccountId = 1;
const INVESTMENT_ID: InvestmentId = InvestmentId(42, 23);
const INVESTMENT_ID: (PoolId, TrancheId) = (42, 23);
const FOREIGN_CURR: CurrencyId = 5;
const POOL_CURR: CurrencyId = 10;
const STABLE_RATIO: Balance = 10; // Means: 1 foreign curr is 10 pool curr
Expand Down
2 changes: 1 addition & 1 deletion pallets/investments/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where

#[cfg(test)]
crate::mock::MockAccountant::mock_bench_default_investment_id(|_| {
crate::mock::InvestmentId::default()
cfg_primitives::InvestmentId::default()
});

T::Accountant::bench_create_funded_pool(pool_id, &pool_admin);
Expand Down
65 changes: 4 additions & 61 deletions pallets/investments/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ use frame_support::{
},
};
use orml_traits::GetByKey;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_arithmetic::{FixedPointNumber, Perquintill};
use sp_io::TestExternalities;
use sp_runtime::{traits::AccountIdConversion, BuildStorage, DispatchError, DispatchResult};
Expand Down Expand Up @@ -111,7 +108,6 @@ impl cfg_mocks::pallet_mock_pools::Config for Runtime {
type BalanceRatio = Quantity;
type CurrencyId = CurrencyId;
type PoolId = PoolId;
type TrancheCurrency = InvestmentId;
type TrancheId = TrancheId;
}

Expand Down Expand Up @@ -153,51 +149,6 @@ impl<T> PreConditions<T> for Always {
}
}

// TODO: This struct should be temporarily needed only
// We should add the possibility to use subsets of the
// global CurrencyId enum
#[derive(
Copy,
Clone,
Encode,
Decode,
PartialEq,
Debug,
Ord,
PartialOrd,
Eq,
TypeInfo,
Serialize,
Deserialize,
MaxEncodedLen,
)]
pub enum InvestmentId {
PoolTranche {
pool_id: PoolId,
tranche_id: TrancheId,
},
}

impl Default for InvestmentId {
fn default() -> Self {
Self::PoolTranche {
pool_id: Default::default(),
tranche_id: Default::default(),
}
}
}

impl From<InvestmentId> for CurrencyId {
fn from(val: InvestmentId) -> Self {
match val {
InvestmentId::PoolTranche {
pool_id,
tranche_id,
} => CurrencyId::Tranche(pool_id, tranche_id),
}
}
}

// Test externalities builder
//
// This type is mainly used for mocking storage in tests. It is the type alias
Expand Down Expand Up @@ -226,21 +177,13 @@ pub const TRANCHE_ID_1: [u8; 16] = [1u8; 16];
pub const OWNER_START_BALANCE: u128 = 100_000_000 * CURRENCY;

/// The investment-id for investing into pool 0 and tranche 0
pub const INVESTMENT_0_0: InvestmentId = InvestmentId::PoolTranche {
pool_id: POOL_ID,
tranche_id: TRANCHE_ID_0,
};
pub const INVESTMENT_0_0: InvestmentId = (POOL_ID, TRANCHE_ID_0);

/// The investment-id for investing into pool 0 and tranche 1
pub const INVESTMENT_0_1: InvestmentId = InvestmentId::PoolTranche {
pool_id: POOL_ID,
tranche_id: TRANCHE_ID_1,
};
pub const INVESTMENT_0_1: InvestmentId = (POOL_ID, TRANCHE_ID_1);

/// An unknown investment id -> i.e. a not yet created pool
pub const UNKNOWN_INVESTMENT: InvestmentId = InvestmentId::PoolTranche {
pool_id: 1,
tranche_id: TRANCHE_ID_0,
};
pub const UNKNOWN_INVESTMENT: InvestmentId = (1, TRANCHE_ID_0);

/// The currency id for the AUSD token
pub const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1);
Expand Down
Loading

0 comments on commit 7385ecb

Please sign in to comment.