use cfg_primitives::{AccountId, Balance, InvestmentId, PoolId};
use cfg_traits::{Permissions, PreConditions};
use cfg_types::{
permissions::{PermissionScope, PoolRole, Role},
tokens::CurrencyId,
};
use frame_support::{dispatch::DispatchResult, traits::UnixTime};
use pallet_investments::OrderType;
use sp_runtime::DispatchError;
use sp_std::marker::PhantomData;
pub struct PoolAdminCheck<P>(PhantomData<P>);
impl<P> PreConditions<(AccountId, PoolId)> for PoolAdminCheck<P>
where
P: Permissions<AccountId, Scope = PermissionScope<PoolId, CurrencyId>, Role = Role>,
{
type Result = bool;
fn check((account_id, pool_id): (AccountId, PoolId)) -> bool {
P::has(
PermissionScope::Pool(pool_id),
account_id,
Role::PoolRole(PoolRole::PoolAdmin),
)
}
#[cfg(feature = "runtime-benchmarks")]
fn satisfy((account_id, pool_id): (AccountId, PoolId)) {
P::add(
PermissionScope::Pool(pool_id),
account_id,
Role::PoolRole(PoolRole::PoolAdmin),
)
.unwrap();
}
}
pub struct IsUnfrozenTrancheInvestor<P, T>(PhantomData<(P, T)>);
impl<
P: Permissions<AccountId, Scope = PermissionScope<PoolId, CurrencyId>, Role = Role>,
T: UnixTime,
> PreConditions<OrderType<AccountId, InvestmentId, Balance>> for IsUnfrozenTrancheInvestor<P, T>
{
type Result = DispatchResult;
fn check(order: OrderType<AccountId, InvestmentId, Balance>) -> Self::Result {
let is_tranche_investor = match order {
OrderType::Investment {
who,
investment_id: (pool_id, tranche_id),
..
} => {
P::has(
PermissionScope::Pool(pool_id),
who.clone(),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, T::now().as_secs())),
) && !P::has(
PermissionScope::Pool(pool_id),
who,
Role::PoolRole(PoolRole::FrozenTrancheInvestor(tranche_id)),
)
}
OrderType::Redemption {
who,
investment_id: (pool_id, tranche_id),
..
} => {
P::has(
PermissionScope::Pool(pool_id),
who.clone(),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, T::now().as_secs())),
) && !P::has(
PermissionScope::Pool(pool_id),
who,
Role::PoolRole(PoolRole::FrozenTrancheInvestor(tranche_id)),
)
}
};
if is_tranche_investor || cfg!(feature = "runtime-benchmarks") {
Ok(())
} else {
Err(DispatchError::Other(
"Account does not have the TrancheInvestor permission.",
))
}
}
}