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

Reactor: Decoupling pool-registry from pool-system and pool-fees #1983

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
12 changes: 0 additions & 12 deletions Cargo.lock

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

79 changes: 78 additions & 1 deletion libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::{
investments::InvestmentAccountant, PoolInspect, PoolReserve, Seconds, TrancheTokenPrice,
investments::InvestmentAccountant, PoolInspect, PoolMutate, PoolReserve, Seconds,
TrancheTokenPrice, UpdateState,
};
use cfg_types::investments::InvestmentInfo;
use frame_support::pallet_prelude::*;
Expand Down Expand Up @@ -224,4 +225,80 @@ pub mod pallet {
execute_call!(a)
}
}

/// Mutability capabilities to this mock
pub trait ConfigMut: Config {
type TrancheInput: Encode + Decode + Clone + TypeInfo + Debug + PartialEq;
type PoolChanges: Encode + Decode + Clone + TypeInfo + Debug + PartialEq + MaxEncodedLen;
type PoolFeeInput: Encode + Decode + Clone + TypeInfo;
type MaxTranches: Get<u32>;
type MaxFeesPerPool: Get<u32>;
}

impl<T: ConfigMut> Pallet<T> {
pub fn mock_create(
func: impl Fn(
T::AccountId,
T::AccountId,
T::PoolId,
BoundedVec<T::TrancheInput, T::MaxTranches>,
T::CurrencyId,
T::Balance,
BoundedVec<T::PoolFeeInput, T::MaxFeesPerPool>,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d, e, f, g)| func(a, b, c, d, e, f, g));
}

pub fn mock_update(
f: impl Fn(T::PoolId, T::PoolChanges) -> Result<UpdateState, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_execute_update(f: impl Fn(T::PoolId) -> Result<u32, DispatchError> + 'static) {
register_call!(f);
}

#[cfg(feature = "runtime-benchmarks")]
pub fn mock_worst_pool_changes(f: impl Fn(Option<u32>) -> T::PoolChanges + 'static) {
register_call!(f);
}
}

impl<T: ConfigMut> PoolMutate<T::AccountId, T::PoolId> for Pallet<T> {
type Balance = T::Balance;
type CurrencyId = T::CurrencyId;
type MaxFeesPerPool = T::MaxFeesPerPool;
type MaxTranches = T::MaxTranches;
type PoolChanges = T::PoolChanges;
type PoolFeeInput = T::PoolFeeInput;
type TrancheInput = T::TrancheInput;

fn create(
a: T::AccountId,
b: T::AccountId,
c: T::PoolId,
d: BoundedVec<T::TrancheInput, T::MaxTranches>,
e: T::CurrencyId,
f: T::Balance,
g: BoundedVec<T::PoolFeeInput, T::MaxFeesPerPool>,
) -> DispatchResult {
execute_call!((a, b, c, d, e, f, g))
}

fn update(a: T::PoolId, b: T::PoolChanges) -> Result<UpdateState, DispatchError> {
execute_call!((a, b))
}

fn execute_update(a: T::PoolId) -> Result<u32, DispatchError> {
execute_call!(a)
}

#[cfg(feature = "runtime-benchmarks")]
fn worst_pool_changes(a: Option<u32>) -> Self::PoolChanges {
execute_call!(a)
}
}
}
25 changes: 0 additions & 25 deletions libs/traits/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_std::{fmt::Debug, vec::Vec};

use crate::fee::PoolFeeBucket;

