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

feat: orderbook bench helper #1558

Merged
merged 9 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
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
11 changes: 6 additions & 5 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

use cfg_primitives::CFG;
use cfg_traits::{
benchmarking::PoolBenchmarkHelper,
changes::ChangeGuard,
data::DataRegistry,
interest::{CompoundingSchedule, InterestAccrual, InterestRate},
Permissions, PoolBenchmarkHelper, PoolWriteOffPolicyMutate,
Permissions, PoolWriteOffPolicyMutate,
};
use cfg_types::{
adjustments::Adjustment,
Expand Down Expand Up @@ -77,8 +78,8 @@ fn config_mocks() {
MockPools::mock_account_for(|_| 0);
MockPools::mock_withdraw(|_, _, _| Ok(()));
MockPools::mock_deposit(|_, _, _| Ok(()));
MockPools::mock_benchmark_create_pool(|_, _| {});
MockPools::mock_benchmark_give_ausd(|_, _| {});
MockPools::mock_bench_create_pool(|_, _| {});
MockPools::mock_bench_investor_setup(|_, _, _| {});
MockPrices::mock_feed_value(|_, _, _| Ok(()));
MockPrices::mock_register_id(|_, _| Ok(()));
MockPrices::mock_collection(|_| MockDataCollection::new(|_| Ok(Default::default())));
Expand Down Expand Up @@ -107,7 +108,7 @@ where
let pool_id = Default::default();

let pool_admin = account("pool_admin", 0, 0);
T::Pool::benchmark_create_pool(pool_id, &pool_admin);
T::Pool::bench_create_pool(pool_id, &pool_admin);

let loan_admin = account("loan_admin", 0, 0);
T::Permissions::add(
Expand All @@ -118,7 +119,7 @@ where
.unwrap();

let borrower = account::<T::AccountId>("borrower", 0, 0);
T::Pool::benchmark_give_ausd(&borrower, (FUNDS * CFG).into());
T::Pool::bench_investor_setup(pool_id, borrower.clone(), (FUNDS * CFG).into());
T::NonFungible::create_collection(&COLLECION_ID.into(), &borrower, &borrower).unwrap();
T::Permissions::add(
PermissionScope::Pool(pool_id),
Expand Down
3 changes: 2 additions & 1 deletion pallets/loans/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cfg_mocks::{
pallet_mock_change_guard, pallet_mock_data, pallet_mock_permissions, pallet_mock_pools,
};
use cfg_primitives::Moment;
use cfg_types::permissions::PermissionScope;
use cfg_types::{permissions::PermissionScope, tokens::TrancheCurrency};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::traits::{
tokens::nonfungibles::{Create, Mutate},
Expand Down Expand Up @@ -199,6 +199,7 @@ impl pallet_mock_pools::Config for Runtime {
type BalanceRatio = Quantity;
type CurrencyId = CurrencyId;
type PoolId = PoolId;
type TrancheCurrency = TrancheCurrency;
type TrancheId = TrancheId;
}

Expand Down
Loading
Loading