From 2a874fc711ec377cc143e5288b03c8eb77b19613 Mon Sep 17 00:00:00 2001
From: zjb0807 <zjb0807@qq.com>
Date: Wed, 14 Jul 2021 07:22:35 +0800
Subject: [PATCH] fix module account ed (#549)

---
 tokens/src/lib.rs   | 25 ++++++++-----------------
 tokens/src/mock.rs  |  7 ++++++-
 tokens/src/tests.rs | 18 +++++-------------
 3 files changed, 19 insertions(+), 31 deletions(-)

diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs
index 6472547..afb5f7d 100644
--- a/tokens/src/lib.rs
+++ b/tokens/src/lib.rs
@@ -48,7 +48,7 @@ use frame_support::{
 		LockableCurrency as PalletLockableCurrency, MaxEncodedLen, ReservableCurrency as PalletReservableCurrency,
 		SignedImbalance, WithdrawReasons,
 	},
-	transactional, BoundedVec, PalletId,
+	transactional, BoundedVec,
 };
 use frame_system::{ensure_signed, pallet_prelude::*};
 use orml_traits::{
@@ -59,8 +59,8 @@ use orml_traits::{
 };
 use sp_runtime::{
 	traits::{
-		AccountIdConversion, AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member,
-		Saturating, StaticLookup, Zero,
+		AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, Saturating,
+		StaticLookup, Zero,
 	},
 	ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
 };
@@ -360,11 +360,6 @@ pub mod module {
 }
 
 impl<T: Config> Pallet<T> {
-	/// Check whether account_id is a module account
-	pub(crate) fn is_module_account_id(account_id: &T::AccountId) -> bool {
-		PalletId::try_from_account(account_id).is_some()
-	}
-
 	pub(crate) fn deposit_consequence(
 		_who: &T::AccountId,
 		currency_id: T::CurrencyId,
@@ -457,9 +452,8 @@ impl<T: Config> Pallet<T> {
 				*maybe_account = if total.is_zero() {
 					None
 				} else {
-					// if non_zero total is below existential deposit and the account is not a
-					// module account, should handle the dust.
-					if total < T::ExistentialDeposits::get(&currency_id) && !Self::is_module_account_id(who) {
+					// if non_zero total is below existential deposit, should handle the dust.
+					if total < T::ExistentialDeposits::get(&currency_id) {
 						maybe_dust = Some(total);
 					}
 					Some(account)
@@ -594,12 +588,9 @@ impl<T: Config> Pallet<T> {
 				to_account.free = to_account.free.checked_add(&amount).ok_or(ArithmeticError::Overflow)?;
 
 				let ed = T::ExistentialDeposits::get(&currency_id);
-				// if to_account non_zero total is below existential deposit and the account is
-				// not a module account, would return an error.
-				ensure!(
-					to_account.total() >= ed || Self::is_module_account_id(to),
-					Error::<T>::ExistentialDeposit
-				);
+				// if to_account non_zero total is below existential deposit, would return an
+				// error.
+				ensure!(to_account.total() >= ed, Error::<T>::ExistentialDeposit);
 
 				Self::ensure_can_withdraw(currency_id, from, amount)?;
 
diff --git a/tokens/src/mock.rs b/tokens/src/mock.rs
index 92a1d9b..d12c8d3 100644
--- a/tokens/src/mock.rs
+++ b/tokens/src/mock.rs
@@ -6,10 +6,15 @@ use super::*;
 use frame_support::{
 	construct_runtime, parameter_types,
 	traits::{ChangeMembers, ContainsLengthBound, SaturatingCurrencyToVote, SortedMembers},
+	PalletId,
 };
 use orml_traits::parameter_type_with_key;
 use sp_core::H256;
-use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32, Permill};
+use sp_runtime::{
+	testing::Header,
+	traits::{AccountIdConversion, IdentityLookup},
+	AccountId32, Permill,
+};
 use sp_std::cell::RefCell;
 
 pub type AccountId = AccountId32;
diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs
index bf37c29..f9eb504 100644
--- a/tokens/src/tests.rs
+++ b/tokens/src/tests.rs
@@ -15,21 +15,10 @@ fn minimum_balance_work() {
 	});
 }
 
-#[test]
-fn is_module_account_id_work() {
-	ExtBuilder::default().build().execute_with(|| {
-		assert_eq!(Tokens::is_module_account_id(&ALICE), false);
-		assert_eq!(Tokens::is_module_account_id(&BOB), false);
-		assert_eq!(Tokens::is_module_account_id(&TREASURY_ACCOUNT), false);
-		assert_eq!(Tokens::is_module_account_id(&DustAccount::get()), true);
-	});
-}
-
 #[test]
 fn remove_dust_work() {
 	ExtBuilder::default().build().execute_with(|| {
 		System::set_block_number(1);
-
 		assert_ok!(Tokens::deposit(DOT, &ALICE, 100));
 		assert_eq!(Tokens::total_issuance(DOT), 100);
 		assert_eq!(Accounts::<Runtime>::contains_key(ALICE, DOT), true);
@@ -49,17 +38,20 @@ fn remove_dust_work() {
 		assert_eq!(Tokens::free_balance(DOT, &DustAccount::get()), 0);
 		assert_eq!(System::providers(&DustAccount::get()), 0);
 
+		// ensure dust account balance gte ED
+		assert_ok!(Tokens::deposit(DOT, &DustAccount::get(), Tokens::minimum_balance(DOT)));
+
 		assert_ok!(Tokens::withdraw(DOT, &ALICE, 1));
 
 		// total is lte ED, will handle dust
-		assert_eq!(Tokens::total_issuance(DOT), 1);
+		assert_eq!(Tokens::total_issuance(DOT), 3);
 		assert_eq!(Accounts::<Runtime>::contains_key(ALICE, DOT), false);
 		assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
 		assert_eq!(System::providers(&ALICE), 0);
 
 		// will not handle dust for module account
 		assert_eq!(Accounts::<Runtime>::contains_key(DustAccount::get(), DOT), true);
-		assert_eq!(Tokens::free_balance(DOT, &DustAccount::get()), 1);
+		assert_eq!(Tokens::free_balance(DOT, &DustAccount::get()), 3);
 		assert_eq!(System::providers(&DustAccount::get()), 1);
 
 		System::assert_last_event(Event::Tokens(crate::Event::DustLost(DOT, ALICE, 1)));
-- 
GitLab