/// Benchmark utility to create pools
pub trait PoolBenchmarkHelper {
type PoolId;
Expand Down Expand Up @@ -51,22 +45,3 @@ pub trait InvestmentIdBenchmarkHelper {
/// Return the default investment id for the given pool.
fn bench_default_investment_id(pool_id: Self::PoolId) -> Self::InvestmentId;
}

/// Benchmark utility for adding pool fees
pub trait PoolFeesBenchmarkHelper {
type PoolFeeInfo: Encode + Decode + Clone + TypeInfo + Debug;
type PoolId: Encode + Decode + Clone + TypeInfo + Debug;

/// Generate n default fixed pool fees and return their info
fn get_pool_fee_infos(n: u32) -> Vec<Self::PoolFeeInfo>;

/// Add the default fixed fee `n` times to the given pool and bucket pair
fn add_pool_fees(pool_id: Self::PoolId, bucket: PoolFeeBucket, n: u32);

/// Get the fee info for a fixed pool fee which takes 1% of the NAV
fn get_default_fixed_fee_info() -> Self::PoolFeeInfo;

/// Get the fee info for a chargeable pool fee which can be charged up to
/// 1000u128 per second
fn get_default_charged_fee_info() -> Self::PoolFeeInfo;
}
19 changes: 12 additions & 7 deletions libs/traits/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use frame_support::pallet_prelude::TypeInfo;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::DispatchResult;
use sp_runtime::{traits::Get, DispatchResult};
use strum::{EnumCount, EnumIter};

/// The priority segregation of pool fees
Expand All @@ -30,10 +30,7 @@ pub enum PoolFeeBucket {
}

/// Trait to add fees to a pool
pub trait PoolFeesMutate {
type PoolId;
type FeeInfo;

pub trait PoolFeesMutate: PoolFeesInspect {
/// Add a new fee to the pool and bucket.
///
/// NOTE: Assumes call permissions are separately checked beforehand.
Expand All @@ -44,8 +41,10 @@ pub trait PoolFeesMutate {
pub trait PoolFeesInspect {
type PoolId;

/// Returns the maximum number of pool fees required for accurate weights
fn get_max_fee_count() -> u32;
type FeeInfo;

/// Maximum number of pool fees required for accurate weights
type MaxFeesPerPool: Get<u32>;

/// Returns the maximum number of pool fees per bucket required for accurate
/// weights
Expand All @@ -57,6 +56,12 @@ pub trait PoolFeesInspect {
/// Returns the current amount of active fees for the given pool and bucket
/// pair
fn get_pool_fee_bucket_count(pool: Self::PoolId, bucket: PoolFeeBucket) -> u32;

/// Worst list of pool fee info
#[cfg(feature = "runtime-benchmarks")]
fn worst_pool_fee_infos(
n: u32,
) -> frame_support::BoundedVec<Self::FeeInfo, Self::MaxFeesPerPool>;
}

/// Trait to prorate a fee amount to a rate or amount
Expand Down
95 changes: 40 additions & 55 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ use frame_support::{
dispatch::DispatchResult,
pallet_prelude::{RuntimeDebug, TypeInfo},
traits::UnixTime,
Parameter,
BoundedVec, Parameter,
};
use impl_trait_for_tuples::impl_for_tuples;
use orml_traits::asset_registry;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{traits::Member, DispatchError};
use sp_std::{fmt::Debug, marker::PhantomData, vec::Vec};
use sp_runtime::{
traits::{Get, Member},
DispatchError,
};
use sp_std::{fmt::Debug, marker::PhantomData};

pub mod changes;
pub mod data;
Expand Down Expand Up @@ -96,7 +99,6 @@ pub trait TrancheTokenPrice<AccountId, CurrencyId> {
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub enum UpdateState {
NoExecution,
Executed(u32),
Stored(u32),
}

Expand All @@ -106,72 +108,55 @@ pub trait PoolMutate<AccountId, PoolId> {
type CurrencyId;
type TrancheInput: Encode + Decode + Clone + TypeInfo + Debug + PartialEq;
type PoolChanges: Encode + Decode + Clone + TypeInfo + Debug + PartialEq + MaxEncodedLen;
type PoolFeeInput: Encode + Decode + Clone + TypeInfo + Debug;
type PoolFeeInput: Encode + Decode + Clone + TypeInfo;
type MaxTranches: Get<u32>;
type MaxFeesPerPool: Get<u32>;

fn create(
admin: AccountId,
depositor: AccountId,
pool_id: PoolId,
tranche_inputs: Vec<Self::TrancheInput>,
tranche_inputs: BoundedVec<Self::TrancheInput, Self::MaxTranches>,
currency: Self::CurrencyId,
max_reserve: Self::Balance,
pool_fees: Vec<Self::PoolFeeInput>,
pool_fees: BoundedVec<Self::PoolFeeInput, Self::MaxFeesPerPool>,
) -> DispatchResult;

fn update(pool_id: PoolId, changes: Self::PoolChanges) -> Result<UpdateState, DispatchError>;

fn execute_update(pool_id: PoolId) -> Result<u32, DispatchError>;
}

/// A trait that supports retrieval and mutation of pool and tranche token
/// metadata.
pub trait PoolMetadata<Balance, VersionedMultiLocation> {
type AssetMetadata;
type CustomMetadata;
type PoolMetadata;
type PoolId: Parameter
+ Member
+ Debug
+ Copy
+ Default
+ TypeInfo
+ Encode
+ Decode
+ MaxEncodedLen;
type TrancheId: Parameter + Member + Debug + Copy + Default + TypeInfo + MaxEncodedLen;

/// Get the metadata of the given pool.
fn get_pool_metadata(pool_id: Self::PoolId) -> Result<Self::PoolMetadata, DispatchError>;

/// Set the metadata of the given pool.
fn set_pool_metadata(pool_id: Self::PoolId, metadata: Vec<u8>) -> DispatchResult;

/// Get the metadata of the given pair of pool and tranche id.
fn get_tranche_token_metadata(
pool_id: Self::PoolId,
tranche: Self::TrancheId,
) -> Result<Self::AssetMetadata, DispatchError>;
/// A worst case list of tranches
#[cfg(feature = "runtime-benchmarks")]
fn worst_tranche_input_list(
_num_tranches: u32,
) -> BoundedVec<Self::TrancheInput, Self::MaxTranches> {
Default::default()
}

/// Register the metadata for the currency derived from the given pair of
/// pool id and tranche.
fn create_tranche_token_metadata(
pool_id: Self::PoolId,
tranche: Self::TrancheId,
metadata: Self::AssetMetadata,
) -> DispatchResult;
/// A worst case list of pool fees
#[cfg(feature = "runtime-benchmarks")]
fn worst_fee_input_list(
_num_pool_fees: u32,
) -> BoundedVec<Self::PoolFeeInput, Self::MaxFeesPerPool> {
Default::default()
}

#[allow(clippy::too_many_arguments)]
/// Update the metadata of the given pair of pool and tranche id.
fn update_tranche_token_metadata(
pool_id: Self::PoolId,
tranche: Self::TrancheId,
decimals: Option<u32>,
name: Option<Vec<u8>>,
symbol: Option<Vec<u8>>,
existential_deposit: Option<Balance>,
location: Option<Option<VersionedMultiLocation>>,
additional: Option<Self::CustomMetadata>,
) -> DispatchResult;
/// A worst case change
#[cfg(feature = "runtime-benchmarks")]
fn worst_pool_changes(_num_tranches: Option<u32>) -> Self::PoolChanges;

/// Returns a valid currency to be used in the pool
#[cfg(feature = "runtime-benchmarks")]
fn register_pool_currency(_: &Self::CurrencyId) {}

/// Satisfy the account preconditions as a depositor
#[cfg(feature = "runtime-benchmarks")]
fn fund_depositor(_: &AccountId) {}

/// Creates the heaviest pool possible
#[cfg(feature = "runtime-benchmarks")]
fn create_heaviest_pool(_: PoolId, _: AccountId, _: Self::CurrencyId, _num_tranches: u32) {}
}

/// A trait that support pool reserve operations such as withdraw and deposit
Expand Down
14 changes: 0 additions & 14 deletions libs/types/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ pub struct TrancheMetadata<StringLimit: Get<u32>> {
pub token_symbol: BoundedVec<u8, StringLimit>,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PoolMetadata<MetaSize>
where
MetaSize: Get<u32>,
{
pub metadata: BoundedVec<u8, MetaSize>,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum PoolRegistrationStatus {
Registered,
Unregistered,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PoolNav<Balance> {
pub nav_aum: Balance,
Expand Down
4 changes: 2 additions & 2 deletions pallets/pool-fees/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// GNU General Public License for more details.

use cfg_traits::{
benchmarking::{PoolBenchmarkHelper, PoolFeesBenchmarkHelper},
benchmarking::PoolBenchmarkHelper,
changes::ChangeGuard,
fee::{PoolFeeBucket, PoolFeesMutate as _},
};
Expand Down Expand Up @@ -166,5 +166,5 @@ where
let pool_id = T::PoolId::default();
T::ChangeGuard::bench_create_pool(pool_id, &pool_admin);

<PoolFees<T> as PoolFeesBenchmarkHelper>::add_pool_fees(pool_id, PoolFeeBucket::Top, n);
PoolFees::<T>::add_pool_fees(pool_id, PoolFeeBucket::Top, n);
}
Loading
Loading