From b3c841286d1aa8e0b118d9ce0d7e6b8a8b4a9697 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 15 Jul 2024 13:43:51 +0200 Subject: [PATCH] schedule_upgrade cancel_upgrade and update_tranche_token_metadata --- pallets/liquidity-pools/src/lib.rs | 5 - pallets/liquidity-pools/src/tests.rs | 185 ++++++++++++++++++ .../src/cases/liquidity_pools.rs | 142 +------------- 3 files changed, 193 insertions(+), 139 deletions(-) diff --git a/pallets/liquidity-pools/src/lib.rs b/pallets/liquidity-pools/src/lib.rs index f9de3217c8..64181d46b9 100644 --- a/pallets/liquidity-pools/src/lib.rs +++ b/pallets/liquidity-pools/src/lib.rs @@ -769,11 +769,6 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin.clone())?; - ensure!( - T::PoolInspect::tranche_exists(pool_id, tranche_id), - Error::::TrancheNotFound - ); - let investment_id = Self::derive_invest_id(pool_id, tranche_id)?; let metadata = T::AssetRegistry::metadata(&investment_id.into()) .ok_or(Error::::TrancheMetadataNotFound)?; diff --git a/pallets/liquidity-pools/src/tests.rs b/pallets/liquidity-pools/src/tests.rs index 76fe5619e6..4f0da61ae5 100644 --- a/pallets/liquidity-pools/src/tests.rs +++ b/pallets/liquidity-pools/src/tests.rs @@ -633,6 +633,101 @@ mod add_tranche { } } +mod update_tranche_token_metadata { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + 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_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::UpdateTrancheTokenMetadata { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + token_name: vec_to_fixed_array(NAME), + token_symbol: vec_to_fixed_array(SYMBOL), + } + ); + Ok(()) + }); + + assert_ok!(LiquidityPools::update_tranche_token_metadata( + RuntimeOrigin::signed(ALICE), + POOL_ID, + TRANCHE_ID, + EVM_DOMAIN_ADDRESS.domain(), + )); + }) + } + + mod erroring_out { + use super::*; + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + Permissions::mock_has(move |_, _, _| true); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::update_tranche_token_metadata( + RuntimeOrigin::signed(ALICE), + POOL_ID, + TRANCHE_ID, + EVM_DOMAIN_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::update_tranche_token_metadata( + RuntimeOrigin::signed(ALICE), + POOL_ID, + TRANCHE_ID, + EVM_DOMAIN_ADDRESS.domain(), + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| None); + + assert_noop!( + LiquidityPools::update_tranche_token_metadata( + RuntimeOrigin::signed(ALICE), + POOL_ID, + TRANCHE_ID, + EVM_DOMAIN_ADDRESS.domain(), + ), + Error::::TrancheMetadataNotFound, + ); + }) + } + } +} + mod update_token_price { use super::*; @@ -1248,6 +1343,96 @@ mod disallow_investment_currency { } } +mod schedule_upgrade { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, TreasuryAccount::get()); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::ScheduleUpgrade { + contract: CONTRACT_ACCOUNT + } + ); + Ok(()) + }); + + assert_ok!(LiquidityPools::schedule_upgrade( + RuntimeOrigin::root(), + CHAIN_ID, + CONTRACT_ACCOUNT, + )); + }) + } + + mod erroring_out { + use super::*; + + #[test] + fn with_origin() { + System::externalities().execute_with(|| { + assert_noop!( + LiquidityPools::schedule_upgrade( + RuntimeOrigin::signed(ALICE), + CHAIN_ID, + CONTRACT_ACCOUNT, + ), + DispatchError::BadOrigin + ); + }) + } + } +} + +mod cancel_upgrade { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, TreasuryAccount::get()); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::CancelUpgrade { + contract: CONTRACT_ACCOUNT + } + ); + Ok(()) + }); + + assert_ok!(LiquidityPools::cancel_upgrade( + RuntimeOrigin::root(), + CHAIN_ID, + CONTRACT_ACCOUNT, + )); + }) + } + + mod erroring_out { + use super::*; + + #[test] + fn with_origin() { + System::externalities().execute_with(|| { + assert_noop!( + LiquidityPools::cancel_upgrade( + RuntimeOrigin::signed(ALICE), + CHAIN_ID, + CONTRACT_ACCOUNT, + ), + DispatchError::BadOrigin + ); + }) + } + } +} + #[test] fn receiving_invalid_message() { System::externalities().execute_with(|| { diff --git a/runtime/integration-tests/src/cases/liquidity_pools.rs b/runtime/integration-tests/src/cases/liquidity_pools.rs index 12158f4bda..5f0c7db3c6 100644 --- a/runtime/integration-tests/src/cases/liquidity_pools.rs +++ b/runtime/integration-tests/src/cases/liquidity_pools.rs @@ -35,7 +35,7 @@ use runtime_common::{ }; use sp_core::{Get, H160}; use sp_runtime::{ - traits::{AccountIdConversion, BadOrigin, Convert, EnsureAdd, One, Zero}, + traits::{AccountIdConversion, Convert, EnsureAdd, One, Zero}, BoundedVec, DispatchError, FixedPointNumber, Perquintill, SaturatedConversion, }; use staging_xcm::{ @@ -50,6 +50,13 @@ use crate::{ utils::{accounts::Keyring, genesis, genesis::Genesis, orml_asset_registry}, }; +// ------------------ +// NOTE +// This file only contains foreign investments tests, but the name must remain +// as it is until feature lpv2 is merged to avoid conflicts: +// (https://github.com/centrifuge/centrifuge-chain/pull/1909) +// ------------------ + /// The AUSD asset id pub const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(3); /// The USDT asset id @@ -662,139 +669,6 @@ mod utils { use utils::*; -mod add_allow_upgrade { - use super::*; - - #[test_runtimes([development])] - fn schedule_upgrade() { - 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(|| { - // Only Root can call `schedule_upgrade` - assert_noop!( - pallet_liquidity_pools::Pallet::::schedule_upgrade( - RawOrigin::Signed(Keyring::Bob.into()).into(), - MOONBEAM_EVM_CHAIN_ID, - [7; 20] - ), - BadOrigin - ); - - // Now it finally works - assert_ok!(pallet_liquidity_pools::Pallet::::schedule_upgrade( - ::RuntimeOrigin::root(), - MOONBEAM_EVM_CHAIN_ID, - [7; 20] - )); - }); - } - - #[test_runtimes([development])] - fn cancel_upgrade() { - 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(|| { - // Only Root can call `cancel_upgrade` - assert_noop!( - pallet_liquidity_pools::Pallet::::cancel_upgrade( - RawOrigin::Signed(Keyring::Bob.into()).into(), - MOONBEAM_EVM_CHAIN_ID, - [7; 20] - ), - BadOrigin - ); - - // Now it finally works - assert_ok!(pallet_liquidity_pools::Pallet::::cancel_upgrade( - ::RuntimeOrigin::root(), - MOONBEAM_EVM_CHAIN_ID, - [7; 20] - )); - }); - } - - #[test_runtimes([development])] - fn update_tranche_token_metadata() { - 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(|| { - let pool_id = POOL_ID; - // NOTE: Default pool admin is BOB - create_ausd_pool::(pool_id); - - // Missing tranche token should throw - let nonexistent_tranche = [71u8; 16]; - - assert_noop!( - pallet_liquidity_pools::Pallet::::update_tranche_token_metadata( - 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); - - // Moving the update to another domain can be called by anyone - assert_ok!( - pallet_liquidity_pools::Pallet::::update_tranche_token_metadata( - RawOrigin::Signed(Keyring::Alice.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 - ); - }); - } -} - mod foreign_investments { use super::*;