|
| 1 | +pub use crate::*; |
| 2 | + |
| 3 | +use frame_benchmarking::v2::*; |
| 4 | +use frame_support::assert_ok; |
| 5 | +use frame_system::RawOrigin; |
| 6 | +use sp_std::vec; |
| 7 | + |
| 8 | +/// Helper trait for benchmarking. |
| 9 | +pub trait BenchmarkHelper<AccountId, Balance> { |
| 10 | + fn get_vesting_account_and_amount() -> Option<(AccountId, Balance)>; |
| 11 | +} |
| 12 | + |
| 13 | +impl<AccountId, Balance> BenchmarkHelper<AccountId, Balance> for () { |
| 14 | + fn get_vesting_account_and_amount() -> Option<(AccountId, Balance)> { |
| 15 | + None |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +fn set_balance<T: Config>(who: &T::AccountId, amount: BalanceOf<T>) { |
| 20 | + let _ = <<T as Config>::Currency as Currency<_>>::deposit_creating(&who, amount); |
| 21 | +} |
| 22 | + |
| 23 | +fn total_balance<T: Config>(who: &T::AccountId) -> BalanceOf<T> { |
| 24 | + <<T as Config>::Currency as Currency<_>>::total_balance(who) |
| 25 | +} |
| 26 | + |
| 27 | +fn free_balance<T: Config>(who: &T::AccountId) -> BalanceOf<T> { |
| 28 | + <<T as Config>::Currency as Currency<_>>::free_balance(who) |
| 29 | +} |
| 30 | + |
| 31 | +#[benchmarks] |
| 32 | +mod benchmarks { |
| 33 | + use super::*; |
| 34 | + |
| 35 | + #[benchmark] |
| 36 | + fn vested_transfer() { |
| 37 | + let schedule = VestingScheduleOf::<T> { |
| 38 | + start: 0u32.into(), |
| 39 | + period: 2u32.into(), |
| 40 | + period_count: 3u32.into(), |
| 41 | + per_period: T::MinVestedTransfer::get(), |
| 42 | + }; |
| 43 | + |
| 44 | + // extra 1 dollar to pay fees |
| 45 | + let (from, amount) = T::BenchmarkHelper::get_vesting_account_and_amount().unwrap(); |
| 46 | + set_balance::<T>(&from, schedule.total_amount().unwrap() + amount); |
| 47 | + |
| 48 | + let to: T::AccountId = account("to", 0, 0); |
| 49 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 50 | + |
| 51 | + #[extrinsic_call] |
| 52 | + _(RawOrigin::Signed(from), to_lookup, schedule.clone()); |
| 53 | + |
| 54 | + assert_eq!(total_balance::<T>(&to), schedule.total_amount().unwrap()); |
| 55 | + } |
| 56 | + |
| 57 | + #[benchmark] |
| 58 | + fn claim(i: Linear<1, { T::MaxVestingSchedules::get() }>) { |
| 59 | + let mut schedule = VestingScheduleOf::<T> { |
| 60 | + start: 0u32.into(), |
| 61 | + period: 2u32.into(), |
| 62 | + period_count: 3u32.into(), |
| 63 | + per_period: T::MinVestedTransfer::get(), |
| 64 | + }; |
| 65 | + |
| 66 | + // extra 1 dollar to pay fees |
| 67 | + let (from, amount) = T::BenchmarkHelper::get_vesting_account_and_amount().unwrap(); |
| 68 | + set_balance::<T>( |
| 69 | + &from, |
| 70 | + schedule.total_amount().unwrap().saturating_mul(i.into()) + amount, |
| 71 | + ); |
| 72 | + |
| 73 | + let to: T::AccountId = account("to", 0, 0); |
| 74 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 75 | + |
| 76 | + for _ in 0..i { |
| 77 | + schedule.start = i.into(); |
| 78 | + assert_ok!(Pallet::<T>::vested_transfer( |
| 79 | + RawOrigin::Signed(from.clone()).into(), |
| 80 | + to_lookup.clone(), |
| 81 | + schedule.clone() |
| 82 | + )); |
| 83 | + } |
| 84 | + frame_system::Pallet::<T>::set_block_number(schedule.end().unwrap() + 1u32.into()); |
| 85 | + |
| 86 | + #[extrinsic_call] |
| 87 | + _(RawOrigin::Signed(to.clone())); |
| 88 | + |
| 89 | + assert_eq!( |
| 90 | + free_balance::<T>(&to), |
| 91 | + schedule.total_amount().unwrap().saturating_mul(i.into()), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + #[benchmark] |
| 96 | + fn update_vesting_schedules(i: Linear<1, { T::MaxVestingSchedules::get() }>) { |
| 97 | + let mut schedule = VestingScheduleOf::<T> { |
| 98 | + start: 0u32.into(), |
| 99 | + period: 2u32.into(), |
| 100 | + period_count: 3u32.into(), |
| 101 | + per_period: T::MinVestedTransfer::get(), |
| 102 | + }; |
| 103 | + |
| 104 | + let to: T::AccountId = account("to", 0, 0); |
| 105 | + let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone()); |
| 106 | + |
| 107 | + set_balance::<T>(&to, schedule.total_amount().unwrap().saturating_mul(i.into())); |
| 108 | + |
| 109 | + let mut schedules = vec![]; |
| 110 | + for _ in 0..i { |
| 111 | + schedule.start = i.into(); |
| 112 | + schedules.push(schedule.clone()); |
| 113 | + } |
| 114 | + |
| 115 | + #[extrinsic_call] |
| 116 | + _(RawOrigin::Root, to_lookup, schedules); |
| 117 | + |
| 118 | + assert_eq!( |
| 119 | + free_balance::<T>(&to), |
| 120 | + schedule.total_amount().unwrap().saturating_mul(i.into()), |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + impl_benchmark_test_suite! { |
| 125 | + Pallet, |
| 126 | + crate::mock::ExtBuilder::build(), |
| 127 | + crate::mock::Runtime, |
| 128 | + } |
| 129 | +} |
0 commit comments