From 37a46b5ecef027936c4ed102851069e954b73930 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 23 Jul 2024 12:43:54 +0200 Subject: [PATCH 1/3] tests inbound messages --- pallets/liquidity-pools/src/mock.rs | 96 ++- pallets/liquidity-pools/src/tests.rs | 92 +-- pallets/liquidity-pools/src/tests/inbound.rs | 791 +++++++++++++++++++ 3 files changed, 888 insertions(+), 91 deletions(-) create mode 100644 pallets/liquidity-pools/src/tests/inbound.rs diff --git a/pallets/liquidity-pools/src/mock.rs b/pallets/liquidity-pools/src/mock.rs index 3d120e2e53..386bb58372 100644 --- a/pallets/liquidity-pools/src/mock.rs +++ b/pallets/liquidity-pools/src/mock.rs @@ -1,20 +1,51 @@ use cfg_primitives::{PoolId, TrancheId}; -use cfg_traits::Millis; +use cfg_traits::{Millis, Seconds}; use cfg_types::{ domain_address::{Domain, DomainAddress}, permissions::PermissionScope, - tokens::{AssetStringLimit, CurrencyId, CustomMetadata, TrancheCurrency}, + tokens::{ + AssetMetadata, AssetStringLimit, CrossChainTransferability, CurrencyId, CustomMetadata, + LocalAssetId, TrancheCurrency, + }, }; -use frame_support::derive_impl; +use frame_support::{derive_impl, traits::PalletInfo as _}; use orml_traits::parameter_type_with_key; use sp_runtime::{traits::IdentityLookup, AccountId32, DispatchResult, FixedU128}; +use staging_xcm::{ + v4::{Junction::*, Location, NetworkId}, + VersionedLocation, +}; -use crate::pallet as pallet_liquidity_pools; +use crate::{pallet as pallet_liquidity_pools, GeneralCurrencyIndexOf}; pub type Balance = u128; pub type AccountId = AccountId32; pub type Ratio = FixedU128; +pub const CHAIN_ID: u64 = 1; +pub const ALICE: AccountId = AccountId::new([0; 32]); +pub const CONTRACT_ACCOUNT: [u8; 20] = [1; 20]; +pub const CONTRACT_ACCOUNT_ID: AccountId = AccountId::new([1; 32]); +pub const EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, CONTRACT_ACCOUNT); +pub const CENTRIFUGE_DOMAIN_ADDRESS: DomainAddress = DomainAddress::Centrifuge([0; 32]); +pub const AMOUNT: Balance = 100; +pub const CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1); +pub const POOL_CURRENCY_ID: CurrencyId = CurrencyId::LocalAsset(LocalAssetId(1)); +pub const POOL_ID: PoolId = 1; +pub const TRANCHE_ID: TrancheId = [1; 16]; +pub const NOW: Millis = 10000; +pub const NOW_SECS: Seconds = 10; +pub const NAME: &[u8] = b"Token name"; +pub const SYMBOL: &[u8] = b"Token symbol"; +pub const DECIMALS: u8 = 6; +pub const TRANCHE_CURRENCY: CurrencyId = CurrencyId::Tranche(POOL_ID, TRANCHE_ID); +pub const TRANCHE_TOKEN_PRICE: Ratio = Ratio::from_rational(10, 1); +pub const MARKET_RATIO: Ratio = Ratio::from_rational(2, 1); +pub const INVESTMENT_ID: TrancheCurrency = TrancheCurrency { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, +}; + frame_support::construct_runtime!( pub enum Runtime { System: frame_system, @@ -149,3 +180,60 @@ impl pallet_liquidity_pools::Config for Runtime { type TreasuryAccount = TreasuryAccount; type WeightInfo = (); } + +pub 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() + } + } + + pub fn transferable_metadata() -> AssetMetadata { + AssetMetadata { + additional: CustomMetadata { + transferability: CrossChainTransferability::LiquidityPools, + ..Default::default() + }, + ..default_metadata() + } + } + + pub fn locatable_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 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::::try_from(currency_id) + .unwrap() + .index + } +} diff --git a/pallets/liquidity-pools/src/tests.rs b/pallets/liquidity-pools/src/tests.rs index 4f0da61ae5..61c9aa3b40 100644 --- a/pallets/liquidity-pools/src/tests.rs +++ b/pallets/liquidity-pools/src/tests.rs @@ -1,101 +1,19 @@ -use cfg_primitives::{PoolId, TrancheId}; -use cfg_traits::{liquidity_pools::InboundQueue, Millis, Seconds}; +use cfg_traits::{liquidity_pools::InboundQueue, Seconds}; use cfg_types::{ domain_address::DomainAddress, permissions::{PermissionScope, PoolRole, Role}, - tokens::{AssetMetadata, CrossChainTransferability, CurrencyId, CustomMetadata, LocalAssetId}, + tokens::CurrencyId, }; use cfg_utils::vec_to_fixed_array; use frame_support::{ assert_noop, assert_ok, - traits::{ - fungibles::{Inspect as _, Mutate as _}, - PalletInfo as _, - }, + traits::fungibles::{Inspect as _, Mutate as _}, }; use sp_runtime::{traits::Saturating, DispatchError, TokenError}; -use staging_xcm::{ - v4::{Junction::*, Location, NetworkId}, - VersionedLocation, -}; - -use crate::{mock::*, Error, GeneralCurrencyIndexOf, Message}; - -const CHAIN_ID: u64 = 1; -const ALICE: AccountId = AccountId::new([0; 32]); -const CONTRACT_ACCOUNT: [u8; 20] = [1; 20]; -const CONTRACT_ACCOUNT_ID: AccountId = AccountId::new([1; 32]); -const EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, CONTRACT_ACCOUNT); -const AMOUNT: Balance = 100; -const CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1); -const POOL_CURRENCY_ID: CurrencyId = CurrencyId::LocalAsset(LocalAssetId(1)); -const POOL_ID: PoolId = 1; -const TRANCHE_ID: TrancheId = [1; 16]; -const NOW: Millis = 10000; -const NOW_SECS: Seconds = 10; -const NAME: &[u8] = b"Token name"; -const SYMBOL: &[u8] = b"Token symbol"; -const DECIMALS: u8 = 6; -const TRANCHE_CURRENCY: CurrencyId = CurrencyId::Tranche(POOL_ID, TRANCHE_ID); -const TRANCHE_TOKEN_PRICE: Ratio = Ratio::from_rational(10, 1); -const MARKET_RATIO: Ratio = Ratio::from_rational(2, 1); - -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() - } - } - - pub fn transferable_metadata() -> AssetMetadata { - AssetMetadata { - additional: CustomMetadata { - transferability: CrossChainTransferability::LiquidityPools, - ..Default::default() - }, - ..default_metadata() - } - } - - pub fn locatable_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 pool_locatable_transferable_metadata() -> AssetMetadata { - AssetMetadata { - additional: CustomMetadata { - pool_currency: true, - ..transferable_metadata().additional - }, - ..locatable_transferable_metadata() - } - } +use crate::{mock::*, Error, Message}; - pub fn currency_index(currency_id: CurrencyId) -> u128 { - GeneralCurrencyIndexOf::::try_from(currency_id) - .unwrap() - .index - } -} +mod inbound; mod transfer { use super::*; diff --git a/pallets/liquidity-pools/src/tests/inbound.rs b/pallets/liquidity-pools/src/tests/inbound.rs new file mode 100644 index 0000000000..e886ff07ee --- /dev/null +++ b/pallets/liquidity-pools/src/tests/inbound.rs @@ -0,0 +1,791 @@ +use cfg_traits::liquidity_pools::InboundQueue; +use cfg_types::permissions::{PermissionScope, PoolRole, Role}; +use frame_support::{assert_noop, assert_ok, traits::fungibles::Mutate as _}; + +use crate::{mock::*, Error, Message}; + +mod handle_transfer { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::Transfer { + currency: util::currency_index(CURRENCY_ID), + sender: ALICE.into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + }, + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_zero_balance() { + System::externalities().execute_with(|| { + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::Transfer { + currency: util::currency_index(CURRENCY_ID), + sender: ALICE.into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: 0, + }, + ), + Error::::InvalidTransferAmount + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + AssetRegistry::mock_metadata(|_| None); + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::Transfer { + currency: util::currency_index(CURRENCY_ID), + sender: ALICE.into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + }, + ), + Error::::AssetNotFound, + ); + }) + } + } +} + +mod handle_tranche_tokens_transfer { + use super::*; + + #[test] + fn success_with_centrifuge_domain() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Time::mock_now(|| NOW); + Permissions::mock_has(move |scope, who, role| { + assert_eq!(who, ALICE); + assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); + assert!(matches!( + role, + Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) + )); + true + }); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + Tokens::mint_into( + TRANCHE_CURRENCY, + &EVM_DOMAIN_ADDRESS.domain().into_account(), + AMOUNT * 2, + ) + .unwrap(); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: CENTRIFUGE_DOMAIN_ADDRESS.address(), + amount: AMOUNT + } + )); + }); + } + + #[test] + fn success_with_evm_domain() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| EVM_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); + Time::mock_now(|| NOW); + Permissions::mock_has(move |scope, who, role| { + assert_eq!(who, CONTRACT_ACCOUNT_ID); + assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); + assert!(matches!( + role, + Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) + )); + true + }); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + Tokens::mint_into( + TRANCHE_CURRENCY, + &EVM_DOMAIN_ADDRESS.domain().into_account(), + AMOUNT, + ) + .unwrap(); + + TransferFilter::mock_check(|_| Ok(())); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, CONTRACT_ACCOUNT_ID); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: CONTRACT_ACCOUNT_ID.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT + } + ); + Ok(()) + }); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT + } + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_zero_balance() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: 0, + } + ), + Error::::InvalidTransferAmount, + ); + }) + } + + #[test] + fn with_wrong_permissions() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Time::mock_now(|| NOW); + Permissions::mock_has(|_, _, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + } + ), + Error::::UnauthorizedTransfer, + ); + }) + } + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Time::mock_now(|| NOW); + Permissions::mock_has(|_, _, _| true); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + } + ), + Error::::PoolNotFound, + ); + }) + } + + #[test] + fn with_wrong_tranche() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Time::mock_now(|| NOW); + Permissions::mock_has(|_, _, _| true); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + } + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn without_sufficient_balance() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Time::mock_now(|| NOW); + Permissions::mock_has(|_, _, _| true); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT, + } + ), + orml_tokens::Error::::BalanceTooLow + ); + }) + } + } +} + +mod handle_increase_invest_order { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_increase_foreign_investment( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::PoolNotFound, + ); + }) + } + + #[test] + fn with_wrong_tranche() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| None); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::AssetNotFound, + ); + }) + } + } +} + +mod handle_cancel_invest_order { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_decrease_foreign_investment( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::PoolNotFound, + ); + }) + } + + #[test] + fn with_wrong_tranche() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| None); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelInvestOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::AssetNotFound, + ); + }) + } + } +} + +mod handle_increase_redeem_order { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + Tokens::mint_into( + TRANCHE_CURRENCY, + &EVM_DOMAIN_ADDRESS.domain().into_account(), + AMOUNT, + ) + .unwrap(); + ForeignInvestment::mock_increase_foreign_redemption( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::PoolNotFound, + ); + }) + } + + #[test] + fn with_wrong_tranche() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| None); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + Error::::AssetNotFound, + ); + }) + } + + #[test] + fn without_sufficient_balance() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::IncreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + amount: AMOUNT, + }, + ), + orml_tokens::Error::::BalanceTooLow + ); + }) + } + } +} + +mod handle_cancel_redeem_order { + use super::*; + + #[test] + fn success() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_decrease_foreign_redemption( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + + // Side effects of this call + ForeignInvestment::mock_redemption(|_, _| Ok(0)); + Tokens::mint_into(TRANCHE_CURRENCY, &ALICE, AMOUNT).unwrap(); + Ok(()) + }, + ); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, TreasuryAccount::get()); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::ExecutedDecreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + tranche_tokens_payout: AMOUNT, + remaining_redeem_amount: 0, + } + ); + Ok(()) + }); + + assert_ok!(LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + )); + }); + } + + mod erroring_out { + use super::*; + + #[test] + fn with_wrong_pool() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::PoolNotFound, + ); + }) + } + + #[test] + fn with_wrong_tranche() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| false); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::TrancheNotFound, + ); + }) + } + + #[test] + fn with_no_metadata() { + System::externalities().execute_with(|| { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| None); + + assert_noop!( + LiquidityPools::submit( + EVM_DOMAIN_ADDRESS, + Message::CancelRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + }, + ), + Error::::AssetNotFound, + ); + }) + } + } +} From 15ea494b5785d945447099f723b206d9b8603587 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Wed, 24 Jul 2024 08:37:11 +0200 Subject: [PATCH 2/3] some cleanings --- pallets/liquidity-pools/src/mock.rs | 4 +- pallets/liquidity-pools/src/tests.rs | 342 +++++++-------- pallets/liquidity-pools/src/tests/inbound.rs | 424 +++++++++---------- 3 files changed, 379 insertions(+), 391 deletions(-) diff --git a/pallets/liquidity-pools/src/mock.rs b/pallets/liquidity-pools/src/mock.rs index 386bb58372..f3e6659b39 100644 --- a/pallets/liquidity-pools/src/mock.rs +++ b/pallets/liquidity-pools/src/mock.rs @@ -24,10 +24,12 @@ pub type Ratio = FixedU128; pub const CHAIN_ID: u64 = 1; pub const ALICE: AccountId = AccountId::new([0; 32]); +pub const ALICE_ETH: [u8; 20] = [0; 20]; +pub const ALICE_EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, ALICE_ETH); +pub const CENTRIFUGE_DOMAIN_ADDRESS: DomainAddress = DomainAddress::Centrifuge([0; 32]); // Alice pub const CONTRACT_ACCOUNT: [u8; 20] = [1; 20]; pub const CONTRACT_ACCOUNT_ID: AccountId = AccountId::new([1; 32]); pub const EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, CONTRACT_ACCOUNT); -pub const CENTRIFUGE_DOMAIN_ADDRESS: DomainAddress = DomainAddress::Centrifuge([0; 32]); pub const AMOUNT: Balance = 100; pub const CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1); pub const POOL_CURRENCY_ID: CurrencyId = CurrencyId::LocalAsset(LocalAssetId(1)); diff --git a/pallets/liquidity-pools/src/tests.rs b/pallets/liquidity-pools/src/tests.rs index 61c9aa3b40..f0f3a582cb 100644 --- a/pallets/liquidity-pools/src/tests.rs +++ b/pallets/liquidity-pools/src/tests.rs @@ -1,4 +1,4 @@ -use cfg_traits::{liquidity_pools::InboundQueue, Seconds}; +use cfg_traits::Seconds; use cfg_types::{ domain_address::DomainAddress, permissions::{PermissionScope, PoolRole, Role}, @@ -23,7 +23,6 @@ mod transfer { System::externalities().execute_with(|| { AssetRegistry::mock_metadata(|_| Some(util::locatable_transferable_metadata())); TransferFilter::mock_check(|_| Ok(())); - Tokens::mint_into(CURRENCY_ID, &ALICE, AMOUNT).unwrap(); Gateway::mock_submit(|sender, destination, msg| { assert_eq!(sender, ALICE); assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); @@ -39,6 +38,8 @@ mod transfer { Ok(()) }); + Tokens::mint_into(CURRENCY_ID, &ALICE, AMOUNT).unwrap(); + assert_ok!(LiquidityPools::transfer( RuntimeOrigin::signed(ALICE), CurrencyId::ForeignAsset(1), @@ -209,40 +210,45 @@ mod transfer { mod transfer_tranche_tokens { use super::*; + fn config_mocks() { + DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); + Time::mock_now(|| NOW); + Permissions::mock_has(move |scope, who, role| { + assert_eq!(who, CONTRACT_ACCOUNT_ID); + assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); + assert!(matches!( + role, + Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) + )); + true + }); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + TransferFilter::mock_check(|_| Ok(())); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, ALICE); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::TransferTrancheTokens { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + sender: ALICE.into(), + domain: EVM_DOMAIN_ADDRESS.domain().into(), + receiver: EVM_DOMAIN_ADDRESS.address(), + amount: AMOUNT + } + ); + Ok(()) + }); + } + #[test] fn success() { System::externalities().execute_with(|| { - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); - Permissions::mock_has(move |scope, who, role| { - assert_eq!(who, CONTRACT_ACCOUNT_ID); - assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); - assert!(matches!( - role, - Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) - )); - true - }); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - TransferFilter::mock_check(|_| Ok(())); + config_mocks(); + Tokens::mint_into(TRANCHE_CURRENCY, &ALICE, AMOUNT).unwrap(); - Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, ALICE); - assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); - assert_eq!( - msg, - Message::TransferTrancheTokens { - pool_id: POOL_ID, - tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), - amount: AMOUNT - } - ); - Ok(()) - }); assert_ok!(LiquidityPools::transfer_tranche_tokens( RuntimeOrigin::signed(ALICE), @@ -280,8 +286,7 @@ mod transfer_tranche_tokens { #[test] fn with_wrong_permissions() { System::externalities().execute_with(|| { - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); + config_mocks(); Permissions::mock_has(|_, _, _| false); assert_noop!( @@ -300,9 +305,7 @@ mod transfer_tranche_tokens { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); - Permissions::mock_has(move |_, _, _| true); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -321,10 +324,7 @@ mod transfer_tranche_tokens { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); - Permissions::mock_has(move |_, _, _| true); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -343,11 +343,7 @@ mod transfer_tranche_tokens { #[test] fn without_satisfy_filter() { System::externalities().execute_with(|| { - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); - Permissions::mock_has(move |_, _, _| true); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); TransferFilter::mock_check(|_| Err(DispatchError::Other("Err"))); assert_noop!( @@ -434,34 +430,38 @@ mod add_pool { mod add_tranche { use super::*; + fn config_mocks() { + 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_DOMAIN_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(()) + }); + } + #[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_DOMAIN_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(()) - }); + config_mocks(); assert_ok!(LiquidityPools::add_tranche( RuntimeOrigin::signed(ALICE), @@ -495,7 +495,7 @@ mod add_tranche { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - Permissions::mock_has(move |_, _, _| true); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -513,8 +513,7 @@ mod add_tranche { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - Permissions::mock_has(move |_, _, _| true); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -532,9 +531,7 @@ mod add_tranche { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - Permissions::mock_has(move |_, _, _| true); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( @@ -554,27 +551,30 @@ mod add_tranche { mod update_tranche_token_metadata { use super::*; + fn config_mocks() { + 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(()) + }); + } + #[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(()) - }); + config_mocks(); assert_ok!(LiquidityPools::update_tranche_token_metadata( RuntimeOrigin::signed(ALICE), @@ -591,7 +591,7 @@ mod update_tranche_token_metadata { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - Permissions::mock_has(move |_, _, _| true); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -609,8 +609,7 @@ mod update_tranche_token_metadata { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - Permissions::mock_has(move |_, _, _| true); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -628,8 +627,7 @@ mod update_tranche_token_metadata { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( @@ -649,34 +647,38 @@ mod update_tranche_token_metadata { mod update_token_price { use super::*; + fn config_mocks() { + Pools::mock_get_price(|_, _| Some((TRANCHE_TOKEN_PRICE, 1234))); + Pools::mock_currency_for(|_| Some(POOL_CURRENCY_ID)); + MarketRatio::mock_market_ratio(|target, origin| { + assert_eq!(target, CURRENCY_ID); + assert_eq!(origin, POOL_CURRENCY_ID); + Ok(MARKET_RATIO) + }); + AssetRegistry::mock_metadata(|_| Some(util::locatable_transferable_metadata())); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, ALICE); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::UpdateTrancheTokenPrice { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + currency: util::currency_index(CURRENCY_ID), + price: TRANCHE_TOKEN_PRICE + .saturating_mul(MARKET_RATIO) + .into_inner(), + computed_at: 1234 + } + ); + Ok(()) + }); + } + #[test] fn success() { System::externalities().execute_with(|| { - Pools::mock_get_price(|_, _| Some((TRANCHE_TOKEN_PRICE, 1234))); - Pools::mock_currency_for(|_| Some(POOL_CURRENCY_ID)); - MarketRatio::mock_market_ratio(|target, origin| { - assert_eq!(target, CURRENCY_ID); - assert_eq!(origin, POOL_CURRENCY_ID); - Ok(MARKET_RATIO) - }); - AssetRegistry::mock_metadata(|_| Some(util::locatable_transferable_metadata())); - Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, ALICE); - assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); - assert_eq!( - msg, - Message::UpdateTrancheTokenPrice { - pool_id: POOL_ID, - tranche_id: TRANCHE_ID, - currency: util::currency_index(CURRENCY_ID), - price: TRANCHE_TOKEN_PRICE - .saturating_mul(MARKET_RATIO) - .into_inner(), - computed_at: 1234 - } - ); - Ok(()) - }); + config_mocks(); assert_ok!(LiquidityPools::update_token_price( RuntimeOrigin::signed(ALICE), @@ -712,7 +714,7 @@ mod update_token_price { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - Pools::mock_get_price(|_, _| Some((TRANCHE_TOKEN_PRICE, 1234))); + config_mocks(); Pools::mock_currency_for(|_| None); assert_noop!( @@ -731,8 +733,7 @@ mod update_token_price { #[test] fn with_no_market_ratio() { System::externalities().execute_with(|| { - Pools::mock_get_price(|_, _| Some((TRANCHE_TOKEN_PRICE, 1234))); - Pools::mock_currency_for(|_| Some(POOL_CURRENCY_ID)); + config_mocks(); MarketRatio::mock_market_ratio(|_, _| Err(DispatchError::Other(""))); assert_noop!( @@ -751,9 +752,7 @@ mod update_token_price { #[test] fn with_no_transferible_asset() { System::externalities().execute_with(|| { - Pools::mock_get_price(|_, _| Some((TRANCHE_TOKEN_PRICE, 1234))); - Pools::mock_currency_for(|_| Some(POOL_CURRENCY_ID)); - MarketRatio::mock_market_ratio(|_, _| Ok(MARKET_RATIO)); + config_mocks(); AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); assert_noop!( @@ -776,36 +775,40 @@ mod update_member { const VALID_UNTIL_SECS: Seconds = NOW_SECS + 1; + fn config_mocks() { + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + Time::mock_now(|| NOW); + DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); + Permissions::mock_has(move |scope, who, role| { + assert_eq!(who, CONTRACT_ACCOUNT_ID); + assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); + assert!(matches!( + role, + Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, VALID_UNTIL_SECS)) + )); + true + }); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, ALICE); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::UpdateMember { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + valid_until: VALID_UNTIL_SECS, + member: EVM_DOMAIN_ADDRESS.address(), + } + ); + Ok(()) + }); + } + #[test] fn success() { System::externalities().execute_with(|| { - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - Time::mock_now(|| NOW); - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Permissions::mock_has(move |scope, who, role| { - assert_eq!(who, CONTRACT_ACCOUNT_ID); - assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); - assert!(matches!( - role, - Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, VALID_UNTIL_SECS)) - )); - true - }); - Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, ALICE); - assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); - assert_eq!( - msg, - Message::UpdateMember { - pool_id: POOL_ID, - tranche_id: TRANCHE_ID, - valid_until: VALID_UNTIL_SECS, - member: EVM_DOMAIN_ADDRESS.address(), - } - ); - Ok(()) - }); + config_mocks(); assert_ok!(LiquidityPools::update_member( RuntimeOrigin::signed(ALICE), @@ -841,7 +844,7 @@ mod update_member { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -860,8 +863,7 @@ mod update_member { #[test] fn with_wrong_time() { System::externalities().execute_with(|| { - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); Time::mock_now(|| VALID_UNTIL_SECS * 1000); assert_noop!( @@ -880,10 +882,7 @@ mod update_member { #[test] fn with_wrong_permissions() { System::externalities().execute_with(|| { - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - Time::mock_now(|| NOW); - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); + config_mocks(); Permissions::mock_has(|_, _, _| false); assert_noop!( @@ -1350,16 +1349,3 @@ mod cancel_upgrade { } } } - -#[test] -fn receiving_invalid_message() { - System::externalities().execute_with(|| { - // Add pool is an outbound message, not valid to be received - let msg = Message::AddPool { pool_id: 123 }; - - assert_noop!( - LiquidityPools::submit(EVM_DOMAIN_ADDRESS, msg), - Error::::InvalidIncomingMessage, - ); - }) -} diff --git a/pallets/liquidity-pools/src/tests/inbound.rs b/pallets/liquidity-pools/src/tests/inbound.rs index e886ff07ee..1b7fe40869 100644 --- a/pallets/liquidity-pools/src/tests/inbound.rs +++ b/pallets/liquidity-pools/src/tests/inbound.rs @@ -1,9 +1,28 @@ use cfg_traits::liquidity_pools::InboundQueue; -use cfg_types::permissions::{PermissionScope, PoolRole, Role}; -use frame_support::{assert_noop, assert_ok, traits::fungibles::Mutate as _}; +use cfg_types::{ + domain_address::DomainAddress, + permissions::{PermissionScope, PoolRole, Role}, +}; +use frame_support::{ + assert_noop, assert_ok, + traits::fungibles::{Inspect as _, Mutate as _}, +}; use crate::{mock::*, Error, Message}; +#[test] +fn receiving_invalid_message() { + System::externalities().execute_with(|| { + // Add pool is an outbound message, not valid to be received + let msg = Message::AddPool { pool_id: 123 }; + + assert_noop!( + LiquidityPools::submit(EVM_DOMAIN_ADDRESS, msg), + Error::::InvalidIncomingMessage, + ); + }) +} + mod handle_transfer { use super::*; @@ -16,11 +35,13 @@ mod handle_transfer { EVM_DOMAIN_ADDRESS, Message::Transfer { currency: util::currency_index(CURRENCY_ID), - sender: ALICE.into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + receiver: ALICE.into(), amount: AMOUNT, }, )); + + assert_eq!(Tokens::balance(CURRENCY_ID, &ALICE.into()), AMOUNT); }); } @@ -35,8 +56,8 @@ mod handle_transfer { EVM_DOMAIN_ADDRESS, Message::Transfer { currency: util::currency_index(CURRENCY_ID), - sender: ALICE.into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + receiver: ALICE.into(), amount: 0, }, ), @@ -54,8 +75,8 @@ mod handle_transfer { EVM_DOMAIN_ADDRESS, Message::Transfer { currency: util::currency_index(CURRENCY_ID), - sender: ALICE.into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + receiver: ALICE.into(), amount: AMOUNT, }, ), @@ -69,27 +90,35 @@ mod handle_transfer { mod handle_tranche_tokens_transfer { use super::*; + pub const LOCAL_ALICE: AccountId = AccountId::new([5; 32]); + + fn config_mocks(receiver: DomainAddress, local_representation: AccountId) { + let local_representation_clone = local_representation.clone(); + DomainAccountToDomainAddress::mock_convert(move |_| receiver.clone()); + DomainAddressToAccountId::mock_convert(move |_| local_representation_clone.clone()); + Time::mock_now(|| NOW); + Permissions::mock_has(move |scope, who, role| { + assert_eq!(who, local_representation.clone()); + assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); + assert!(matches!( + role, + Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) + )); + true + }); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + } + #[test] fn success_with_centrifuge_domain() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Time::mock_now(|| NOW); - Permissions::mock_has(move |scope, who, role| { - assert_eq!(who, ALICE); - assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); - assert!(matches!( - role, - Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) - )); - true - }); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + Tokens::mint_into( TRANCHE_CURRENCY, &EVM_DOMAIN_ADDRESS.domain().into_account(), - AMOUNT * 2, + AMOUNT, ) .unwrap(); @@ -98,68 +127,70 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), + sender: EVM_DOMAIN_ADDRESS.address(), domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), - receiver: CENTRIFUGE_DOMAIN_ADDRESS.address(), + receiver: ALICE.into(), amount: AMOUNT } )); + + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &ALICE.into()), AMOUNT); }); } #[test] fn success_with_evm_domain() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| EVM_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| CONTRACT_ACCOUNT_ID); - Time::mock_now(|| NOW); - Permissions::mock_has(move |scope, who, role| { - assert_eq!(who, CONTRACT_ACCOUNT_ID); - assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); - assert!(matches!( - role, - Role::PoolRole(PoolRole::TrancheInvestor(TRANCHE_ID, NOW_SECS)) - )); - true - }); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - Tokens::mint_into( - TRANCHE_CURRENCY, - &EVM_DOMAIN_ADDRESS.domain().into_account(), - AMOUNT, - ) - .unwrap(); + config_mocks(ALICE_EVM_DOMAIN_ADDRESS, LOCAL_ALICE); TransferFilter::mock_check(|_| Ok(())); Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, CONTRACT_ACCOUNT_ID); - assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!(sender, LOCAL_ALICE); + assert_eq!(destination, ALICE_EVM_DOMAIN_ADDRESS.domain()); assert_eq!( msg, Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: CONTRACT_ACCOUNT_ID.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: LOCAL_ALICE.into(), + domain: ALICE_EVM_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT } ); Ok(()) }); + Tokens::mint_into( + TRANCHE_CURRENCY, + &EVM_DOMAIN_ADDRESS.domain().into_account(), + AMOUNT, + ) + .unwrap(); + assert_ok!(LiquidityPools::submit( EVM_DOMAIN_ADDRESS, Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: ALICE_EVM_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT } )); + + assert_eq!( + Tokens::balance(TRANCHE_CURRENCY, &EVM_DOMAIN_ADDRESS.address().into()), + 0 + ); + assert_eq!( + Tokens::balance( + TRANCHE_CURRENCY, + &ALICE_EVM_DOMAIN_ADDRESS.domain().into_account() + ), + AMOUNT + ); }); } @@ -169,7 +200,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_zero_balance() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); assert_noop!( LiquidityPools::submit( @@ -177,9 +208,9 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: 0, } ), @@ -191,9 +222,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_permissions() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Time::mock_now(|| NOW); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); Permissions::mock_has(|_, _, _| false); assert_noop!( @@ -202,9 +231,9 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT, } ), @@ -216,10 +245,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Time::mock_now(|| NOW); - Permissions::mock_has(|_, _, _| true); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -228,9 +254,9 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT, } ), @@ -242,11 +268,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Time::mock_now(|| NOW); - Permissions::mock_has(|_, _, _| true); - Pools::mock_pool_exists(|_| true); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -255,9 +277,9 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT, } ), @@ -269,12 +291,7 @@ mod handle_tranche_tokens_transfer { #[test] fn without_sufficient_balance() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Time::mock_now(|| NOW); - Permissions::mock_has(|_, _, _| true); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); assert_noop!( LiquidityPools::submit( @@ -282,9 +299,9 @@ mod handle_tranche_tokens_transfer { Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: ALICE.into(), - domain: EVM_DOMAIN_ADDRESS.domain().into(), - receiver: EVM_DOMAIN_ADDRESS.address(), + sender: EVM_DOMAIN_ADDRESS.address(), + domain: CENTRIFUGE_DOMAIN_ADDRESS.domain().into(), + receiver: ALICE.into(), amount: AMOUNT, } ), @@ -298,23 +315,27 @@ mod handle_tranche_tokens_transfer { mod handle_increase_invest_order { use super::*; + fn config_mocks() { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_increase_foreign_investment( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + } + #[test] fn success() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); - ForeignInvestment::mock_increase_foreign_investment( - |who, investment_id, amount, foreign_currency| { - assert_eq!(*who, ALICE); - assert_eq!(investment_id, INVESTMENT_ID); - assert_eq!(amount, AMOUNT); - assert_eq!(foreign_currency, CURRENCY_ID); - Ok(()) - }, - ); + config_mocks(); assert_ok!(LiquidityPools::submit( EVM_DOMAIN_ADDRESS, @@ -335,8 +356,7 @@ mod handle_increase_invest_order { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -358,9 +378,7 @@ mod handle_increase_invest_order { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -382,10 +400,7 @@ mod handle_increase_invest_order { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( @@ -409,24 +424,28 @@ mod handle_increase_invest_order { mod handle_cancel_invest_order { use super::*; + fn config_mocks() { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_decrease_foreign_investment( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + } + #[test] fn success() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); - ForeignInvestment::mock_decrease_foreign_investment( - |who, investment_id, amount, foreign_currency| { - assert_eq!(*who, ALICE); - assert_eq!(investment_id, INVESTMENT_ID); - assert_eq!(amount, AMOUNT); - assert_eq!(foreign_currency, CURRENCY_ID); - Ok(()) - }, - ); + config_mocks(); assert_ok!(LiquidityPools::submit( EVM_DOMAIN_ADDRESS, @@ -446,9 +465,7 @@ mod handle_cancel_invest_order { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -469,10 +486,7 @@ mod handle_cancel_invest_order { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -493,11 +507,7 @@ mod handle_cancel_invest_order { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_investment(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( @@ -520,29 +530,34 @@ mod handle_cancel_invest_order { mod handle_increase_redeem_order { use super::*; + fn config_mocks() { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_increase_foreign_redemption( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + Ok(()) + }, + ); + } + #[test] fn success() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + config_mocks(); + Tokens::mint_into( TRANCHE_CURRENCY, &EVM_DOMAIN_ADDRESS.domain().into_account(), AMOUNT, ) .unwrap(); - ForeignInvestment::mock_increase_foreign_redemption( - |who, investment_id, amount, foreign_currency| { - assert_eq!(*who, ALICE); - assert_eq!(investment_id, INVESTMENT_ID); - assert_eq!(amount, AMOUNT); - assert_eq!(foreign_currency, CURRENCY_ID); - Ok(()) - }, - ); assert_ok!(LiquidityPools::submit( EVM_DOMAIN_ADDRESS, @@ -563,8 +578,7 @@ mod handle_increase_redeem_order { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -586,9 +600,7 @@ mod handle_increase_redeem_order { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -610,10 +622,7 @@ mod handle_increase_redeem_order { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( @@ -635,11 +644,7 @@ mod handle_increase_redeem_order { #[test] fn without_sufficient_balance() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + config_mocks(); assert_noop!( LiquidityPools::submit( @@ -662,44 +667,48 @@ mod handle_increase_redeem_order { mod handle_cancel_redeem_order { use super::*; + fn config_mocks() { + DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); + DomainAddressToAccountId::mock_convert(|_| ALICE); + ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + Pools::mock_pool_exists(|_| true); + Pools::mock_tranche_exists(|_, _| true); + AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); + ForeignInvestment::mock_decrease_foreign_redemption( + |who, investment_id, amount, foreign_currency| { + assert_eq!(*who, ALICE); + assert_eq!(investment_id, INVESTMENT_ID); + assert_eq!(amount, AMOUNT); + assert_eq!(foreign_currency, CURRENCY_ID); + + // Side effects of this call + ForeignInvestment::mock_redemption(|_, _| Ok(0)); + Tokens::mint_into(TRANCHE_CURRENCY, &ALICE, AMOUNT).unwrap(); + Ok(()) + }, + ); + Gateway::mock_submit(|sender, destination, msg| { + assert_eq!(sender, TreasuryAccount::get()); + assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); + assert_eq!( + msg, + Message::ExecutedDecreaseRedeemOrder { + pool_id: POOL_ID, + tranche_id: TRANCHE_ID, + investor: ALICE.into(), + currency: util::currency_index(CURRENCY_ID), + tranche_tokens_payout: AMOUNT, + remaining_redeem_amount: 0, + } + ); + Ok(()) + }); + } + #[test] fn success() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); - AssetRegistry::mock_metadata(|_| Some(util::default_metadata())); - ForeignInvestment::mock_decrease_foreign_redemption( - |who, investment_id, amount, foreign_currency| { - assert_eq!(*who, ALICE); - assert_eq!(investment_id, INVESTMENT_ID); - assert_eq!(amount, AMOUNT); - assert_eq!(foreign_currency, CURRENCY_ID); - - // Side effects of this call - ForeignInvestment::mock_redemption(|_, _| Ok(0)); - Tokens::mint_into(TRANCHE_CURRENCY, &ALICE, AMOUNT).unwrap(); - Ok(()) - }, - ); - Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, TreasuryAccount::get()); - assert_eq!(destination, EVM_DOMAIN_ADDRESS.domain()); - assert_eq!( - msg, - Message::ExecutedDecreaseRedeemOrder { - pool_id: POOL_ID, - tranche_id: TRANCHE_ID, - investor: ALICE.into(), - currency: util::currency_index(CURRENCY_ID), - tranche_tokens_payout: AMOUNT, - remaining_redeem_amount: 0, - } - ); - Ok(()) - }); + config_mocks(); assert_ok!(LiquidityPools::submit( EVM_DOMAIN_ADDRESS, @@ -719,9 +728,7 @@ mod handle_cancel_redeem_order { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); + config_mocks(); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -742,10 +749,7 @@ mod handle_cancel_redeem_order { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); + config_mocks(); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -766,11 +770,7 @@ mod handle_cancel_redeem_order { #[test] fn with_no_metadata() { System::externalities().execute_with(|| { - DomainAccountToDomainAddress::mock_convert(|_| CENTRIFUGE_DOMAIN_ADDRESS); - DomainAddressToAccountId::mock_convert(|_| ALICE); - ForeignInvestment::mock_redemption(|_, _| Ok(AMOUNT)); - Pools::mock_pool_exists(|_| true); - Pools::mock_tranche_exists(|_, _| true); + config_mocks(); AssetRegistry::mock_metadata(|_| None); assert_noop!( From 00c3fec25453e561ab707513227efcc93ae10af0 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Wed, 24 Jul 2024 10:51:52 +0200 Subject: [PATCH 3/3] some adjustements --- pallets/liquidity-pools/src/mock.rs | 9 ++-- pallets/liquidity-pools/src/tests/inbound.rs | 55 ++++++++++---------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/pallets/liquidity-pools/src/mock.rs b/pallets/liquidity-pools/src/mock.rs index f3e6659b39..f194bd2445 100644 --- a/pallets/liquidity-pools/src/mock.rs +++ b/pallets/liquidity-pools/src/mock.rs @@ -23,10 +23,11 @@ pub type AccountId = AccountId32; pub type Ratio = FixedU128; pub const CHAIN_ID: u64 = 1; -pub const ALICE: AccountId = AccountId::new([0; 32]); -pub const ALICE_ETH: [u8; 20] = [0; 20]; -pub const ALICE_EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, ALICE_ETH); -pub const CENTRIFUGE_DOMAIN_ADDRESS: DomainAddress = DomainAddress::Centrifuge([0; 32]); // Alice +pub const ALICE_32: [u8; 32] = [2; 32]; +pub const ALICE: AccountId = AccountId::new(ALICE_32); +pub const ALICE_ETH: [u8; 20] = [2; 20]; +pub const ALICE_EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(42, ALICE_ETH); +pub const CENTRIFUGE_DOMAIN_ADDRESS: DomainAddress = DomainAddress::Centrifuge(ALICE_32); pub const CONTRACT_ACCOUNT: [u8; 20] = [1; 20]; pub const CONTRACT_ACCOUNT_ID: AccountId = AccountId::new([1; 32]); pub const EVM_DOMAIN_ADDRESS: DomainAddress = DomainAddress::EVM(CHAIN_ID, CONTRACT_ACCOUNT); diff --git a/pallets/liquidity-pools/src/tests/inbound.rs b/pallets/liquidity-pools/src/tests/inbound.rs index 1b7fe40869..3c9b876d43 100644 --- a/pallets/liquidity-pools/src/tests/inbound.rs +++ b/pallets/liquidity-pools/src/tests/inbound.rs @@ -90,15 +90,12 @@ mod handle_transfer { mod handle_tranche_tokens_transfer { use super::*; - pub const LOCAL_ALICE: AccountId = AccountId::new([5; 32]); - - fn config_mocks(receiver: DomainAddress, local_representation: AccountId) { - let local_representation_clone = local_representation.clone(); + fn config_mocks(receiver: DomainAddress) { DomainAccountToDomainAddress::mock_convert(move |_| receiver.clone()); - DomainAddressToAccountId::mock_convert(move |_| local_representation_clone.clone()); + DomainAddressToAccountId::mock_convert(move |_| ALICE); Time::mock_now(|| NOW); Permissions::mock_has(move |scope, who, role| { - assert_eq!(who, local_representation.clone()); + assert_eq!(who, ALICE); assert!(matches!(scope, PermissionScope::Pool(POOL_ID))); assert!(matches!( role, @@ -113,7 +110,7 @@ mod handle_tranche_tokens_transfer { #[test] fn success_with_centrifuge_domain() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); Tokens::mint_into( TRANCHE_CURRENCY, @@ -134,6 +131,8 @@ mod handle_tranche_tokens_transfer { } )); + let origin = EVM_DOMAIN_ADDRESS.domain().into_account(); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &origin), 0); assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &ALICE.into()), AMOUNT); }); } @@ -141,20 +140,20 @@ mod handle_tranche_tokens_transfer { #[test] fn success_with_evm_domain() { System::externalities().execute_with(|| { - config_mocks(ALICE_EVM_DOMAIN_ADDRESS, LOCAL_ALICE); + config_mocks(ALICE_EVM_DOMAIN_ADDRESS); TransferFilter::mock_check(|_| Ok(())); Gateway::mock_submit(|sender, destination, msg| { - assert_eq!(sender, LOCAL_ALICE); + assert_eq!(sender, ALICE); assert_eq!(destination, ALICE_EVM_DOMAIN_ADDRESS.domain()); assert_eq!( msg, Message::TransferTrancheTokens { pool_id: POOL_ID, tranche_id: TRANCHE_ID, - sender: LOCAL_ALICE.into(), + sender: ALICE.into(), domain: ALICE_EVM_DOMAIN_ADDRESS.domain().into(), - receiver: ALICE.into(), + receiver: ALICE_EVM_DOMAIN_ADDRESS.address().into(), amount: AMOUNT } ); @@ -180,17 +179,11 @@ mod handle_tranche_tokens_transfer { } )); - assert_eq!( - Tokens::balance(TRANCHE_CURRENCY, &EVM_DOMAIN_ADDRESS.address().into()), - 0 - ); - assert_eq!( - Tokens::balance( - TRANCHE_CURRENCY, - &ALICE_EVM_DOMAIN_ADDRESS.domain().into_account() - ), - AMOUNT - ); + let origin = EVM_DOMAIN_ADDRESS.domain().into_account(); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &origin), 0); + + let destination = ALICE_EVM_DOMAIN_ADDRESS.domain().into_account(); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &destination), AMOUNT); }); } @@ -200,7 +193,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_zero_balance() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); assert_noop!( LiquidityPools::submit( @@ -222,7 +215,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_permissions() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); Permissions::mock_has(|_, _, _| false); assert_noop!( @@ -245,7 +238,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_pool() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); Pools::mock_pool_exists(|_| false); assert_noop!( @@ -268,7 +261,7 @@ mod handle_tranche_tokens_transfer { #[test] fn with_wrong_tranche() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); Pools::mock_tranche_exists(|_, _| false); assert_noop!( @@ -291,7 +284,7 @@ mod handle_tranche_tokens_transfer { #[test] fn without_sufficient_balance() { System::externalities().execute_with(|| { - config_mocks(CENTRIFUGE_DOMAIN_ADDRESS, ALICE); + config_mocks(CENTRIFUGE_DOMAIN_ADDRESS); assert_noop!( LiquidityPools::submit( @@ -569,6 +562,10 @@ mod handle_increase_redeem_order { amount: AMOUNT, }, )); + + let destination = EVM_DOMAIN_ADDRESS.domain().into_account(); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &destination), 0); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &ALICE), AMOUNT); }); } @@ -719,6 +716,10 @@ mod handle_cancel_redeem_order { currency: util::currency_index(CURRENCY_ID), }, )); + + let destination = EVM_DOMAIN_ADDRESS.domain().into_account(); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &ALICE), 0); + assert_eq!(Tokens::balance(TRANCHE_CURRENCY, &destination), AMOUNT); }); }