Skip to content

Commit

Permalink
feat: make PoolAdmin configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer committed May 6, 2024
1 parent b1ef424 commit b4854b1
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 56 deletions.
17 changes: 16 additions & 1 deletion pallets/pool-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_support::{
dispatch::DispatchResult,
pallet_prelude::DispatchError,
parameter_types,
traits::{Contains, Hooks, PalletInfoAccess, SortedMembers},
traits::{Contains, EnsureOriginWithArg, Hooks, PalletInfoAccess, SortedMembers},
PalletId,
};
use frame_system::EnsureSigned;
Expand Down Expand Up @@ -115,7 +115,22 @@ impl cfg_test_utils::mocks::nav::Config for Test {
type PoolId = PoolId;
}

pub struct All;
impl EnsureOriginWithArg<RuntimeOrigin, PoolId> for All {
type Success = ();

fn try_origin(_: RuntimeOrigin, _: &PoolId) -> Result<Self::Success, RuntimeOrigin> {
Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_: &PoolId) -> Result<RuntimeOrigin, ()> {
Ok(RuntimeOrigin::root())
}
}

impl pallet_pool_system::Config for Test {
type AdminOrigin = All;
type AssetRegistry = RegistryMock;
type AssetsUnderManagementNAV = FakeNav;
type Balance = Balance;
Expand Down
24 changes: 5 additions & 19 deletions pallets/pool-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub mod pallet {
use frame_support::{
pallet_prelude::*,
sp_runtime::traits::Convert,
traits::{tokens::Preservation, Contains},
traits::{tokens::Preservation, Contains, EnsureOriginWithArg},
PalletId,
};
use sp_runtime::{traits::BadOrigin, ArithmeticError};
Expand All @@ -195,6 +195,8 @@ pub mod pallet {
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

type AdminOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, Self::PoolId>;

type Balance: Member
+ Parameter
+ AtLeast32BitUnsigned
Expand Down Expand Up @@ -613,15 +615,7 @@ pub mod pallet {
#[transactional]
#[pallet::call_index(1)]
pub fn close_epoch(origin: OriginFor<T>, pool_id: T::PoolId) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(
T::Permission::has(
PermissionScope::Pool(pool_id),
who,
Role::PoolRole(PoolRole::LiquidityAdmin)
),
BadOrigin
);
T::AdminOrigin::ensure_origin(origin, &pool_id)?;

Pool::<T>::try_mutate(pool_id, |pool| {
let pool = pool.as_mut().ok_or(Error::<T>::NoSuchPool)?;
Expand Down Expand Up @@ -884,15 +878,7 @@ pub mod pallet {
origin: OriginFor<T>,
pool_id: T::PoolId,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(
T::Permission::has(
PermissionScope::Pool(pool_id),
who,
Role::PoolRole(PoolRole::LiquidityAdmin)
),
BadOrigin
);
T::AdminOrigin::ensure_origin(origin, &pool_id)?;

EpochExecution::<T>::try_mutate(pool_id, |epoch_info| {
let epoch = epoch_info
Expand Down
40 changes: 33 additions & 7 deletions pallets/pool-system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use cfg_types::{
tokens::{CurrencyId, CustomMetadata, TrancheCurrency},
};
use frame_support::{
assert_ok, derive_impl, parameter_types,
traits::{Contains, Hooks, PalletInfoAccess, SortedMembers},
assert_ok, derive_impl,
dispatch::RawOrigin,
parameter_types,
traits::{Contains, EnsureOriginWithArg, Hooks, PalletInfoAccess, SortedMembers},
Blake2_128, PalletId, StorageHasher,
};
use frame_system::{EnsureSigned, EnsureSignedBy};
Expand All @@ -37,11 +39,8 @@ use pallet_pool_fees::PoolFeeInfoOf;
use pallet_restricted_tokens::TransferDetails;
use parity_scale_codec::Encode;
use sp_arithmetic::FixedPointNumber;
use sp_core::H256;
use sp_runtime::{
traits::{ConstU128, Zero},
BuildStorage,
};
use sp_core::{ConstU128, H256};
use sp_runtime::{traits::Zero, BuildStorage};
use sp_std::marker::PhantomData;

use crate::{
Expand Down Expand Up @@ -385,7 +384,34 @@ parameter_types! {
pub const PoolDeposit: Balance = 1 * CURRENCY;
}

pub struct LiquidityAndPoolAdmin;
impl EnsureOriginWithArg<RuntimeOrigin, PoolId> for LiquidityAndPoolAdmin {
type Success = ();

fn try_origin(o: RuntimeOrigin, _: &PoolId) -> Result<Self::Success, RuntimeOrigin> {
<RuntimeOrigin as Into<Result<RawOrigin<AccountId>, RuntimeOrigin>>>::into(o).and_then(
|r| match r {
RawOrigin::Root => Ok(()),
RawOrigin::Signed(account) => {
if account == DEFAULT_POOL_OWNER {
Ok(())
} else {
Err(RawOrigin::Signed(account).into())
}
}
RawOrigin::None => Err(RawOrigin::None.into()),
},
)
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_: &PoolId) -> Result<RuntimeOrigin, ()> {
Ok(RuntimeOrigin::Signed(DEFAULT_POOL_OWNER))
}
}

impl Config for Runtime {
type AdminOrigin = LiquidityAndPoolAdmin;
type AssetRegistry = RegistryMock;
type AssetsUnderManagementNAV = FakeNav;
type Balance = Balance;
Expand Down
57 changes: 28 additions & 29 deletions pallets/pool-system/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ fn pool_constraints_pass() {
#[test]
fn epoch() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);
let borrower = 3;

Expand Down Expand Up @@ -744,7 +744,7 @@ fn epoch() {
#[test]
fn submission_period() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -932,7 +932,7 @@ fn submission_period() {
#[test]
fn execute_info_removed_after_epoch_execute() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -1021,7 +1021,7 @@ fn execute_info_removed_after_epoch_execute() {
#[test]
fn pool_updates_should_be_constrained() {
new_test_ext().execute_with(|| {
let pool_owner = 0_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);
let pool_id = 0;

Expand Down Expand Up @@ -1556,7 +1556,7 @@ fn valid_tranche_structure_is_enforced() {
#[test]
fn triger_challange_period_with_zero_solution() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -1650,7 +1650,7 @@ fn triger_challange_period_with_zero_solution() {
#[test]
fn min_challenge_time_is_respected() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -1747,7 +1747,7 @@ fn min_challenge_time_is_respected() {
#[test]
fn only_zero_solution_is_accepted_max_reserve_violated() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -1948,7 +1948,7 @@ fn only_zero_solution_is_accepted_max_reserve_violated() {
#[test]
fn only_zero_solution_is_accepted_when_risk_buff_violated_else() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;
let pool_owner_origin = RuntimeOrigin::signed(pool_owner);

// Initialize pool with initial investments
Expand Down Expand Up @@ -2138,7 +2138,7 @@ fn only_zero_solution_is_accepted_when_risk_buff_violated_else() {
#[test]
fn only_usd_as_pool_currency_allowed() {
new_test_ext().execute_with(|| {
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;

// Initialize pool with initial investments
let senior_interest_rate = Rate::saturating_from_rational(10, 100)
Expand Down Expand Up @@ -2331,7 +2331,7 @@ fn creation_takes_deposit() {
// Pool creation one:
// Owner 2, first deposit
// total deposit for this owner is 1
let pool_owner = 2_u64;
let pool_owner = DEFAULT_POOL_OWNER;

assert_ok!(PoolSystem::create(
pool_owner.clone(),
Expand Down Expand Up @@ -2742,7 +2742,6 @@ mod pool_fees {
use super::*;
use crate::{mock::default_pool_fees, Event};

const POOL_OWNER: AccountId = 2;
const INVESTMENT_AMOUNT: Balance = DEFAULT_POOL_MAX_RESERVE / 10;
const NAV_AMOUNT: Balance = INVESTMENT_AMOUNT / 2 + 2_345_000;
const FEE_AMOUNT_FIXED: Balance = NAV_AMOUNT / 10;
Expand All @@ -2761,8 +2760,8 @@ mod pool_fees {
let senior_interest_rate =
interest_rate / Rate::saturating_from_integer(SECONDS_PER_YEAR) + One::one();
assert_ok!(PoolSystem::create(
POOL_OWNER,
POOL_OWNER,
DEFAULT_POOL_OWNER,
DEFAULT_POOL_OWNER,
DEFAULT_POOL_ID,
vec![
TrancheInput {
Expand Down Expand Up @@ -2854,11 +2853,11 @@ mod pool_fees {
INVESTMENT_AMOUNT
));
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
assert_ok!(PoolSystem::submit_solution(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID,
vec![
TrancheSolution {
Expand All @@ -2874,7 +2873,7 @@ mod pool_fees {

// Execute epoch 1 should reduce reserve due to redemption
assert_ok!(PoolSystem::execute_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID
));
assert!(!EpochExecution::<Runtime>::contains_key(DEFAULT_POOL_ID));
Expand Down Expand Up @@ -2902,7 +2901,7 @@ mod pool_fees {
// Closing epoch 2 should not change anything but reserve.available
next_block();
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
assert_eq!(
Expand Down Expand Up @@ -2985,7 +2984,7 @@ mod pool_fees {
));
next_block();
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
assert_eq!(
Expand Down Expand Up @@ -3038,7 +3037,7 @@ mod pool_fees {
// Executing epoch should reduce FeeNav by disbursement and transfer from
// PoolFees account to destination
assert_ok!(PoolSystem::submit_solution(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID,
vec![
TrancheSolution {
Expand All @@ -3052,7 +3051,7 @@ mod pool_fees {
]
));
assert_ok!(PoolSystem::execute_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID
));
assert!(!EpochExecution::<Runtime>::contains_key(DEFAULT_POOL_ID));
Expand Down Expand Up @@ -3087,7 +3086,7 @@ mod pool_fees {
next_block();
test_nav_up(DEFAULT_POOL_ID, new_nav_amount - NAV_AMOUNT);
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));

Expand Down Expand Up @@ -3140,7 +3139,7 @@ mod pool_fees {

// NAV = 0 + AUM - PoolFeesNAV = -AUM
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
assert!(System::events().iter().any(|e| match e.event {
Expand Down Expand Up @@ -3209,7 +3208,7 @@ mod pool_fees {
// Closing should update fee nav
next_block();
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
let fee_amount_from_charge =
Expand Down Expand Up @@ -3241,7 +3240,7 @@ mod pool_fees {

// Executin should reduce fee_nav by disbursement and transfer
assert_ok!(PoolSystem::submit_solution(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID,
vec![
TrancheSolution {
Expand All @@ -3255,7 +3254,7 @@ mod pool_fees {
]
));
assert_ok!(PoolSystem::execute_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID
));
assert_eq!(
Expand Down Expand Up @@ -3327,7 +3326,7 @@ mod pool_fees {
// Closing should update fee nav
next_block();
assert_ok!(PoolSystem::close_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
0
));
assert_eq!(
Expand All @@ -3344,7 +3343,7 @@ mod pool_fees {
// by fees
assert_noop!(
PoolSystem::submit_solution(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID,
vec![
TrancheSolution {
Expand All @@ -3360,7 +3359,7 @@ mod pool_fees {
Error::<Runtime>::InsufficientCurrency
);
assert_ok!(PoolSystem::submit_solution(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID,
vec![
TrancheSolution {
Expand All @@ -3374,7 +3373,7 @@ mod pool_fees {
]
));
assert_ok!(PoolSystem::execute_epoch(
RuntimeOrigin::signed(POOL_OWNER),
RuntimeOrigin::signed(DEFAULT_POOL_OWNER),
DEFAULT_POOL_ID
));
assert_pending_fees(DEFAULT_POOL_ID, fees.clone(), vec![(fee_nav, 0, None)]);
Expand Down
Loading

0 comments on commit b4854b1

Please sign in to comment.