Skip to content

Remove module account ed #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)?;

Expand Down
7 changes: 6 additions & 1 deletion tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 5 additions & 13 deletions tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)));
Expand Down