1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::Bounded;
use crate::Pallet as Vesting;
const SEED: u32 = 0;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
fn add_locks<T: Config>(who: &T::AccountId, n: u8) {
for id in 0..n {
let lock_id = [id; 8];
let locked = 100u32;
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
T::Currency::set_lock(lock_id, who, locked.into(), reasons);
}
}
fn add_vesting_schedule<T: Config>(who: &T::AccountId) -> Result<(), &'static str> {
let locked = 100u32;
let per_block = 10u32;
let starting_block = 1u32;
System::<T>::set_block_number(0u32.into());
Vesting::<T>::add_vesting_schedule(
&who,
locked.into(),
per_block.into(),
starting_block.into(),
)?;
Ok(())
}
benchmarks! {
vest_locked {
let l in 0 .. MaxLocksOf::<T>::get();
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
add_locks::<T>(&caller, l as u8);
add_vesting_schedule::<T>(&caller)?;
System::<T>::set_block_number(T::BlockNumber::zero());
assert_eq!(
Vesting::<T>::vesting_balance(&caller),
Some(100u32.into()),
"Vesting schedule not added",
);
}: vest(RawOrigin::Signed(caller.clone()))
verify {
assert_eq!(
Vesting::<T>::vesting_balance(&caller),
Some(100u32.into()),
"Vesting schedule was removed",
);
}
vest_unlocked {
let l in 0 .. MaxLocksOf::<T>::get();
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
add_locks::<T>(&caller, l as u8);
add_vesting_schedule::<T>(&caller)?;
System::<T>::set_block_number(20u32.into());
assert_eq!(
Vesting::<T>::vesting_balance(&caller),
Some(BalanceOf::<T>::zero()),
"Vesting schedule still active",
);
}: vest(RawOrigin::Signed(caller.clone()))
verify {
assert_eq!(
Vesting::<T>::vesting_balance(&caller),
None,
"Vesting schedule was not removed",
);
}
vest_other_locked {
let l in 0 .. MaxLocksOf::<T>::get();
let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
T::Currency::make_free_balance_be(&other, BalanceOf::<T>::max_value());
add_locks::<T>(&other, l as u8);
add_vesting_schedule::<T>(&other)?;
System::<T>::set_block_number(T::BlockNumber::zero());
assert_eq!(
Vesting::<T>::vesting_balance(&other),
Some(100u32.into()),
"Vesting schedule not added",
);
let caller: T::AccountId = whitelisted_caller();
}: vest_other(RawOrigin::Signed(caller.clone()), other_lookup)
verify {
assert_eq!(
Vesting::<T>::vesting_balance(&other),
Some(100u32.into()),
"Vesting schedule was removed",
);
}
vest_other_unlocked {
let l in 0 .. MaxLocksOf::<T>::get();
let other: T::AccountId = account("other", 0, SEED);
let other_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(other.clone());
T::Currency::make_free_balance_be(&other, BalanceOf::<T>::max_value());
add_locks::<T>(&other, l as u8);
add_vesting_schedule::<T>(&other)?;
System::<T>::set_block_number(20u32.into());
assert_eq!(
Vesting::<T>::vesting_balance(&other),
Some(BalanceOf::<T>::zero()),
"Vesting schedule still active",
);
let caller: T::AccountId = whitelisted_caller();
}: vest_other(RawOrigin::Signed(caller.clone()), other_lookup)
verify {
assert_eq!(
Vesting::<T>::vesting_balance(&other),
None,
"Vesting schedule was not removed",
);
}
vested_transfer {
let l in 0 .. MaxLocksOf::<T>::get();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let target: T::AccountId = account("target", 0, SEED);
let target_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(target.clone());
add_locks::<T>(&target, l as u8);
let transfer_amount = T::MinVestedTransfer::get();
let vesting_schedule = VestingInfo {
locked: transfer_amount,
per_block: 10u32.into(),
starting_block: 1u32.into(),
};
}: _(RawOrigin::Signed(caller), target_lookup, vesting_schedule)
verify {
assert_eq!(
T::MinVestedTransfer::get(),
T::Currency::free_balance(&target),
"Transfer didn't happen",
);
assert_eq!(
Vesting::<T>::vesting_balance(&target),
Some(T::MinVestedTransfer::get()),
"Lock not created",
);
}
force_vested_transfer {
let l in 0 .. MaxLocksOf::<T>::get();
let source: T::AccountId = account("source", 0, SEED);
let source_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(source.clone());
T::Currency::make_free_balance_be(&source, BalanceOf::<T>::max_value());
let target: T::AccountId = account("target", 0, SEED);
let target_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(target.clone());
add_locks::<T>(&target, l as u8);
let transfer_amount = T::MinVestedTransfer::get();
let vesting_schedule = VestingInfo {
locked: transfer_amount,
per_block: 10u32.into(),
starting_block: 1u32.into(),
};
}: _(RawOrigin::Root, source_lookup, target_lookup, vesting_schedule)
verify {
assert_eq!(
T::MinVestedTransfer::get(),
T::Currency::free_balance(&target),
"Transfer didn't happen",
);
assert_eq!(
Vesting::<T>::vesting_balance(&target),
Some(T::MinVestedTransfer::get()),
"Lock not created",
);
}
}
impl_benchmark_test_suite!(
Vesting,
crate::mock::ExtBuilder::default().existential_deposit(256).build(),
crate::mock::Test,
);