Skip to content

Commit

Permalink
remove InvestmentProperties trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Sep 27, 2023
1 parent 62174b5 commit 2392851
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 115 deletions.
40 changes: 13 additions & 27 deletions libs/test-utils/src/mocks/order_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::investments::{
Investment, InvestmentAccountant, InvestmentProperties, OrderManager, TrancheCurrency,
Investment, InvestmentAccountant, OrderManager, TrancheCurrency,
};
use cfg_types::{
investments::InvestmentInfo,
orders::{FulfillmentWithPrice, TotalOrder},
};
use cfg_types::orders::{FulfillmentWithPrice, TotalOrder};
use frame_support::{
pallet_prelude::*,
traits::fungibles::{Inspect, Mutate, Transfer},
Expand Down Expand Up @@ -48,8 +51,6 @@ pub mod pallet {
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<Self::Tokens as Inspect<Self::AccountId>>::AssetId:
MaxEncodedLen + MaybeSerializeDeserialize,
<Self::Accountant as InvestmentAccountant<Self::AccountId>>::InvestmentInfo:
InvestmentProperties<Self::AccountId, Currency = CurrencyOf<Self>>,
{
type FundsAccount: Get<PalletId>;

Expand All @@ -58,6 +59,7 @@ pub mod pallet {
Amount = BalanceOf<Self>,
Error = DispatchError,
InvestmentId = Self::InvestmentId,
InvestmentInfo = InvestmentInfo<Self::AccountId, CurrencyOf<Self>, Self::InvestmentId>,
>;

type PoolId: Member + Parameter + Default + Copy + MaxEncodedLen;
Expand Down Expand Up @@ -87,8 +89,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
pub invest_orders: Vec<(T::InvestmentId, BalanceOf<T>)>,
pub redeem_orders: Vec<(T::InvestmentId, BalanceOf<T>)>,
Expand All @@ -100,8 +100,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
fn default() -> Self {
Self {
Expand All @@ -117,8 +115,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
fn build(&self) {
for (id, amount) in &self.invest_orders {
Expand All @@ -144,8 +140,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
// TODO: Remove once we are on Substrate:polkadot-v0.9.29
}
Expand All @@ -155,8 +149,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
// TODO: Remove once we are on Substrate:polkadot-v0.9.29
}
Expand All @@ -166,8 +158,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
/// **Test Method**
///
Expand All @@ -184,7 +174,7 @@ pub mod pallet {
let details = T::Accountant::info(investment_id)?;

T::Tokens::transfer(
details.payment_currency(),
details.payment_currency,
&T::FundsAccount::get().into_account_truncating(),
&OrderManagerAccount::get::<T>(),
amount,
Expand Down Expand Up @@ -218,8 +208,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
type Amount = BalanceOf<T>;
type CurrencyId = CurrencyOf<T>;
Expand All @@ -239,7 +227,7 @@ pub mod pallet {
currency: Self::CurrencyId,
) -> bool {
T::Accountant::info(investment_id)
.map(|info| info.payment_currency() == currency)
.map(|info| info.payment_currency == currency)
.unwrap_or(false)
}

Expand All @@ -265,7 +253,7 @@ pub mod pallet {
currency: Self::CurrencyId,
) -> bool {
T::Accountant::info(investment_id)
.map(|info| info.payment_currency() == currency)
.map(|info| info.payment_currency == currency)
.unwrap_or(false)
}

Expand Down Expand Up @@ -298,8 +286,6 @@ pub mod pallet {
<T::Tokens as Inspect<T::AccountId>>::Balance:
From<u64> + FixedPointOperand + MaxEncodedLen + MaybeSerializeDeserialize,
<T::Tokens as Inspect<T::AccountId>>::AssetId: MaxEncodedLen + MaybeSerializeDeserialize,
<T::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyOf<T>>,
{
type Error = DispatchError;
type Fulfillment = FulfillmentWithPrice<T::Rate>;
Expand Down Expand Up @@ -344,9 +330,9 @@ pub mod pallet {
let tokens_to_transfer_to_pool = fulfillment.of_amount.mul_floor(orders.amount);
let details = T::Accountant::info(asset_id)?;
T::Tokens::transfer(
details.payment_currency(),
details.payment_currency,
&OrderManagerAccount::get::<T>(),
&details.payment_account(),
&details.owner,
tokens_to_transfer_to_pool,
true,
)
Expand Down Expand Up @@ -410,8 +396,8 @@ pub mod pallet {
.unwrap();
let details = T::Accountant::info(asset_id)?;
T::Tokens::transfer(
details.payment_currency(),
&details.payment_account(),
details.payment_currency,
&details.owner,
&OrderManagerAccount::get::<T>(),
payment_currency_to_move_to_order_manager,
false,
Expand Down
51 changes: 1 addition & 50 deletions libs/traits/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub trait OrderManager {
pub trait InvestmentAccountant<AccountId> {
type Error;
type InvestmentId;
type InvestmentInfo: InvestmentProperties<AccountId, Id = Self::InvestmentId>;
type InvestmentInfo;
type Amount;

/// Information about an asset. Must allow to derive
Expand Down Expand Up @@ -219,55 +219,6 @@ pub trait InvestmentAccountant<AccountId> {
) -> Result<(), Self::Error>;
}

/// A trait that allows to retrieve information
/// about an investment class.
pub trait InvestmentProperties<AccountId> {
/// The overarching Currency that payments
/// for this class are made in
type Currency;
/// Who the investment class can be identified
type Id;

/// Returns the owner of the investment class
fn owner(&self) -> AccountId;

/// Returns the id of the investment class
fn id(&self) -> Self::Id;

/// Returns the currency in which the investment class
/// can be bought.
fn payment_currency(&self) -> Self::Currency;

/// Returns the account a payment for the investment class
/// must be made to.
///
/// Defaults to owner.
fn payment_account(&self) -> AccountId {
self.owner()
}
}

impl<AccountId, T: InvestmentProperties<AccountId>> InvestmentProperties<AccountId> for &T {
type Currency = T::Currency;
type Id = T::Id;

fn owner(&self) -> AccountId {
(*self).owner()
}

fn id(&self) -> Self::Id {
(*self).id()
}

fn payment_currency(&self) -> Self::Currency {
(*self).payment_currency()
}

fn payment_account(&self) -> AccountId {
(*self).payment_account()
}
}

/// Trait to handle Investment Portfolios for accounts
pub trait InvestmentsPortfolio<Account> {
type InvestmentId;
Expand Down
24 changes: 0 additions & 24 deletions libs/types/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// GNU General Public License for more details.

use cfg_primitives::OrderId;
use cfg_traits::investments::InvestmentProperties;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::fmt::Debug, RuntimeDebug};
use scale_info::TypeInfo;
Expand All @@ -35,29 +34,6 @@ pub struct InvestmentInfo<AccountId, Currency, InvestmentId> {
pub payment_currency: Currency,
}

impl<AccountId, Currency, InvestmentId> InvestmentProperties<AccountId>
for InvestmentInfo<AccountId, Currency, InvestmentId>
where
AccountId: Clone,
Currency: Clone,
InvestmentId: Clone,
{
type Currency = Currency;
type Id = InvestmentId;

fn owner(&self) -> AccountId {
self.owner.clone()
}

fn id(&self) -> Self::Id {
self.id.clone()
}

fn payment_currency(&self) -> Self::Currency {
self.payment_currency.clone()
}
}

/// The outstanding collections for an account
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct InvestCollection<Balance> {
Expand Down
8 changes: 3 additions & 5 deletions pallets/investments/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use cfg_traits::{
benchmarking::{InvestmentIdBenchmarkHelper, PoolBenchmarkHelper},
investments::{Investment, InvestmentAccountant, InvestmentProperties, OrderManager},
investments::{Investment, InvestmentAccountant, OrderManager},
};
use cfg_types::orders::FulfillmentWithPrice;
use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*, whitelisted_caller};
Expand Down Expand Up @@ -58,7 +58,7 @@ mod benchmarks {
fn update_invest_order() {
let caller: T::AccountId = whitelisted_caller();
let investment_id = Helper::<T>::get_investment_id();
let currency_id = T::Accountant::info(investment_id)?.payment_currency();
let currency_id = T::Accountant::info(investment_id)?.payment_currency;

T::Tokens::mint_into(currency_id, &caller, 1u32.into())?;

Expand All @@ -82,9 +82,7 @@ mod benchmarks {
fn collect_investments(n: Linear<1, 10>) {
let caller: T::AccountId = whitelisted_caller();
let investment_id = Helper::<T>::get_investment_id();
let currency_id = T::Accountant::info(investment_id)
.unwrap()
.payment_currency();
let currency_id = T::Accountant::info(investment_id)?.payment_currency;

T::Tokens::mint_into(currency_id, &caller, 1u32.into())?;

Expand Down
4 changes: 1 addition & 3 deletions pallets/pool-registry/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

//! Module provides benchmarking for Loan Pallet
use cfg_primitives::{Moment, PoolEpochId};
use cfg_traits::investments::{InvestmentAccountant, InvestmentProperties, TrancheCurrency as _};
use cfg_traits::investments::TrancheCurrency as _;
use cfg_types::{
pools::TrancheMetadata,
tokens::{CurrencyId, TrancheCurrency},
Expand Down Expand Up @@ -70,8 +70,6 @@ benchmarks! {
MaxTranches = <T as Config>::MaxTranches>,
T: pallet_timestamp::Config<Moment = Moment>,
<T as pallet_investments::Config>::Tokens: Inspect<T::AccountId, AssetId = CurrencyId, Balance = u128>,
<<T as pallet_investments::Config>::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyId>,
<<T as frame_system::Config>::Lookup as sp_runtime::traits::StaticLookup>::Source:
From<<T as frame_system::Config>::AccountId>,
<T as pallet_pool_system::Config>::Permission: Permissions<T::AccountId, Ok = ()>,
Expand Down
7 changes: 1 addition & 6 deletions pallets/pool-system/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

//! Module provides benchmarking for Loan Pallet
use cfg_primitives::PoolEpochId;
use cfg_traits::{
investments::{InvestmentAccountant, InvestmentProperties, TrancheCurrency as _},
UpdateState,
};
use cfg_traits::{investments::TrancheCurrency as _, UpdateState};
use cfg_types::{
pools::TrancheMetadata,
tokens::{CurrencyId, CustomMetadata, TrancheCurrency},
Expand Down Expand Up @@ -53,8 +50,6 @@ benchmarks! {
EpochId = PoolEpochId>
+ pallet_investments::Config<InvestmentId = TrancheCurrency, Amount = u128>,
<T as pallet_investments::Config>::Tokens: Inspect<T::AccountId, AssetId = CurrencyId, Balance = u128>,
<<T as pallet_investments::Config>::Accountant as InvestmentAccountant<T::AccountId>>::InvestmentInfo:
InvestmentProperties<T::AccountId, Currency = CurrencyId>,
T::AccountId: EncodeLike<<T as frame_system::Config>::AccountId>,
<<T as frame_system::Config>::Lookup as sp_runtime::traits::StaticLookup>::Source:
From<<T as frame_system::Config>::AccountId>,
Expand Down

0 comments on commit 2392851

Please sign in to comment.