From 211594bfde62aa83ebda758977d331fc79891987 Mon Sep 17 00:00:00 2001
From: Ermal Kaleci <ermalkaleci@gmail.com>
Date: Tue, 18 Feb 2020 05:47:22 +0100
Subject: [PATCH] remove ensure_lockable (#92)

* remove ensure_lockable

* remove unused

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
---
 vesting/src/lib.rs   | 21 +--------------------
 vesting/src/tests.rs | 38 +-------------------------------------
 2 files changed, 2 insertions(+), 57 deletions(-)

diff --git a/vesting/src/lib.rs b/vesting/src/lib.rs
index db03727..0a4bc69 100644
--- a/vesting/src/lib.rs
+++ b/vesting/src/lib.rs
@@ -14,7 +14,7 @@ use sp_std::{
 // #3295 https://github.com/paritytech/substrate/issues/3295
 use frame_system::{self as system, ensure_root, ensure_signed};
 use sp_runtime::{
-	traits::{AtLeast32Bit, CheckedAdd, Saturating, StaticLookup, Zero},
+	traits::{AtLeast32Bit, CheckedAdd, StaticLookup, Zero},
 	DispatchResult, RuntimeDebug,
 };
 
@@ -110,7 +110,6 @@ decl_error! {
 		ZeroVestingPeriodCount,
 		NumOverflow,
 		InsufficientBalanceToLock,
-		HasNonVestingLocks,
 	}
 }
 
@@ -180,8 +179,6 @@ impl<T: Trait> Module<T> {
 		to: &T::AccountId,
 		schedule: VestingScheduleOf<T>,
 	) -> DispatchResult {
-		Self::ensure_lockable(to)?;
-
 		let schedule_amount = Self::ensure_valid_vesting_schedule(&schedule)?;
 		let total_amount = Self::locked_balance(to)
 			.checked_add(&schedule_amount.into())
@@ -195,8 +192,6 @@ impl<T: Trait> Module<T> {
 	}
 
 	fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec<VestingScheduleOf<T>>) -> DispatchResult {
-		Self::ensure_lockable(who)?;
-
 		let total_amount = schedules.iter().try_fold::<_, _, Result<BalanceOf<T>, Error<T>>>(
 			Zero::zero(),
 			|acc_amount, schedule| {
@@ -223,18 +218,4 @@ impl<T: Trait> Module<T> {
 
 		schedule.total_amount().ok_or(Error::<T>::NumOverflow)
 	}
-
-	/// Ensure no other types of locks except `VESTING_LOCK_ID`.
-	fn ensure_lockable(who: &T::AccountId) -> DispatchResult {
-		// FIXME: when locks query in `LockableCurrency` is ready(https://github.com/paritytech/substrate/issues/4655):
-		// 1. use locks query instead of `ensure_can_withdraw` for locks check.
-		// 2. remove `do_claim` workaround
-		let _ = Self::do_claim(who);
-
-		let balance = T::Currency::free_balance(who);
-		let locked = Self::locked_balance(who);
-		let usable = balance.saturating_sub(locked);
-		T::Currency::ensure_can_withdraw(who, usable, WithdrawReasons::all(), locked)
-			.map_err(|_| Error::<T>::HasNonVestingLocks.into())
-	}
 }
diff --git a/vesting/src/tests.rs b/vesting/src/tests.rs
index a8a74e7..6ccc060 100644
--- a/vesting/src/tests.rs
+++ b/vesting/src/tests.rs
@@ -3,7 +3,7 @@
 #![cfg(test)]
 
 use super::*;
-use frame_support::{assert_err, assert_noop, assert_ok, traits::WithdrawReason};
+use frame_support::{assert_err, assert_ok, traits::WithdrawReason};
 use mock::{ExtBuilder, Origin, PalletBalances, Runtime, System, TestEvent, Vesting, ALICE, BOB};
 use pallet_balances::{BalanceLock, Reasons};
 
@@ -109,24 +109,6 @@ fn add_vesting_schedule_fails_if_zero_period_or_count() {
 	});
 }
 
-#[test]
-fn add_vesting_schedule_fails_if_unexpected_existing_locks() {
-	ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| {
-		assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 1));
-		PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, WithdrawReasons::all());
-		let schedule = VestingSchedule {
-			start: 1u64,
-			period: 1u64,
-			period_count: 1u32,
-			per_period: 10u64,
-		};
-		assert_err!(
-			Vesting::add_vesting_schedule(Origin::signed(ALICE), BOB, schedule),
-			Error::<Runtime>::HasNonVestingLocks
-		);
-	});
-}
-
 #[test]
 fn add_vesting_schedule_fails_if_transfer_err() {
 	ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| {
@@ -250,21 +232,3 @@ fn update_vesting_schedules_fails_if_unexpected_existing_locks() {
 		PalletBalances::set_lock(*b"prelocks", &BOB, 0u64, WithdrawReasons::all());
 	});
 }
-
-#[test]
-fn update_vesting_schedules_fails_if_insufficient_balance_to_lock() {
-	ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| {
-		assert_ok!(PalletBalances::transfer(Origin::signed(ALICE), BOB, 10));
-		PalletBalances::set_lock(*b"prelocks", &BOB, 10u64, WithdrawReasons::all());
-		let schedule = VestingSchedule {
-			start: 1u64,
-			period: 1u64,
-			period_count: 1u32,
-			per_period: 10u64,
-		};
-		assert_noop!(
-			Vesting::update_vesting_schedules(Origin::ROOT, BOB, vec![schedule]),
-			Error::<Runtime>::HasNonVestingLocks
-		);
-	});
-}
-- 
GitLab