Skip to content

Commit

Permalink
add add_tranche tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 20, 2024
1 parent d5e46fe commit 91b79bb
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 120 deletions.
5 changes: 0 additions & 5 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,6 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

ensure!(
T::PoolInspect::tranche_exists(pool_id, tranche_id),
Error::<T>::TrancheNotFound
);

ensure!(
T::Permission::has(
PermissionScope::Pool(pool_id),
Expand Down
219 changes: 176 additions & 43 deletions pallets/liquidity-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use cfg_traits::{liquidity_pools::InboundQueue, Millis};
use cfg_types::{
domain_address::DomainAddress,
permissions::{PermissionScope, PoolRole, Role},
tokens::{
default_metadata, AssetMetadata, CrossChainTransferability, CurrencyId, CustomMetadata,
},
tokens::{AssetMetadata, CrossChainTransferability, CurrencyId, CustomMetadata},
};
use cfg_utils::vec_to_fixed_array;
use frame_support::{
assert_noop, assert_ok,
traits::{fungibles::Mutate as _, PalletInfo as _},
Expand All @@ -29,39 +28,55 @@ const CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1);
const POOL_ID: PoolId = 1;
const TRANCHE_ID: TrancheId = [1; 16];
const NOW: Millis = 0;
const NAME: &[u8] = b"Token name";
const SYMBOL: &[u8] = b"Token symbol";
const DECIMALS: u8 = 6;

fn transferable_metadata() -> AssetMetadata {
AssetMetadata {
additional: CustomMetadata {
transferability: CrossChainTransferability::LiquidityPools,
..Default::default()
},
..default_metadata()
mod util {
use super::*;

pub fn default_metadata() -> AssetMetadata {
AssetMetadata {
decimals: DECIMALS as u32,
name: Vec::from(NAME).try_into().unwrap(),
symbol: Vec::from(SYMBOL).try_into().unwrap(),
..cfg_types::tokens::default_metadata()
}
}
}

fn wrapped_transferable_metadata() -> AssetMetadata {
let pallet_index = PalletInfo::index::<LiquidityPools>();
AssetMetadata {
location: Some(VersionedLocation::V4(Location::new(
0,
[
PalletInstance(pallet_index.unwrap() as u8),
GlobalConsensus(NetworkId::Ethereum { chain_id: CHAIN_ID }),
AccountKey20 {
network: None,
key: CONTRACT_ACCOUNT,
},
],
))),
..transferable_metadata()
pub fn transferable_metadata() -> AssetMetadata {
AssetMetadata {
additional: CustomMetadata {
transferability: CrossChainTransferability::LiquidityPools,
..Default::default()
},
..default_metadata()
}
}
}

fn currency_index(currency_id: CurrencyId) -> u128 {
GeneralCurrencyIndexOf::<Runtime>::try_from(currency_id)
.unwrap()
.index
pub fn wrapped_transferable_metadata() -> AssetMetadata {
let pallet_index = PalletInfo::index::<LiquidityPools>();
AssetMetadata {
location: Some(VersionedLocation::V4(Location::new(
0,
[
PalletInstance(pallet_index.unwrap() as u8),
GlobalConsensus(NetworkId::Ethereum { chain_id: CHAIN_ID }),
AccountKey20 {
network: None,
key: CONTRACT_ACCOUNT,
},
],
))),
..transferable_metadata()
}
}

pub fn currency_index(currency_id: CurrencyId) -> u128 {
GeneralCurrencyIndexOf::<Runtime>::try_from(currency_id)
.unwrap()
.index
}
}

mod transfer {
Expand All @@ -70,7 +85,7 @@ mod transfer {
#[test]
fn success() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(wrapped_transferable_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::wrapped_transferable_metadata()));
TransferFilter::mock_check(|_| Ok(()));
Tokens::mint_into(CURRENCY_ID, &ALICE, AMOUNT).unwrap();
Gateway::mock_submit(|sender, destination, msg| {
Expand All @@ -79,7 +94,7 @@ mod transfer {
assert_eq!(
msg,
Message::Transfer {
currency: currency_index(CURRENCY_ID),
currency: util::currency_index(CURRENCY_ID),
sender: ALICE.into(),
receiver: EVM_ADDRESS.address(),
amount: AMOUNT
Expand Down Expand Up @@ -150,7 +165,7 @@ mod transfer {
#[test]
fn with_unsupported_token() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(default_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::transfer(
Expand All @@ -167,7 +182,7 @@ mod transfer {
#[test]
fn with_no_transferible_asset() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(default_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));

assert_noop!(
LiquidityPools::transfer(
Expand All @@ -184,7 +199,7 @@ mod transfer {
#[test]
fn with_wrong_location() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(transferable_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::transferable_metadata()));

assert_noop!(
LiquidityPools::transfer(
Expand All @@ -201,7 +216,7 @@ mod transfer {
#[test]
fn with_wrong_domain() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(wrapped_transferable_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::wrapped_transferable_metadata()));

assert_noop!(
LiquidityPools::transfer(
Expand All @@ -218,7 +233,7 @@ mod transfer {
#[test]
fn without_satisfy_filter() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(wrapped_transferable_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::wrapped_transferable_metadata()));
TransferFilter::mock_check(|_| Err(DispatchError::Other("Err")));

assert_noop!(
Expand All @@ -236,7 +251,7 @@ mod transfer {
#[test]
fn without_sufficient_balance() {
System::externalities().execute_with(|| {
AssetRegistry::mock_metadata(|_| Some(wrapped_transferable_metadata()));
AssetRegistry::mock_metadata(|_| Some(util::wrapped_transferable_metadata()));
TransferFilter::mock_check(|_| Ok(()));

assert_noop!(
Expand Down Expand Up @@ -274,7 +289,6 @@ mod transfer_tranche_tokens {
Pools::mock_tranche_exists(|_, _| true);
TransferFilter::mock_check(|_| Ok(()));
Tokens::mint_into(CurrencyId::Tranche(POOL_ID, TRANCHE_ID), &ALICE, AMOUNT).unwrap();

Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_ADDRESS.domain());
Expand Down Expand Up @@ -363,7 +377,7 @@ mod transfer_tranche_tokens {
}

#[test]
fn with_wrong_tranche_id() {
fn with_wrong_tranche() {
System::externalities().execute_with(|| {
DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID);
Time::mock_now(|| NOW);
Expand Down Expand Up @@ -422,7 +436,6 @@ mod add_pool {
true
});
Pools::mock_pool_exists(|_| true);

Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_ADDRESS.domain());
Expand All @@ -443,6 +456,22 @@ mod add_pool {

#[test]
fn with_wrong_pool() {
System::externalities().execute_with(|| {
Pools::mock_pool_exists(|_| false);

assert_noop!(
LiquidityPools::add_pool(
RuntimeOrigin::signed(ALICE),
POOL_ID,
EVM_ADDRESS.domain(),
),
Error::<Runtime>::PoolNotFound
);
})
}

#[test]
fn with_wrong_permissions() {
System::externalities().execute_with(|| {
Pools::mock_pool_exists(|_| true);
Permissions::mock_has(move |_, _, _| false);
Expand All @@ -457,22 +486,126 @@ mod add_pool {
);
})
}
}
}

mod add_tranche {
use super::*;

#[test]
fn success() {
System::externalities().execute_with(|| {
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
});
Pools::mock_pool_exists(|_| true);
Pools::mock_tranche_exists(|_, _| true);
AssetRegistry::mock_metadata(|_| Some(util::default_metadata()));
Gateway::mock_submit(|sender, destination, msg| {
assert_eq!(sender, ALICE);
assert_eq!(destination, EVM_ADDRESS.domain());
assert_eq!(
msg,
Message::AddTranche {
pool_id: POOL_ID,
tranche_id: TRANCHE_ID,
token_name: vec_to_fixed_array(NAME),
token_symbol: vec_to_fixed_array(SYMBOL),
decimals: DECIMALS,
restriction_set: 1
}
);
Ok(())
});

assert_ok!(LiquidityPools::add_tranche(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS.domain(),
));
})
}

mod erroring_out_when {
use super::*;

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

assert_noop!(
LiquidityPools::add_tranche(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS.domain(),
),
Error::<Runtime>::NotPoolAdmin
);
})
}

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

assert_noop!(
LiquidityPools::add_pool(
LiquidityPools::add_tranche(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS.domain(),
),
Error::<Runtime>::PoolNotFound
);
})
}

#[test]
fn with_wrong_tranche() {
System::externalities().execute_with(|| {
Permissions::mock_has(move |_, _, _| true);
Pools::mock_pool_exists(|_| true);
Pools::mock_tranche_exists(|_, _| false);

assert_noop!(
LiquidityPools::add_tranche(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS.domain(),
),
Error::<Runtime>::TrancheNotFound,
);
})
}

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

assert_noop!(
LiquidityPools::add_tranche(
RuntimeOrigin::signed(ALICE),
POOL_ID,
TRANCHE_ID,
EVM_ADDRESS.domain(),
),
Error::<Runtime>::TrancheMetadataNotFound,
);
})
}
}
}

Expand Down
Loading

0 comments on commit 91b79bb

Please sign in to comment.