#![cfg(feature = "runtime-benchmarks")]
use crate::*;
use frame_benchmarking::v2::*;
use frame_support::traits::Get;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
const SEED: u32 = 0;
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn claim() {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
}
#[benchmark]
fn transfer() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let recipient: T::AccountId = account("recipient", 0, SEED);
let recipient_lookup = T::Lookup::unlookup(recipient.clone());
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), recipient_lookup, account_index);
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
Ok(())
}
#[benchmark]
fn free() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);
assert_eq!(Accounts::<T>::get(account_index), None);
Ok(())
}
#[benchmark]
fn force_transfer() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
let original: T::AccountId = account("original", 0, SEED);
T::Currency::make_free_balance_be(&original, BalanceOf::<T>::max_value());
let recipient: T::AccountId = account("recipient", 0, SEED);
let recipient_lookup = T::Lookup::unlookup(recipient.clone());
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
Pallet::<T>::claim(RawOrigin::Signed(original).into(), account_index)?;
#[extrinsic_call]
_(RawOrigin::Root, recipient_lookup, account_index, false);
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
Ok(())
}
#[benchmark]
fn freeze() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);
assert_eq!(Accounts::<T>::get(account_index).unwrap().2, true);
Ok(())
}
#[benchmark]
fn poke_deposit() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let original_deposit = T::Deposit::get();
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
assert_eq!(Accounts::<T>::get(account_index).unwrap().1, original_deposit);
assert_eq!(T::Currency::reserved_balance(&caller), original_deposit);
let additional_amount = 2u32.into();
T::Currency::reserve(&caller, additional_amount)?;
assert_eq!(
T::Currency::reserved_balance(&caller),
original_deposit.saturating_add(additional_amount)
);
Accounts::<T>::try_mutate(account_index, |maybe_value| -> Result<(), BenchmarkError> {
let (account, amount, perm) = maybe_value
.take()
.ok_or(BenchmarkError::Stop("Mutating storage to change deposits failed"))?;
*maybe_value = Some((account, amount.saturating_add(additional_amount), perm));
Ok(())
})?;
assert_eq!(
Accounts::<T>::get(account_index).unwrap().1,
original_deposit.saturating_add(additional_amount)
);
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);
assert!(Accounts::<T>::contains_key(account_index));
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
assert_eq!(Accounts::<T>::get(account_index).unwrap().1, original_deposit);
assert_eq!(T::Currency::reserved_balance(&caller), original_deposit);
Ok(())
}
impl_benchmark_test_suite!(Pallet, mock::new_test_ext(), mock::Test);
}