Skip to content

Commit 81511f1

Browse files
authored
cleanup the storage (#560)
1 parent 868b0fd commit 81511f1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

vesting/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ impl<T: Config> Pallet<T> {
283283
fn do_claim(who: &T::AccountId) -> BalanceOf<T> {
284284
let locked = Self::locked_balance(who);
285285
if locked.is_zero() {
286+
// cleanup the storage and unlock the fund
287+
<VestingSchedules<T>>::remove(who);
286288
T::Currency::remove_lock(VESTING_LOCK_ID, who);
287289
} else {
288290
T::Currency::set_lock(VESTING_LOCK_ID, who, locked, WithdrawReasons::all());
@@ -331,6 +333,13 @@ impl<T: Config> Pallet<T> {
331333
.try_into()
332334
.map_err(|_| Error::<T>::MaxVestingSchedulesExceeded)?;
333335

336+
// empty vesting schedules cleanup the storage and unlock the fund
337+
if bounded_schedules.len().is_zero() {
338+
<VestingSchedules<T>>::remove(who);
339+
T::Currency::remove_lock(VESTING_LOCK_ID, who);
340+
return Ok(());
341+
}
342+
334343
let total_amount = bounded_schedules
335344
.iter()
336345
.try_fold::<_, _, Result<BalanceOf<T>, DispatchError>>(Zero::zero(), |acc_amount, schedule| {

vesting/src/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,15 @@ fn claim_works() {
218218
assert!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10).is_err());
219219
// unlocked after claiming
220220
assert_ok!(Vesting::claim(Origin::signed(BOB)));
221+
assert_eq!(VestingSchedules::<Runtime>::contains_key(BOB), true);
221222
assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10));
222223
// more are still locked
223224
assert!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 1).is_err());
224225

225226
MockBlockNumberProvider::set(21);
226227
// claim more
227228
assert_ok!(Vesting::claim(Origin::signed(BOB)));
229+
assert_eq!(VestingSchedules::<Runtime>::contains_key(BOB), false);
228230
assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10));
229231
// all used up
230232
assert_eq!(PalletBalances::free_balance(BOB), 0);
@@ -264,6 +266,20 @@ fn update_vesting_schedules_works() {
264266
MockBlockNumberProvider::set(21);
265267
assert_ok!(Vesting::claim(Origin::signed(BOB)));
266268
assert_ok!(PalletBalances::transfer(Origin::signed(BOB), ALICE, 10));
269+
270+
// empty vesting schedules cleanup the storage and unlock the fund
271+
assert_eq!(VestingSchedules::<Runtime>::contains_key(BOB), true);
272+
assert_eq!(
273+
PalletBalances::locks(&BOB).get(0),
274+
Some(&BalanceLock {
275+
id: VESTING_LOCK_ID,
276+
amount: 10u64,
277+
reasons: Reasons::All,
278+
})
279+
);
280+
assert_ok!(Vesting::update_vesting_schedules(Origin::root(), BOB, vec![]));
281+
assert_eq!(VestingSchedules::<Runtime>::contains_key(BOB), false);
282+
assert_eq!(PalletBalances::locks(&BOB), vec![]);
267283
});
268284
}
269285

0 commit comments

Comments
 (0)