0

I'm trying to run the following benchmark test:

use primitives::{CurrencyId, TokenId};

benchmarks! {
    onboard_asset {
        let caller: T::AccountId = whitelisted_caller();
        let currency_id: CurrencyId = CurrencyId::NativeToken(TokenId::GasToken);
        let enabled: bool = true;
    }: _(RawOrigin::Root, currency_id.clone(), enabled.clone())
    verify {
        assert_eq!(FeeAssets::<T>::get(currency_id), Some(enabled));
    }

but am getting this error:

error[E0308]: mismatched types
  --> pallets/fee-enablement/src/benchmarking.rs:14:24
   |
9  | / benchmarks! {
10 | |     onboard_asset {
11 | |         let caller: T::AccountId = whitelisted_caller();
12 | |         let currency_id: CurrencyId = CurrencyId::NativeToken(TokenId::GasToken);
13 | |         let enabled: bool = true;
14 | |     }: _(RawOrigin::Root, currency_id.clone(), enabled.clone())
   | |                           ^^^^^^^^^^^^^^^^^^^ expected associated type, found enum `CurrencyId`
...  |
36 | |     impl_benchmark_test_suite!(FeeEnablement, crate::mock::ExtBuilder::default().build(), crate::mock::Runtime);
37 | | }
   | |_- arguments to this function are incorrect
   |
   = note: expected associated type `<<T as pallet::Config>::MultiCurrency as orml_traits::MultiCurrency<<T as frame_system::Config>::AccountId>>::CurrencyId`
                         found enum `CurrencyId`
   = help: consider constraining the associated type `<<T as pallet::Config>::MultiCurrency as orml_traits::MultiCurrency<<T as frame_system::Config>::AccountId>>::CurrencyId` to `CurrencyId` or calling a method that returns `<<T as pallet::Config>::MultiCurrency as orml_traits::MultiCurrency<<T as frame_system::Config>::AccountId>>::CurrencyId`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: associated function defined here
  --> pallets/fee-enablement/src/lib.rs:77:10
   |
74 |        #[pallet::call]
   |   _______________-
   |  |_______________|
   | ||
75 | ||     impl<T: Config> Pallet<T> {
76 | ||         #[pallet::weight(1000]
77 | ||         pub fn onboard_asset(
   | ||                ^^^^^^^^^^^^^
78 | ||             origin: OriginFor<T>,
79 | ||             asset_id: CurrencyOf<T, T::MultiCurrency>,
   | ||____________-
80 | |              enabled: bool,
   | |_____________-

The associated type <<T as pallet::Config>::MultiCurrency as orml_traits::MultiCurrency<<T as frame_system::Config>::AccountId>>::CurrencyId is declared to be the enum CurrencyIdin the mock runtime definition.

The same function accepts CurrencyId enum as input parameter when I run it in unit tests, but throws this mismatched types error for benchmark tests. Please help me fix this error. Thanks in advance :)

2 Answers 2

1

You can add a where_clause to your benchmarks which allows to enforce that associated types from the pallet's config are some particular concrete types.

I believe something along this lines should work for your case:

benchmarks! {
    where_clause {
        where
            T: pallet::Config,
            T::MultiCurrency: orml_traits::MultiCurrency<<T as frame_system::Config>::AccountId>>,
            T::MultiCurrency::CurrencyId: CurrencyId,
    }

    ...
}
1
  • Thank you, using the where_clause worked.
    – AKASH S M
    Sep 27, 2022 at 13:33
0

You should convert the CurrencyId to the associated type.

I'm not familiar with your code.

But here is a simple example.

Note, these are pseudo codes.

trait Currency {
    type Balance: From<u128>;
}
type Balance = Currency::Balance;
fn mint(origin, balance: Balance) {}


benchmarks! {
    let amount: Balance = 100_u128.into();
    mint(someone, amount);
}

Although, we config the Balance = u128 in mock/runtime.

You can not use u128 directly, you need to convert it to the Balance associated type.

Just like you can not use CurrencyId directly, you need to convert it to the CurrencyOf<T, T::MultiCurrency>.

1
  • You actually can use u128 directly in benchmarks, if you add an appropriate where_clause. Take a look a this code. Sep 27, 2022 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.