Skip to content

Commit

Permalink
Investments: simplify constraints and remove InvestmentProperties tra…
Browse files Browse the repository at this point in the history
…it (#1571)

* remove extra where bounds

* use concrete InvestmentInfo

* remove InvestmentProperties trait
  • Loading branch information
lemunozm authored Sep 27, 2023
1 parent 828c8b6 commit 8ecf05c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 239 deletions.
52 changes: 13 additions & 39 deletions libs/test-utils/src/mocks/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@

/// Exposes a struct $name that implements the `trait Accountant`. The struct
/// expects one generic parameter that implements the fungibles traits
/// `Inspect`, `Mutate` and `Transfer`. Furthermore, there exists a struct
/// `GenesisConfig` that implements `trait GenesisBuild` that can be used
/// like any other `GenesisConfig` to initialize state in the
/// `TestExternalities`.
///
/// Also exports a `struct InvestmentInfo` to be used in the `GenesisConfig`
/// `Inspect`, `Mutate` and `Transfer`.
///
/// * E.g.: `MockAccountant<Tokens:
/// frame_support::traits::tokens::fungibles::{Inspect, Mutate, Transfer}>`
Expand Down Expand Up @@ -54,7 +49,7 @@
///
/// // Using the `GenesisConfig`
/// use accountant_mock::InvestmentInfo;
/// let storage = GenesisBuild::build_storage(&accountant_mock::GenesisConfig {
/// let storage = GenesisBuild::init(&accountant_mock::Genesis {
/// infos: vec![
/// (
/// InvestmentId::Tranche(0, [0;16]),
Expand All @@ -79,32 +74,28 @@ macro_rules! impl_mock_accountant {

use super::*;

#[derive(Default, serde::Serialize, serde::Deserialize)]
pub struct GenesisConfig {
pub type InvestmentInfo =
cfg_types::investments::InvestmentInfo<$account_id, $currency_id, $investment_id>;

#[derive(Default)]
pub struct Genesis {
pub infos: Vec<($investment_id, InvestmentInfo)>,
}

impl frame_support::traits::GenesisBuild<()> for GenesisConfig {
fn build(&self) {
pub struct $name<Tokens>(sp_std::marker::PhantomData<Tokens>);

impl<Tokens> $name<Tokens> {
pub fn init(genesis: Genesis) {
__private_STATE.with(|s| {
let mut state = s.borrow_mut();

for (id, info) in &self.infos {
for (id, info) in &genesis.infos {
state.add(id.clone(), info.clone())
}
})
}
}

pub struct $name<Tokens>(sp_std::marker::PhantomData<Tokens>);

#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct InvestmentInfo {
pub owner: $account_id,
pub id: $investment_id,
pub payment_currency: $currency_id,
}

impl<Tokens> cfg_traits::investments::InvestmentAccountant<$account_id> for $name<Tokens>
where
Tokens: frame_support::traits::tokens::fungibles::Mutate<$account_id>
Expand Down Expand Up @@ -187,23 +178,6 @@ macro_rules! impl_mock_accountant {
}
}

impl cfg_traits::investments::InvestmentProperties<$account_id> for InvestmentInfo {
type Currency = $currency_id;
type Id = $investment_id;

fn owner(&self) -> $account_id {
self.owner
}

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

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

mod __private {
use super::*;

Expand Down Expand Up @@ -235,7 +209,7 @@ macro_rules! impl_mock_accountant {

pub fn add(&mut self, investment_id: $investment_id, info: InvestmentInfo) {
// NOTE: We deliberately update the info here as add() is only called
// upon GenesisConfig.build(). We assume, if we are running in the
// upon init(). We assume, if we are running in the
// same thread this means a new initialization is wanted.
for (curr_id, curr_info) in &mut self.infos {
if *curr_id == investment_id {
Expand Down
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
Loading

0 comments on commit 8ecf05c

Please sign in to comment.