Skip to content

Check to account ExistentialDeposit #522

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 2 commits into from
Jun 17, 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
34 changes: 25 additions & 9 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ pub mod module {
MaxLocksExceeded,
/// Transfer/payment would kill account
KeepAlive,
/// Value too low to create account due to existential deposit
ExistentialDeposit,
}

#[pallet::event]
Expand Down Expand Up @@ -570,6 +572,8 @@ impl<T: Config> Pallet<T> {
/// Transfer some free balance from `from` to `to`.
/// Is a no-op if value to be transferred is zero or the `from` is the
/// same as `to`.
/// Ensure from_account allow death or new balance above existential
/// deposit. Ensure to_account new balance above existential deposit.
pub(crate) fn do_transfer(
currency_id: T::CurrencyId,
from: &T::AccountId,
Expand All @@ -581,19 +585,28 @@ impl<T: Config> Pallet<T> {
return Ok(());
}

Pallet::<T>::try_mutate_account(to, currency_id, |to_account, _is_new| -> DispatchResult {
Pallet::<T>::try_mutate_account(from, currency_id, |from_account, _is_new| -> DispatchResult {
Pallet::<T>::try_mutate_account(to, currency_id, |to_account, _existed| -> DispatchResult {
Pallet::<T>::try_mutate_account(from, currency_id, |from_account, _existed| -> DispatchResult {
from_account.free = from_account
.free
.checked_sub(&amount)
.ok_or(Error::<T>::BalanceTooLow)?;
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
);

Self::ensure_can_withdraw(currency_id, from, amount)?;

let ed = T::ExistentialDeposits::get(&currency_id);
let allow_death = existence_requirement == ExistenceRequirement::AllowDeath;
let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(from);
// if from_account does not allow death and non_zero total is below existential
// deposit, would return an error.
ensure!(allow_death || from_account.total() >= ed, Error::<T>::KeepAlive);

Ok(())
Expand Down Expand Up @@ -979,7 +992,7 @@ impl<T: Config> fungibles::Mutate<T::AccountId> for Pallet<T> {
if amount.is_zero() {
return Ok(());
}
Pallet::<T>::try_mutate_account(who, asset_id, |account, _is_new| -> DispatchResult {
Pallet::<T>::try_mutate_account(who, asset_id, |account, _existed| -> DispatchResult {
Pallet::<T>::deposit_consequence(who, asset_id, amount, &account).into_result()?;
// deposit_consequence already did overflow checking
account.free += amount;
Expand All @@ -998,14 +1011,17 @@ impl<T: Config> fungibles::Mutate<T::AccountId> for Pallet<T> {
if amount.is_zero() {
return Ok(Self::Balance::zero());
}
let actual =
Pallet::<T>::try_mutate_account(who, asset_id, |account, _is_new| -> Result<T::Balance, DispatchError> {
let actual = Pallet::<T>::try_mutate_account(
who,
asset_id,
|account, _existed| -> Result<T::Balance, DispatchError> {
let extra = Pallet::<T>::withdraw_consequence(who, asset_id, amount, &account).into_result()?;
// withdraw_consequence already did underflow checking
let actual = amount + extra;
account.free -= actual;
Ok(actual)
})?;
},
)?;
// withdraw_consequence already did underflow checking
<TotalIssuance<T>>::mutate(asset_id, |t| *t -= actual);
Ok(actual)
Expand Down Expand Up @@ -1090,7 +1106,7 @@ impl<T: Config> fungibles::MutateHold<T::AccountId> for Pallet<T> {
return Ok(amount);
}
// Done on a best-effort basis.
Pallet::<T>::try_mutate_account(who, asset_id, |a, _| {
Pallet::<T>::try_mutate_account(who, asset_id, |a, _existed| {
let new_free = a.free.saturating_add(amount.min(a.reserved));
let actual = new_free - a.free;
// Guaranteed to be <= amount and <= a.reserved
Expand Down Expand Up @@ -1248,7 +1264,7 @@ where
}

let currency_id = GetCurrencyId::get();
Pallet::<T>::try_mutate_account(who, currency_id, |account, _is_new| -> DispatchResult {
Pallet::<T>::try_mutate_account(who, currency_id, |account, _existed| -> DispatchResult {
account.free = account.free.checked_sub(&value).ok_or(Error::<T>::BalanceTooLow)?;

Pallet::<T>::ensure_can_withdraw(currency_id, who, value)?;
Expand Down
3 changes: 2 additions & 1 deletion tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub const BTC: CurrencyId = 2;
pub const ETH: CurrencyId = 3;
pub const ALICE: AccountId = AccountId32::new([0u8; 32]);
pub const BOB: AccountId = AccountId32::new([1u8; 32]);
pub const TREASURY_ACCOUNT: AccountId = AccountId32::new([2u8; 32]);
pub const CHARLIE: AccountId = AccountId32::new([2u8; 32]);
pub const TREASURY_ACCOUNT: AccountId = AccountId32::new([3u8; 32]);
pub const ID_1: LockIdentifier = *b"1 ";
pub const ID_2: LockIdentifier = *b"2 ";
pub const ID_3: LockIdentifier = *b"3 ";
Expand Down
15 changes: 10 additions & 5 deletions tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ fn transfer_should_work() {
Tokens::transfer(Some(ALICE).into(), BOB, DOT, 60),
Error::<Runtime>::BalanceTooLow,
);
assert_noop!(
Tokens::transfer(Some(ALICE).into(), CHARLIE, DOT, 1),
Error::<Runtime>::ExistentialDeposit,
);
assert_ok!(Tokens::transfer(Some(ALICE).into(), CHARLIE, DOT, 2));
});
}

Expand Down Expand Up @@ -708,7 +713,7 @@ fn currency_adapter_partial_locking_should_work() {
assert_ok!(TreasuryCurrencyAdapter::transfer(
&TREASURY_ACCOUNT,
&ALICE,
1,
2,
ExistenceRequirement::AllowDeath
));
});
Expand All @@ -725,7 +730,7 @@ fn currency_adapter_lock_removal_should_work() {
assert_ok!(TreasuryCurrencyAdapter::transfer(
&TREASURY_ACCOUNT,
&ALICE,
1,
2,
ExistenceRequirement::AllowDeath
));
});
Expand All @@ -742,7 +747,7 @@ fn currency_adapter_lock_replacement_should_work() {
assert_ok!(TreasuryCurrencyAdapter::transfer(
&TREASURY_ACCOUNT,
&ALICE,
1,
2,
ExistenceRequirement::AllowDeath
));
});
Expand All @@ -759,7 +764,7 @@ fn currency_adapter_double_locking_should_work() {
assert_ok!(TreasuryCurrencyAdapter::transfer(
&TREASURY_ACCOUNT,
&ALICE,
1,
2,
ExistenceRequirement::AllowDeath
));
});
Expand All @@ -775,7 +780,7 @@ fn currency_adapter_combination_locking_should_work() {
TreasuryCurrencyAdapter::set_lock(ID_1, &TREASURY_ACCOUNT, u64::max_value(), WithdrawReasons::empty());
TreasuryCurrencyAdapter::set_lock(ID_2, &TREASURY_ACCOUNT, 0, WithdrawReasons::all());
assert_noop!(
TreasuryCurrencyAdapter::transfer(&TREASURY_ACCOUNT, &ALICE, 1, ExistenceRequirement::AllowDeath),
TreasuryCurrencyAdapter::transfer(&TREASURY_ACCOUNT, &ALICE, 2, ExistenceRequirement::AllowDeath),
Error::<Runtime>::LiquidityRestrictions
);
});
Expand Down