Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into loans/transfer-debt
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Sep 25, 2023
2 parents 36423f1 + d007468 commit a18552d
Show file tree
Hide file tree
Showing 39 changed files with 1,888 additions and 724 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cfg_traits::liquidity_pools::Codec;
use codec::{Decode, Error, Input};
use scale_info::TypeInfo;

#[derive(Debug, Eq, PartialEq, Clone, codec::Encode, Decode)]
#[derive(Debug, Eq, PartialEq, Clone, codec::Encode, Decode, TypeInfo)]
pub enum MessageMock {
First,
Second,
Expand Down
33 changes: 26 additions & 7 deletions libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod pallet {
type Balance;
type BalanceRatio;
type CurrencyId;
type TrancheCurrency;
}

#[pallet::pallet]
Expand Down Expand Up @@ -66,12 +67,20 @@ pub mod pallet {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_benchmark_create_pool(f: impl Fn(T::PoolId, &T::AccountId) + 'static) {
pub fn mock_bench_create_pool(f: impl Fn(T::PoolId, &T::AccountId) + 'static) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_benchmark_give_ausd(f: impl Fn(&T::AccountId, T::Balance) + 'static) {
register_call!(move |(a, b)| f(a, b));
pub fn mock_bench_investor_setup(
f: impl Fn(T::PoolId, T::AccountId, T::Balance) + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_bench_default_investment_id(
f: impl Fn(T::PoolId) -> T::TrancheCurrency + 'static + 'static,
) {
register_call!(f);
}
}

Expand Down Expand Up @@ -124,17 +133,27 @@ pub mod pallet {
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> cfg_traits::PoolBenchmarkHelper for Pallet<T> {
impl<T: Config> cfg_traits::benchmarking::PoolBenchmarkHelper for Pallet<T> {
type AccountId = T::AccountId;
type Balance = T::Balance;
type PoolId = T::PoolId;

fn benchmark_create_pool(a: Self::PoolId, b: &Self::AccountId) {
fn bench_create_pool(a: Self::PoolId, b: &Self::AccountId) {
execute_call!((a, b))
}

fn benchmark_give_ausd(a: &Self::AccountId, b: Self::Balance) {
execute_call!((a, b))
fn bench_investor_setup(a: Self::PoolId, b: Self::AccountId, c: Self::Balance) {
execute_call!((a, b, c))
}
}

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

fn bench_default_investment_id(a: Self::PoolId) -> Self::InvestmentId {
execute_call!(a)
}
}
}
60 changes: 60 additions & 0 deletions libs/traits/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

/// Benchmark utility to create pools
pub trait PoolBenchmarkHelper {
type PoolId;
type AccountId;
type Balance;

/// Create a pool for the given the pool id and the admin.
fn bench_create_pool(pool_id: Self::PoolId, admin: &Self::AccountId);

/// Prepare user to be able to invest, i.e. fund with pool currency and give
/// permissions.
fn bench_investor_setup(
pool_id: Self::PoolId,
account: Self::AccountId,
balance: Self::Balance,
);
}

/// Benchmark utility to expose investment identifiers
pub trait InvestmentIdBenchmarkHelper {
type PoolId;
type InvestmentId;

/// Return the default investment id for the given pool.
fn bench_default_investment_id(pool_id: Self::PoolId) -> Self::InvestmentId;
}

/// Benchmark utility for adding currency trading pairs
pub trait OrderBookBenchmarkHelper {
type AccountId;
type Balance;
type CurrencyId;
type OrderIdNonce;

/// Adds the corresponding trading pair, creates trader accounts and mints
/// appropriate amounts of balance into these
fn bench_setup_trading_pair(
asset_in: Self::CurrencyId,
asset_out: Self::CurrencyId,
amount_in: Self::Balance,
amount_out: Self::Balance,
decimals_in: u32,
decimals_out: u32,
) -> (Self::AccountId, Self::AccountId);

/// Fulfills the given swap order from the trader account
fn bench_fill_order_full(trader: Self::AccountId, order_id: Self::OrderIdNonce);
}
4 changes: 1 addition & 3 deletions libs/traits/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ pub trait ForeignInvestment<AccountId> {
type CurrencyId;
type Error: Debug;
type InvestmentId;
type CollectInvestResult;

/// Initiates the increment of a foreign investment amount in
/// `foreign_payment_currency` of who into the investment class
Expand Down Expand Up @@ -365,8 +364,7 @@ pub trait ForeignInvestment<AccountId> {
who: &AccountId,
investment_id: Self::InvestmentId,
foreign_currency: Self::CurrencyId,
pool_currency: Self::CurrencyId,
) -> Result<Self::CollectInvestResult, Self::Error>;
) -> Result<(), Self::Error>;

/// Collect the results of a user's foreign redeem orders for the given
/// investment. If any amounts are not fulfilled they are directly
Expand Down
22 changes: 7 additions & 15 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub mod liquidity_pools;
/// Traits related to rewards.
pub mod rewards;

#[cfg(feature = "runtime-benchmarks")]
/// Traits related to benchmarking tooling.
pub mod benchmarking;

/// A trait used for loosely coupling the claim pallet with a reward mechanism.
///
/// ## Overview
Expand Down Expand Up @@ -128,8 +132,10 @@ pub trait PoolInspect<AccountId, CurrencyId> {
type TrancheId;
type Moment;

/// check if the pool exists
/// Check if the pool exists
fn pool_exists(pool_id: Self::PoolId) -> bool;

/// Check if the tranche exists for the given pool
fn tranche_exists(pool_id: Self::PoolId, tranche_id: Self::TrancheId) -> bool;

/// Get the account used for the given `pool_id`.
Expand Down Expand Up @@ -257,20 +263,6 @@ pub trait PoolWriteOffPolicyMutate<PoolId> {
fn worst_case_policy() -> Self::Policy;
}

/// Utility to benchmark pools easily
#[cfg(feature = "runtime-benchmarks")]
pub trait PoolBenchmarkHelper {
type PoolId;
type AccountId;
type Balance;

/// Create a benchmark pool giving the id and the admin.
fn benchmark_create_pool(pool_id: Self::PoolId, admin: &Self::AccountId);

/// Give AUSD to the account
fn benchmark_give_ausd(account: &Self::AccountId, balance: Self::Balance);
}

/// A trait that can be used to retrieve the current price for a currency
pub struct CurrencyPair<CurrencyId> {
pub base: CurrencyId,
Expand Down
7 changes: 3 additions & 4 deletions libs/types/src/domain_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use cfg_traits::liquidity_pools::Codec;
use cfg_utils::{decode_be_bytes, vec_to_fixed_array};
use codec::{Decode, Encode, Input, MaxEncodedLen};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
use sp_runtime::traits::{AccountIdConversion, Convert};
use sp_std::{vec, vec::Vec};
Expand All @@ -23,8 +24,7 @@ use crate::EVMChainId;
/// The domain indices need to match those used in the EVM contracts and these
/// need to pass the Centrifuge domain to send tranche tokens from the other
/// domain here. Therefore, DO NOT remove or move variants around.
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Encode, Decode, Clone, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub enum Domain {
/// Referring to the Centrifuge Parachain. Will be used for handling
/// incoming messages.
Expand Down Expand Up @@ -77,8 +77,7 @@ pub struct DomainLocator<Domain> {
pub domain: Domain,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Encode, Decode, Clone, Eq, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub enum DomainAddress {
/// A Centrifuge-Chain based account address, 32-bytes long
Centrifuge([u8; 32]),
Expand Down
43 changes: 21 additions & 22 deletions libs/types/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub struct Swap<
pub currency_in: Currency,
/// The outgoing currency, i.e. the one which should be replaced.
pub currency_out: Currency,
/// The amount of outgoing currency which shall be exchanged.
/// The amount of incoming currency which shall be bought.
pub amount: Balance,
}

Expand Down Expand Up @@ -237,30 +237,29 @@ pub struct ExecutedForeignDecreaseInvest<Balance, Currency> {
pub amount_remaining: Balance,
}

/// A representation of an executed collected investment.
/// A representation of an executed collected foreign investment or redemption.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]

pub struct ExecutedForeignCollectInvest<Balance> {
/// The amount that was actually collected
pub amount_currency_payout: Balance,
/// The amount of tranche tokens received for the investment made
pub amount_tranche_tokens_payout: Balance,
/// The unprocessed plus processed but not yet collected investment amount
/// denominated in foreign currency
pub amount_remaining_invest: Balance,
}

/// A representation of an executed collected redemption.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]

pub struct ExecutedForeignCollectRedeem<Balance, Currency> {
/// The foreign currency in which the payout takes place
pub struct ExecutedForeignCollect<Balance, Currency> {
/// The foreign currency in which ...
/// * If investment: the payment took place
/// * If redemption: the payout takes place
pub currency: Currency,
/// The amount of `currency` being paid out to the investor

/// The amount of `currency`...
/// * If investment: that was collected
/// * If redemption: paid out to the investor
pub amount_currency_payout: Balance,
/// How many tranche tokens were actually redeemed

/// The amount of tranche tokens...
/// * If investment: received for the investment made
/// * If redemption: which were actually redeemed
pub amount_tranche_tokens_payout: Balance,
/// The unprocessed plus processed but not yet collected redemption amount
/// of tranche tokens
pub amount_remaining_redeem: Balance,

/// The unprocessed ...
/// * If investment: investment amount of `currency` (denominated in foreign
/// currency)
/// * If redemption: redemption amount of tranche tokens (denominated in
/// pool currency)
pub amount_remaining: Balance,
}
19 changes: 12 additions & 7 deletions pallets/foreign-investments/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub enum InvestError {
CollectTransition,
/// The investment needs to be collected before it can be updated further.
CollectRequired,
/// The provided currency does not match the one stored when the first
/// investment increase was triggered.
///
/// NOTE: As long as the `InvestmentState` has not been cleared, the
/// payment currency cannot change from the initially provided one.
InvalidPaymentCurrency,
}

#[derive(Encode, Decode, TypeInfo, PalletError)]
Expand All @@ -41,13 +47,6 @@ pub enum RedeemError {
IncreaseTransition,
/// Failed to collect the redemption.
CollectTransition,
/// Failed to retrieve the foreign payout currency for a collected
/// redemption.
///
/// NOTE: This error can only occur, if a user tries to collect before
/// having increased their redemption as this would store the payout
/// currency.
CollectPayoutCurrencyNotFound,
/// The desired decreasing amount exceeds the max amount.
DecreaseAmountOverflow,
/// Failed to transition the state as a result of a decrease.
Expand All @@ -56,6 +55,12 @@ pub enum RedeemError {
FulfillSwapOrderTransition,
/// The redemption needs to be collected before it can be updated further.
CollectRequired,
/// The provided currency does not match the one stored when the first
/// redemption increase was triggered.
///
/// NOTE: As long as the `RedemptionState` has not been cleared, the
/// payout currency cannot change from the initially provided one.
InvalidPayoutCurrency,
}

impl<T: Config> From<InvestError> for Error<T> {
Expand Down
Loading

0 comments on commit a18552d

Please sign in to comment.