From 91b79bb7ca940fec03ac0837deba6b0f0909f0cd Mon Sep 17 00:00:00 2001 From: lemunozm Date: Thu, 20 Jun 2024 12:06:00 +0200 Subject: [PATCH] add add_tranche tests --- pallets/liquidity-pools/src/lib.rs | 5 - pallets/liquidity-pools/src/tests.rs | 219 ++++++++++++++---- .../src/generic/cases/liquidity_pools.rs | 72 ------ 3 files changed, 176 insertions(+), 120 deletions(-) diff --git a/pallets/liquidity-pools/src/lib.rs b/pallets/liquidity-pools/src/lib.rs index 275666aafd..c03021eb60 100644 --- a/pallets/liquidity-pools/src/lib.rs +++ b/pallets/liquidity-pools/src/lib.rs @@ -370,11 +370,6 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin.clone())?; - ensure!( - T::PoolInspect::tranche_exists(pool_id, tranche_id), - Error::::TrancheNotFound - ); - ensure!( T::Permission::has( PermissionScope::Pool(pool_id), diff --git a/pallets/liquidity-pools/src/tests.rs b/pallets/liquidity-pools/src/tests.rs index 4e4a7f2e9e..f4a72e73cf 100644 --- a/pallets/liquidity-pools/src/tests.rs +++ b/pallets/liquidity-pools/src/tests.rs @@ -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 _}, @@ -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::(); - 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::::try_from(currency_id) - .unwrap() - .index + pub fn wrapped_transferable_metadata() -> AssetMetadata { + let pallet_index = PalletInfo::index::(); + 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::::try_from(currency_id) + .unwrap() + .index + } } mod transfer { @@ -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| { @@ -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 @@ -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( @@ -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( @@ -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( @@ -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( @@ -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!( @@ -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!( @@ -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()); @@ -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); @@ -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()); @@ -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::::PoolNotFound + ); + }) + } + + #[test] + fn with_wrong_permissions() { System::externalities().execute_with(|| { Pools::mock_pool_exists(|_| true); Permissions::mock_has(move |_, _, _| false); @@ -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::::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::::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::::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::::TrancheMetadataNotFound, + ); + }) + } } } diff --git a/runtime/integration-tests/src/generic/cases/liquidity_pools.rs b/runtime/integration-tests/src/generic/cases/liquidity_pools.rs index b92197b337..d306e411f9 100644 --- a/runtime/integration-tests/src/generic/cases/liquidity_pools.rs +++ b/runtime/integration-tests/src/generic/cases/liquidity_pools.rs @@ -699,78 +699,6 @@ mod add_allow_upgrade { use super::*; - #[test_runtimes([development])] - fn add_tranche() { - let mut env = FudgeEnv::::from_parachain_storage( - Genesis::default() - .add(genesis::balances::(cfg(1_000))) - .add(genesis::tokens::(vec![( - GLMR_CURRENCY_ID, - DEFAULT_BALANCE_GLMR, - )])) - .storage(), - ); - - setup_test(&mut env); - - env.parachain_state_mut(|| { - // Now create the pool - let pool_id = POOL_ID; - create_ausd_pool::(pool_id); - - // Verify we can't call pallet_liquidity_pools::Pallet::::add_tranche with a - // non-existing tranche_id - let nonexistent_tranche = [71u8; 16]; - - assert_noop!( - pallet_liquidity_pools::Pallet::::add_tranche( - RawOrigin::Signed(Keyring::Alice.into()).into(), - pool_id, - nonexistent_tranche, - Domain::EVM(MOONBEAM_EVM_CHAIN_ID), - ), - pallet_liquidity_pools::Error::::TrancheNotFound - ); - let tranche_id = default_tranche_id::(pool_id); - - // Verify ALICE can't call `add_tranche` given she is not the `PoolAdmin` - assert_noop!( - pallet_liquidity_pools::Pallet::::add_tranche( - RawOrigin::Signed(Keyring::Alice.into()).into(), - pool_id, - tranche_id, - Domain::EVM(MOONBEAM_EVM_CHAIN_ID), - ), - pallet_liquidity_pools::Error::::NotPoolAdmin - ); - - // Finally, verify we can call pallet_liquidity_pools::Pallet::::add_tranche - // successfully when called by the PoolAdmin with the right pool + tranche id - // pair. - assert_ok!(pallet_liquidity_pools::Pallet::::add_tranche( - RawOrigin::Signed(POOL_ADMIN.into()).into(), - pool_id, - tranche_id, - Domain::EVM(MOONBEAM_EVM_CHAIN_ID), - )); - - // Edge case: Should throw if tranche exists but metadata does not exist - let tranche_currency_id = CurrencyId::Tranche(pool_id, tranche_id); - - orml_asset_registry::Metadata::::remove(tranche_currency_id); - - assert_noop!( - pallet_liquidity_pools::Pallet::::update_tranche_token_metadata( - RawOrigin::Signed(POOL_ADMIN.into()).into(), - pool_id, - tranche_id, - Domain::EVM(MOONBEAM_EVM_CHAIN_ID), - ), - pallet_liquidity_pools::Error::::TrancheMetadataNotFound - ); - }); - } - #[test_runtimes([development])] fn update_member() { let mut env = FudgeEnv::::from_parachain_storage(