Skip to content

Commit

Permalink
allow and disallow currency tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jul 17, 2024
1 parent 07ff058 commit 8a4fc2a
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 394 deletions.
8 changes: 4 additions & 4 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ pub mod pallet {
pub fn validate_investment_currency(
currency_id: T::CurrencyId,
) -> Result<(u128, EVMChainId), DispatchError> {
let currency = Self::try_get_general_index(currency_id)?;

let (chain_id, ..) = Self::try_get_wrapped_token(&currency_id)?;

// Ensure the currency is enabled as pool_currency
let metadata =
T::AssetRegistry::metadata(&currency_id).ok_or(Error::<T>::AssetNotFound)?;
Expand All @@ -931,10 +935,6 @@ pub mod pallet {
Error::<T>::AssetMetadataNotPoolCurrency
);

let currency = Self::try_get_general_index(currency_id)?;

let (chain_id, ..) = Self::try_get_wrapped_token(&currency_id)?;

Ok((currency, chain_id))
}

Expand Down
290 changes: 290 additions & 0 deletions pallets/liquidity-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ mod util {
}
}

pub fn pool_locatable_transferable_metadata() -> AssetMetadata {
AssetMetadata {
additional: CustomMetadata {
pool_currency: true,
..transferable_metadata().additional
},
..locatable_transferable_metadata()
}
}

pub fn currency_index(currency_id: CurrencyId) -> u128 {
GeneralCurrencyIndexOf::<Runtime>::try_from(currency_id)
.unwrap()
Expand Down Expand Up @@ -958,6 +968,286 @@ mod add_currency {
}
}

mod allow_investment_currency {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(util::pool_locatable_transferable_metadata()));
Permissions::mock_has(move |scope, who, role| {
assert_eq!(who, ALICE);
assert!(matches!(scope, PermissionScope::Pool(POOL_ID)));
assert!(matches!(role, Role::PoolRole(PoolRole::PoolAdmin)));
true
});
Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain());
assert_eq!(
msg,
Message::AllowInvestmentCurrency {
pool_id: POOL_ID,
currency: util::currency_index(CURRENCY_ID),
}
);
Ok(())
});

assert_ok!(LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
));
})
}

mod erroring_out {
use super::*;

#[test]
fn with_wrong_permissions() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| false);

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::NotPoolAdmin
);
})
}

#[test]
fn with_no_metadata() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| None);

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotFound,
);
})
}

#[test]
fn with_unsupported_token() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CurrencyId::Native
),
TokenError::Unsupported,
);
})
}

#[test]
fn with_no_transferible_asset() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotLiquidityPoolsTransferable,
);
})
}

#[test]
fn with_wrong_location() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::transferable_metadata()));

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotLiquidityPoolsWrappedToken
);
})
}

#[test]
fn with_wrong_pool_currency() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::locatable_transferable_metadata()));

assert_noop!(
LiquidityPools::allow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetMetadataNotPoolCurrency
);
})
}
}
}

mod disallow_investment_currency {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(util::pool_locatable_transferable_metadata()));
Permissions::mock_has(move |scope, who, role| {
assert_eq!(who, ALICE);
assert!(matches!(scope, PermissionScope::Pool(POOL_ID)));
assert!(matches!(role, Role::PoolRole(PoolRole::PoolAdmin)));
true
});
Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain());
assert_eq!(
msg,
Message::DisallowInvestmentCurrency {
pool_id: POOL_ID,
currency: util::currency_index(CURRENCY_ID),
}
);
Ok(())
});

assert_ok!(LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
));
})
}

mod erroring_out {
use super::*;

#[test]
fn with_wrong_permissions() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| false);

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::NotPoolAdmin
);
})
}

#[test]
fn with_no_metadata() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| None);

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotFound,
);
})
}

#[test]
fn with_unsupported_token() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CurrencyId::Native
),
TokenError::Unsupported,
);
})
}

#[test]
fn with_no_transferible_asset() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotLiquidityPoolsTransferable,
);
})
}

#[test]
fn with_wrong_location() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::transferable_metadata()));

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetNotLiquidityPoolsWrappedToken
);
})
}

#[test]
fn with_wrong_pool_currency() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
AssetRegistry::mock_metadata(|_| Some(util::locatable_transferable_metadata()));

assert_noop!(
LiquidityPools::disallow_investment_currency(
RuntimeOrigin::signed(ALICE),
POOL_ID,
CURRENCY_ID
),
Error::<Runtime>::AssetMetadataNotPoolCurrency
);
})
}
}
}

#[test]
fn receiving_invalid_message() {
System::externalities().execute_with(|| {
Expand Down
Loading

0 comments on commit 8a4fc2a

Please sign in to comment.