|
| 1 | +pub use crate::*; |
| 2 | + |
| 3 | +use frame_benchmarking::v2::*; |
| 4 | +use frame_support::assert_ok; |
| 5 | +use frame_system::RawOrigin; |
| 6 | +use sp_runtime::traits::SaturatedConversion; |
| 7 | + |
| 8 | +/// Helper trait for benchmarking. |
| 9 | +pub trait BenchmarkHelper<CurrencyId, Balance> { |
| 10 | + /// Returns a currency id and amount to be used in benchmarking. |
| 11 | + fn get_currency_id_and_amount() -> Option<(CurrencyId, Balance)>; |
| 12 | +} |
| 13 | + |
| 14 | +impl<CurrencyId, Balance> BenchmarkHelper<CurrencyId, Balance> for () { |
| 15 | + fn get_currency_id_and_amount() -> Option<(CurrencyId, Balance)> { |
| 16 | + None |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +#[benchmarks] |
| 21 | +mod benchmarks { |
| 22 | + use super::*; |
| 23 | + |
| 24 | + #[benchmark] |
| 25 | + fn transfer() { |
| 26 | + let from: T::AccountId = account("from", 0, 0); |
| 27 | + |
| 28 | + let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap(); |
| 29 | + |
| 30 | + assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance( |
| 31 | + currency_id, |
| 32 | + &from, |
| 33 | + amount.saturated_into() |
| 34 | + )); |
| 35 | + |
| 36 | + let to: T::AccountId = account("to", 0, 0); |
| 37 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 38 | + |
| 39 | + #[extrinsic_call] |
| 40 | + _(RawOrigin::Signed(from), to_lookup, currency_id, amount); |
| 41 | + |
| 42 | + assert_eq!(Pallet::<T>::total_balance(currency_id, &to), amount); |
| 43 | + } |
| 44 | + |
| 45 | + #[benchmark] |
| 46 | + fn transfer_all() { |
| 47 | + let from: T::AccountId = account("from", 0, 0); |
| 48 | + |
| 49 | + let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap(); |
| 50 | + |
| 51 | + assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance( |
| 52 | + currency_id, |
| 53 | + &from, |
| 54 | + amount.saturated_into() |
| 55 | + )); |
| 56 | + |
| 57 | + let to: T::AccountId = account("to", 0, 0); |
| 58 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 59 | + |
| 60 | + #[extrinsic_call] |
| 61 | + _(RawOrigin::Signed(from.clone()), to_lookup, currency_id, false); |
| 62 | + |
| 63 | + assert_eq!( |
| 64 | + <Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &from), |
| 65 | + 0u32.into() |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + #[benchmark] |
| 70 | + fn transfer_keep_alive() { |
| 71 | + let from: T::AccountId = account("from", 0, 0); |
| 72 | + |
| 73 | + let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap(); |
| 74 | + |
| 75 | + assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance( |
| 76 | + currency_id, |
| 77 | + &from, |
| 78 | + amount.saturating_mul(2u32.into()).saturated_into() |
| 79 | + )); |
| 80 | + |
| 81 | + let to: T::AccountId = account("to", 0, 0); |
| 82 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 83 | + |
| 84 | + #[extrinsic_call] |
| 85 | + _(RawOrigin::Signed(from), to_lookup, currency_id, amount); |
| 86 | + |
| 87 | + assert_eq!( |
| 88 | + <Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to), |
| 89 | + amount |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[benchmark] |
| 94 | + fn force_transfer() { |
| 95 | + let from: T::AccountId = account("from", 0, 0); |
| 96 | + let from_lookup = <T as frame_system::Config>::Lookup::unlookup(from.clone()); |
| 97 | + |
| 98 | + let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap(); |
| 99 | + |
| 100 | + assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance( |
| 101 | + currency_id, |
| 102 | + &from, |
| 103 | + amount.saturated_into() |
| 104 | + )); |
| 105 | + |
| 106 | + let to: T::AccountId = account("to", 0, 0); |
| 107 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 108 | + |
| 109 | + #[extrinsic_call] |
| 110 | + _(RawOrigin::Root, from_lookup, to_lookup, currency_id, amount); |
| 111 | + |
| 112 | + assert_eq!( |
| 113 | + <Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to), |
| 114 | + amount |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + #[benchmark] |
| 119 | + fn set_balance() { |
| 120 | + let who: T::AccountId = account("who", 0, 0); |
| 121 | + let who_lookup = <T as frame_system::Config>::Lookup::unlookup(who.clone()); |
| 122 | + |
| 123 | + let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap(); |
| 124 | + |
| 125 | + #[extrinsic_call] |
| 126 | + _(RawOrigin::Root, who_lookup, currency_id, amount, amount); |
| 127 | + |
| 128 | + assert_eq!( |
| 129 | + <Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &who), |
| 130 | + amount.saturating_mul(2u32.into()) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + impl_benchmark_test_suite! { |
| 135 | + Pallet, |
| 136 | + crate::mock::ExtBuilder::default().build(), |
| 137 | + crate::mock::Runtime, |
| 138 | + } |
| 139 | +} |
0 commit comments