Skip to content
Snippets Groups Projects
Unverified Commit 2a874fc7 authored by zjb0807's avatar zjb0807 Committed by GitHub
Browse files

fix module account ed (#549)

parent 3d64077e
No related branches found
No related tags found
No related merge requests found
......@@ -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)?;
......
......@@ -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;
......
......@@ -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)));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment