Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitute mock-accountant macro by a mock builder pallet #1652

Merged
merged 10 commits into from
Dec 15, 2023
4 changes: 2 additions & 2 deletions Cargo.lock

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

78 changes: 75 additions & 3 deletions libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::{PoolInspect, PoolReserve, PriceValue, Seconds, TrancheTokenPrice};
use cfg_traits::{
investments::InvestmentAccountant, PoolInspect, PoolReserve, PriceValue, Seconds,
TrancheTokenPrice,
};
use cfg_types::investments::InvestmentInfo;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
Expand All @@ -22,7 +26,7 @@ pub mod pallet {
type Balance;
type BalanceRatio;
type CurrencyId;
type TrancheCurrency;
type TrancheCurrency: Default;
}

#[pallet::pallet]
Expand Down Expand Up @@ -64,6 +68,42 @@ pub mod pallet {
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_info(
f: impl Fn(
T::TrancheCurrency,
) -> Result<
InvestmentInfo<T::AccountId, T::CurrencyId, T::TrancheCurrency>,
DispatchError,
> + 'static,
) {
register_call!(f);
}

pub fn mock_balance(f: impl Fn(T::TrancheCurrency, &T::AccountId) -> T::Balance + 'static) {
register_call!(move |(a, b)| f(a, b));
}

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

#[allow(non_snake_case)]
pub fn mock_InvestmentAccountant_deposit(
f: impl Fn(&T::AccountId, T::TrancheCurrency, T::Balance) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

#[allow(non_snake_case)]
pub fn mock_InvestmentAccountant_withdraw(
f: impl Fn(&T::AccountId, T::TrancheCurrency, T::Balance) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
Comment on lines +94 to +106
Copy link
Contributor Author

@lemunozm lemunozm Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application of foss3/runtime-pallet-library#16, @wischli

Basically the pallet implements deposit() and withdraw() methods from two different traits. So we need to add mock_<trait>_method() to one of these deposit()/withdraw() to differentiate from the others

In those cases, the T::AccountId used as generic for InvestmentAccountant was wrongly internally coded.

}

impl<T: Config> PoolInspect<T::AccountId, T::CurrencyId> for Pallet<T> {
Expand All @@ -88,6 +128,38 @@ pub mod pallet {
}
}

impl<T: Config> InvestmentAccountant<T::AccountId> for Pallet<T> {
type Amount = T::Balance;
type Error = DispatchError;
type InvestmentId = T::TrancheCurrency;
type InvestmentInfo = InvestmentInfo<T::AccountId, T::CurrencyId, Self::InvestmentId>;

fn info(a: Self::InvestmentId) -> Result<Self::InvestmentInfo, DispatchError> {
execute_call!(a)
}

fn balance(a: Self::InvestmentId, b: &T::AccountId) -> Self::Amount {
execute_call!((a, b))
}

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

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

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

impl<T: Config> TrancheTokenPrice<T::AccountId, T::CurrencyId> for Pallet<T> {
type BalanceRatio = T::BalanceRatio;
type Moment = Seconds;
Expand Down Expand Up @@ -131,7 +203,7 @@ pub mod pallet {
type PoolId = T::PoolId;

fn bench_default_investment_id(_: Self::PoolId) -> Self::InvestmentId {
unimplemented!();
T::TrancheCurrency::default()
}
}
}
244 changes: 0 additions & 244 deletions libs/test-utils/src/mocks/accountant.rs

This file was deleted.

Loading
Loading