Skip to content
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

ensure account of native token keeps alive after charge fee #1204

Merged
merged 1 commit into from
Jul 14, 2021
Merged
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
21 changes: 14 additions & 7 deletions modules/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,24 @@ where
charge_fee_order.dedup();

let price_impact_limit = Some(T::MaxSlippageSwapWithDEX::get());
let native_existential_deposit = <T as Config>::Currency::minimum_balance();
let total_native = <T as Config>::Currency::total_balance(who);
// add the gap amount to keep account alive and have enough fee
let fee_and_alive_gap = if total_native < native_existential_deposit {
fee.saturating_add(native_existential_deposit.saturating_sub(total_native))
} else {
fee
};

// iterator charge fee order to get enough fee
for currency_id in charge_fee_order {
if currency_id == native_currency_id {
// check native balance if is enough
let native_is_enough =
<T as Config>::Currency::free_balance(who)
.checked_sub(&fee)
.map_or(false, |new_free_balance| {
<T as Config>::Currency::ensure_can_withdraw(who, fee, reason, new_free_balance).is_ok()
});
let native_is_enough = <T as Config>::Currency::free_balance(who)
.checked_sub(&fee_and_alive_gap)
.map_or(false, |new_free_balance| {
<T as Config>::Currency::ensure_can_withdraw(who, fee, reason, new_free_balance).is_ok()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<T as Config>::Currency::ensure_can_withdraw(who, fee, reason, new_free_balance).is_ok()
<T as Config>::Currency::ensure_can_withdraw(who, fee, reason, fee_and_alive_gap).is_ok()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xlc The old code seems to be wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we removed the last parameter

});
if native_is_enough {
// native balance is enough, break iteration
break;
Expand All @@ -593,7 +600,7 @@ where
if T::DEX::swap_with_exact_target(
who,
&trading_path,
fee.unique_saturated_into(),
fee_and_alive_gap.unique_saturated_into(),
<T as Config>::MultiCurrency::free_balance(currency_id, who),
price_impact_limit,
)
Expand Down