-
Notifications
You must be signed in to change notification settings - Fork 86
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: benchmark foreign investments worst case #1565
Changes from 18 commits
3189ed3
b7227b4
bb563a0
80f4c2f
ac3ae0c
6681346
32e28bd
d1b80a5
2131eda
ff04c51
6adb198
daddfb6
0af85e5
1d6a5b1
9cd2f50
67aef5b
4fd7682
99e7087
a9f5b3f
d34c7ce
a22054e
239fa89
0d1c6f3
030c3a0
1936e32
04ea57a
90eb02e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright 2021 Centrifuge Foundation (centrifuge.io). | ||
// This file is part of 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. | ||
|
||
use cfg_traits::{ | ||
benchmarking::{ | ||
ForeignInvestmentBenchmarkHelper, InvestmentIdBenchmarkHelper, OrderBookBenchmarkHelper, | ||
PoolBenchmarkHelper, | ||
}, | ||
investments::{ForeignInvestment, OrderManager}, | ||
}; | ||
use cfg_types::{ | ||
fixed_point::Ratio, | ||
orders::{FulfillmentWithPrice, TotalOrder}, | ||
tokens::CurrencyId, | ||
}; | ||
use frame_benchmarking::Zero; | ||
use frame_support::assert_ok; | ||
use sp_runtime::{DispatchError, FixedPointNumber, Perquintill}; | ||
|
||
use crate::{Config, Pallet}; | ||
|
||
pub const CURRENCY_POOL: CurrencyId = CurrencyId::ForeignAsset(1); | ||
pub const CURRENCY_FOREIGN: CurrencyId = CurrencyId::ForeignAsset(2); | ||
pub const DECIMALS_POOL: u32 = 12; | ||
pub const DECIMALS_FOREIGN: u32 = 6; | ||
pub const INVEST_AMOUNT_POOL_DENOMINATED: u128 = 1_000_000_000_000; | ||
pub const INVEST_AMOUNT_FOREIGN_DENOMINATED: u128 = INVEST_AMOUNT_POOL_DENOMINATED / 1_000_000; | ||
|
||
impl<T: Config> ForeignInvestmentBenchmarkHelper for Pallet<T> | ||
where | ||
T::Balance: From<u128>, | ||
T::CurrencyId: From<CurrencyId>, | ||
T::PoolInspect: PoolBenchmarkHelper<PoolId = T::PoolId, AccountId = T::AccountId, Balance = T::Balance> | ||
+ InvestmentIdBenchmarkHelper<PoolId = T::PoolId, InvestmentId = T::InvestmentId>, | ||
T::TokenSwaps: OrderBookBenchmarkHelper< | ||
AccountId = T::AccountId, | ||
Balance = T::Balance, | ||
CurrencyId = T::CurrencyId, | ||
OrderIdNonce = T::TokenSwapOrderId, | ||
>, | ||
T::Investment: OrderManager< | ||
Error = DispatchError, | ||
InvestmentId = T::InvestmentId, | ||
Orders = TotalOrder<T::Balance>, | ||
Fulfillment = FulfillmentWithPrice<T::BalanceRatio>, | ||
>, | ||
T::BalanceRatio: From<Ratio>, | ||
{ | ||
type AccountId = T::AccountId; | ||
type Balance = T::Balance; | ||
type CurrencyId = T::CurrencyId; | ||
type InvestmentId = T::InvestmentId; | ||
|
||
fn bench_prepare_foreign_investments_setup() -> ( | ||
T::AccountId, | ||
T::InvestmentId, | ||
T::CurrencyId, | ||
T::CurrencyId, | ||
T::AccountId, | ||
) { | ||
let pool_id = Default::default(); | ||
let pool_admin: T::AccountId = frame_benchmarking::account("pool_admin", 0, 0); | ||
<T::PoolInspect as PoolBenchmarkHelper>::bench_create_pool(pool_id, &pool_admin); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment: I really like how the |
||
|
||
// Add bidirectional trading pair and fund both accounts | ||
let (investor, trader) = | ||
<T::TokenSwaps as OrderBookBenchmarkHelper>::bench_setup_trading_pair( | ||
CURRENCY_POOL.into(), | ||
CURRENCY_FOREIGN.into(), | ||
INVEST_AMOUNT_POOL_DENOMINATED.into(), | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
DECIMALS_POOL.into(), | ||
DECIMALS_FOREIGN.into(), | ||
); | ||
<T::TokenSwaps as OrderBookBenchmarkHelper>::bench_setup_trading_pair( | ||
CURRENCY_FOREIGN.into(), | ||
CURRENCY_POOL.into(), | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
INVEST_AMOUNT_POOL_DENOMINATED.into(), | ||
DECIMALS_FOREIGN.into(), | ||
DECIMALS_POOL.into(), | ||
); | ||
|
||
// Grant investor permissions | ||
<T::PoolInspect as PoolBenchmarkHelper>::bench_investor_setup( | ||
pool_id, | ||
investor.clone(), | ||
T::Balance::zero(), | ||
); | ||
let investment_id = | ||
<T::PoolInspect as InvestmentIdBenchmarkHelper>::bench_default_investment_id(pool_id); | ||
|
||
( | ||
investor, | ||
investment_id, | ||
CURRENCY_POOL.into(), | ||
CURRENCY_FOREIGN.into(), | ||
trader, | ||
) | ||
} | ||
|
||
fn bench_prep_foreign_investments_worst_case( | ||
investor: Self::AccountId, | ||
investment_id: Self::InvestmentId, | ||
pool_currency: Self::CurrencyId, | ||
foreign_currency: Self::CurrencyId, | ||
) { | ||
log::debug!( | ||
"Preparing worst case foreign investment benchmark setup with pool currency {:?} and foreign currency: {:?}", | ||
pool_currency, | ||
foreign_currency | ||
); | ||
|
||
// Create `InvestState::ActiveSwapIntoPoolCurrency` and prepare redemption for | ||
// collection by redeeming | ||
assert_ok!(Pallet::<T>::increase_foreign_investment( | ||
&investor, | ||
investment_id, | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
foreign_currency, | ||
pool_currency, | ||
)); | ||
assert_eq!( | ||
crate::InvestmentPaymentCurrency::<T>::get(&investor, investment_id).unwrap(), | ||
foreign_currency | ||
); | ||
|
||
log::debug!("Increasing foreign redemption"); | ||
assert_ok!(Pallet::<T>::increase_foreign_redemption( | ||
&investor, | ||
investment_id, | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
foreign_currency, | ||
)); | ||
assert_eq!( | ||
crate::RedemptionPayoutCurrency::<T>::get(&investor, investment_id).unwrap(), | ||
foreign_currency | ||
); | ||
|
||
// Process redemption such that collecting will trigger worst case | ||
let fulfillment: FulfillmentWithPrice<T::BalanceRatio> = FulfillmentWithPrice { | ||
of_amount: Perquintill::from_percent(50), | ||
price: Ratio::checked_from_rational(1, 4).unwrap().into(), | ||
}; | ||
assert_ok!(<T::Investment as OrderManager>::process_redeem_orders( | ||
investment_id | ||
)); | ||
assert_ok!(<T::Investment as OrderManager>::redeem_fulfillment( | ||
investment_id, | ||
fulfillment | ||
)); | ||
log::debug!("Worst case benchmark foreign investment setup done!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,7 @@ pub mod pallet { | |
|
||
/// Type for price ratio for cost of incoming currency relative to | ||
/// outgoing | ||
type Rate: Parameter | ||
type BalanceRatio: Parameter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for feature creeping. This change does not have any functional impact as it's just renaming. |
||
+ Member | ||
+ sp_runtime::FixedPointNumber | ||
+ sp_runtime::traits::EnsureMul | ||
|
@@ -171,7 +171,7 @@ pub mod pallet { | |
/// more sophisticated swap price discovery. For now, this should be set | ||
/// to one. | ||
#[pallet::constant] | ||
type DefaultTokenSellRate: Get<Self::Rate>; | ||
type DefaultTokenSellRatio: Get<Self::BalanceRatio>; | ||
|
||
/// The token swap order identifying type | ||
type TokenSwapOrderId: Parameter | ||
|
@@ -190,7 +190,7 @@ pub mod pallet { | |
Balance = Self::Balance, | ||
OrderId = Self::TokenSwapOrderId, | ||
OrderDetails = Swap<Self::Balance, Self::CurrencyId>, | ||
SellRatio = Self::Rate, | ||
SellRatio = Self::BalanceRatio, | ||
>; | ||
|
||
/// The hook type which acts upon a finalized investment decrement. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021 Centrifuge Foundation (centrifuge.io). | ||
// This file is part of 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. | ||
|
||
use cfg_traits::{ | ||
benchmarking::ForeignInvestmentBenchmarkHelper, | ||
investments::{ForeignInvestment, TrancheCurrency}, | ||
}; | ||
use frame_benchmarking::v2::*; | ||
|
||
use super::*; | ||
use crate::Pallet; | ||
|
||
#[benchmarks( | ||
where | ||
T::ForeignInvestment: ForeignInvestmentBenchmarkHelper<AccountId = T::AccountId, Balance = T::Balance, CurrencyId = T::CurrencyId, InvestmentId = T::TrancheCurrency>, | ||
T::Balance: From<u128>, | ||
T::AccountId: From<[u8; 32]> + Into<[u8; 32]>, | ||
)] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[benchmark] | ||
fn inbound_collect_redeem() { | ||
let (investor, investment_id, pool_currency, foreign_currency, _) = <T::ForeignInvestment as ForeignInvestmentBenchmarkHelper>::bench_prepare_foreign_investments_setup(); | ||
|
||
// Fund investor with foreign currency and tranche tokens | ||
T::Tokens::mint_into( | ||
investment_id.clone().into(), | ||
&investor, | ||
(u128::max_value() / 10).into(), | ||
)?; | ||
T::Tokens::mint_into(foreign_currency, &investor, (u128::max_value() / 10).into())?; | ||
|
||
// Increase investment and redemption | ||
<T::ForeignInvestment as ForeignInvestmentBenchmarkHelper>::bench_prep_foreign_investments_worst_case(investor.clone(), investment_id.clone(), pool_currency, foreign_currency); | ||
|
||
let investor_pointer = investor.clone(); | ||
let redeeming_amount = | ||
T::ForeignInvestment::redemption(&investor_pointer, investment_id.clone())?; | ||
let pool_id = investment_id.of_pool(); | ||
let tranche_id = investment_id.of_tranche(); | ||
let foreign_currency_index = Pallet::<T>::try_get_general_index(foreign_currency)?.into(); | ||
|
||
#[block] | ||
{ | ||
Pallet::<T>::handle_collect_redemption( | ||
pool_id, | ||
tranche_id, | ||
investor, | ||
foreign_currency_index, | ||
)?; | ||
} | ||
|
||
assert!( | ||
T::ForeignInvestment::redemption(&investor_pointer, investment_id)? < redeeming_amount | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a plain struct along with this trait can avoid errors when reading from these values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
239fa89