Skip to content

Commit

Permalink
Replace mock-accountant macro with mock builder pallet and Rate by
Browse files Browse the repository at this point in the history
Quantity
  • Loading branch information
lemunozm committed Dec 12, 2023
1 parent 6d6cd79 commit 083d77f
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

74 changes: 73 additions & 1 deletion 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,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));
}
}

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
3 changes: 3 additions & 0 deletions pallets/investments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-
[dev-dependencies]
cfg-test-utils = { path = "../../libs/test-utils" }
cfg-types = { path = "../../libs/types" }
cfg-mocks = { workspace = true, default-features = true }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.43" }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.43" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" }
Expand All @@ -44,6 +45,7 @@ runtime-benchmarks = [
"cfg-test-utils/runtime-benchmarks",
"cfg-traits/runtime-benchmarks",
"cfg-types/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
Expand All @@ -67,6 +69,7 @@ try-runtime = [
"cfg-primitives/try-runtime",
"cfg-test-utils/try-runtime",
"cfg-traits/try-runtime",
"cfg-mocks/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
Expand Down
Loading

0 comments on commit 083d77f

Please sign in to comment.