Skip to content

Commit

Permalink
Substitute mock-accountant macro by a mock builder pallet (#1652)
Browse files Browse the repository at this point in the history
* Replace mock-accountant macro with mock builder pallet and Rate by
Quantity

* remove unused test-utils mocks

* add warn

* update mock-builder dependency

* improve mock configuration

* modify hardcoded values in tests due rounding issues

* modify event index

* fix benchmarking tests

* remove Default restriction for pool mocks
  • Loading branch information
lemunozm authored Dec 15, 2023
1 parent 1d1bdb7 commit ba5466c
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 808 deletions.
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.

85 changes: 82 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 Down Expand Up @@ -64,6 +68,49 @@ 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));
}

#[cfg(feature = "runtime-benchmarks")]
pub fn mock_bench_default_investment_id(
f: impl Fn(T::PoolId) -> T::TrancheCurrency + 'static,
) {
register_call!(f);
}
}

impl<T: Config> PoolInspect<T::AccountId, T::CurrencyId> for Pallet<T> {
Expand All @@ -88,6 +135,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 @@ -130,8 +209,8 @@ pub mod pallet {
type InvestmentId = T::TrancheCurrency;
type PoolId = T::PoolId;

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

This file was deleted.

Loading

0 comments on commit ba5466c

Please sign in to comment.