Skip to content

Commit

Permalink
Liquidity-pools: Add unitary tests (#1879)
Browse files Browse the repository at this point in the history
* prepare mock for UTs and simplify some minor things

* normal function instead of Convert trait

* rename into_account_id() to evm_to_account()

* add UTs for transfer

* simplify account conversion

* remove unused CurrencyIdOf

* remove already covered tests

* remove democracy utils

* apply Cosmin suggestions

* add transfer_tranche_tokens tests

* minor clean

* add add_pool tests

* add add_tranche tests

* check balance post transfers

* fix lp tests

* fix imports after rebasing
  • Loading branch information
lemunozm authored Jun 25, 2024
1 parent ee8fc69 commit 47384e9
Show file tree
Hide file tree
Showing 32 changed files with 1,207 additions and 1,155 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions libs/mocks/src/try_convert.rs → libs/mocks/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ pub mod pallet {
use cfg_traits::TryConvert;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call_instance, register_call_instance};
use sp_runtime::traits::Convert;

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
type From;

type To;

type Error;
}

#[pallet::pallet]
Expand All @@ -20,16 +18,26 @@ pub mod pallet {
type CallIds<T: Config<I>, I: 'static = ()> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn mock_try_convert(f: impl Fn(T::From) -> Result<T::To, T::Error> + 'static) {
pub fn mock_try_convert(f: impl Fn(T::From) -> Result<T::To, DispatchError> + 'static) {
register_call_instance!(f);
}

pub fn mock_convert(f: impl Fn(T::From) -> T::To + 'static) {
register_call_instance!(f);
}
}

impl<T: Config<I>, I: 'static> TryConvert<T::From, T::To> for Pallet<T, I> {
type Error = T::Error;
type Error = DispatchError;

fn try_convert(from: T::From) -> Result<T::To, Self::Error> {
execute_call_instance!(from)
}
}

impl<T: Config<I>, I: 'static> Convert<T::From, T::To> for Pallet<T, I> {
fn convert(from: T::From) -> T::To {
execute_call_instance!(from)
}
}
}
159 changes: 159 additions & 0 deletions libs/mocks/src/foreign_investment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::investments::ForeignInvestment;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Amount;
type TrancheAmount;
type CurrencyId;
type InvestmentId;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_increase_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount, T::CurrencyId) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_decrease_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount, T::CurrencyId) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_increase_foreign_redemption(
f: impl Fn(
&T::AccountId,
T::InvestmentId,
T::TrancheAmount,
T::CurrencyId,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_decrease_foreign_redemption(
f: impl Fn(
&T::AccountId,
T::InvestmentId,
T::TrancheAmount,
T::CurrencyId,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_collect_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::CurrencyId) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_collect_foreign_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId, T::CurrencyId) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_investment(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::Amount, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::TrancheAmount, DispatchError>
+ 'static,
) {
register_call!(move |(a, b)| f(a, b));
}
}

impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> {
type Amount = T::Amount;
type CurrencyId = T::CurrencyId;
type Error = DispatchError;
type InvestmentId = T::InvestmentId;
type TrancheAmount = T::TrancheAmount;

fn increase_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn decrease_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn increase_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::TrancheAmount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn decrease_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::TrancheAmount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn collect_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn collect_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn investment(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Amount, DispatchError> {
execute_call!((a, b))
}

fn redemption(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::TrancheAmount, DispatchError> {
execute_call!((a, b))
}
}
}
5 changes: 3 additions & 2 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pub mod asset_registry;
pub mod change_guard;
pub mod converter;
pub mod currency_conversion;
pub mod data;
pub mod fees;
pub mod foreign_investment;
pub mod investment;
pub mod liquidity_pools;
pub mod liquidity_pools_gateway_routers;
pub mod outbound_queue;
pub mod pay_fee;
pub mod permissions;
pub mod pools;
Expand All @@ -14,7 +17,6 @@ pub mod rewards;
pub mod status_notification;
pub mod time;
pub mod token_swaps;
pub mod try_convert;
pub mod value_provider;
pub mod write_off_policy;

Expand All @@ -33,7 +35,6 @@ pub use rewards::pallet as pallet_mock_rewards;
pub use status_notification::pallet as pallet_mock_status_notification;
pub use time::pallet as pallet_mock_time;
pub use token_swaps::pallet as pallet_mock_token_swaps;
pub use try_convert::pallet as pallet_mock_try_convert;
pub use value_provider::pallet as pallet_mock_value_provider;
pub use write_off_policy::pallet as pallet_mock_write_off_policy;

Expand Down
37 changes: 37 additions & 0 deletions libs/mocks/src/outbound_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::OutboundQueue;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Sender;
type Destination;
type Message;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_submit(
f: impl Fn(T::Sender, T::Destination, T::Message) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
}

impl<T: Config> OutboundQueue for Pallet<T> {
type Destination = T::Destination;
type Message = T::Message;
type Sender = T::Sender;

fn submit(a: Self::Sender, b: Self::Destination, c: Self::Message) -> DispatchResult {
execute_call!((a, b, c))
}
}
}
36 changes: 29 additions & 7 deletions libs/types/src/domain_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cfg_utils::{decode_be_bytes, vec_to_fixed_array};
use frame_support::pallet_prelude::RuntimeDebug;
use parity_scale_codec::{Decode, Encode, Input, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::traits::{AccountIdConversion, Convert};
use sp_runtime::traits::AccountIdConversion;
use sp_std::{vec, vec::Vec};

use crate::EVMChainId;
Expand Down Expand Up @@ -63,12 +63,12 @@ impl Codec for Domain {
}
}

impl<AccountId> Convert<Domain, AccountId> for Domain
where
AccountId: Encode + Decode,
{
fn convert(domain: Domain) -> AccountId {
DomainLocator { domain }.into_account_truncating()
impl Domain {
pub fn into_account<AccountId: Encode + Decode>(&self) -> AccountId {
DomainLocator {
domain: self.clone(),
}
.into_account_truncating()
}
}

Expand Down Expand Up @@ -118,3 +118,25 @@ impl DomainAddress {
self.clone().into()
}
}

#[cfg(test)]
mod tests {
use parity_scale_codec::{Decode, Encode};

use super::*;

#[test]
fn test_domain_encode_decode() {
test_domain_identity(Domain::Centrifuge);
test_domain_identity(Domain::EVM(1284));
test_domain_identity(Domain::EVM(1));
}

/// Test that (decode . encode) results in the original value
fn test_domain_identity(domain: Domain) {
let encoded = domain.encode();
let decoded = Domain::decode(&mut encoded.as_slice()).unwrap();

assert_eq!(domain, decoded);
}
}
12 changes: 12 additions & 0 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ pub struct CustomMetadata {
pub local_representation: Option<LocalAssetId>,
}

#[cfg(feature = "std")]
pub fn default_metadata() -> AssetMetadata {
AssetMetadata {
decimals: 0,
name: Default::default(),
symbol: Default::default(),
existential_deposit: 0,
location: None,
additional: Default::default(),
}
}

/// The Cross Chain Transferability property of an asset describes the way(s),
/// if any, that said asset is cross-chain transferable. It may currently be
/// transferable through Xcm, Centrifuge Liquidity Pools, or All .
Expand Down
Loading

0 comments on commit 47384e9

Please sign in to comment.