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

Fix/linear pricing #1812

Merged
merged 25 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3df5900
feat: restrict epoch closing to liquiduty admin
mustermeiszer Apr 15, 2024
e004106
fix: linear pricing
mustermeiszer Apr 15, 2024
7b4ab74
proof: type unsafety
mustermeiszer Apr 15, 2024
9ad611b
revert: type unsafety
mustermeiszer Apr 15, 2024
9dc56e5
fix: tests and adapt logic to allow dyanmic input
mustermeiszer Apr 16, 2024
ca5ab9d
Merge branch 'main' into fix/linear-pricing
mustermeiszer Apr 16, 2024
c002271
chore: generic removal
mustermeiszer Apr 16, 2024
ec1a96b
chore: cleaner logic
mustermeiszer Apr 16, 2024
2c2edc0
fix: Clippy you annoying thing
mustermeiszer Apr 16, 2024
db35635
fix: cliiiiiippyyyyy
mustermeiszer Apr 16, 2024
b1ef424
fix: cliippyy v1m
mustermeiszer Apr 16, 2024
b4854b1
feat: make PoolAdmin configurable
mustermeiszer May 6, 2024
2aca1dd
fix: benchmarks
mustermeiszer May 6, 2024
c978951
feat: make pricing linear optional
mustermeiszer May 6, 2024
e2dd3ca
Merge remote-tracking branch 'origin/main' into fix/linear-pricing
mustermeiszer May 6, 2024
9265bbb
Fix: linear accrual not overpassing maturity (#1842)
lemunozm May 23, 2024
967584d
increment coverage when loan is at maturity date
lemunozm May 23, 2024
2a691e9
fix division_by_zero issue
lemunozm May 23, 2024
45be927
feat: fix linear pricing migration (#1838)
wischli May 23, 2024
8c01edc
Merge remote-tracking branch 'origin/main' into fix/linear-pricing
mustermeiszer May 24, 2024
3001d9d
fix: make mock accept all origins
mustermeiszer May 24, 2024
b831f99
feat: add test for switching between oracle or settlement price
mustermeiszer May 24, 2024
5fee771
fix: not needed where clause
mustermeiszer May 24, 2024
fcddabf
feat: move additional test
mustermeiszer May 24, 2024
b6976c8
fix: nits
mustermeiszer May 24, 2024
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
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)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I didn't know about EnsureOriginWithArg. Probably, I should have used it in oracle-collection instead of PreConditions there.


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
Loading