pallet-asset: Fix transfer of a large amount of an asset (#11241)

* pallet-asset: Fix transfer of a large amount of an asset

Before this pr transferring a large amount of an asset would check that transferring the asset would
not overflow the supply of the asset. However, it doesn't make sense to check for asset supply
overflow when we just transfer from one account to another account and don't increase the supply in
any way. It also required to extend the `can_deposit` method of `fungible` and `fungibles` with a
`mint` parameter. If this parameter is set to `true`, it means we want to mint the amount of an
asset before transferring it into an account. For `can_withdraw` we don't need to add an extra
parameter, because withdrawing should never be able to underflow the supply. If that would happen,
it would mean that somewhere the supply wasn't increased while increasing the balance of an account.

* Update frame/assets/src/functions.rs

* Update frame/assets/src/functions.rs

* Update frame/assets/src/functions.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* FMT

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Bastian Köcher
2022-04-25 20:12:06 +02:00
committed by GitHub
parent 914db49d04
commit 2541efdbcc
6 changed files with 47 additions and 13 deletions
+5 -4
View File
@@ -810,12 +810,13 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
_who: &T::AccountId,
amount: T::Balance,
account: &AccountData<T::Balance>,
mint: bool,
) -> DepositConsequence {
if amount.is_zero() {
return DepositConsequence::Success
}
if TotalIssuance::<T, I>::get().checked_add(&amount).is_none() {
if mint && TotalIssuance::<T, I>::get().checked_add(&amount).is_none() {
return DepositConsequence::Overflow
}
@@ -1093,8 +1094,8 @@ impl<T: Config<I>, I: 'static> fungible::Inspect<T::AccountId> for Pallet<T, I>
liquid.saturating_sub(must_remain_to_exist)
}
}
fn can_deposit(who: &T::AccountId, amount: Self::Balance) -> DepositConsequence {
Self::deposit_consequence(who, amount, &Self::account(who))
fn can_deposit(who: &T::AccountId, amount: Self::Balance, mint: bool) -> DepositConsequence {
Self::deposit_consequence(who, amount, &Self::account(who), mint)
}
fn can_withdraw(
who: &T::AccountId,
@@ -1110,7 +1111,7 @@ impl<T: Config<I>, I: 'static> fungible::Mutate<T::AccountId> for Pallet<T, I> {
return Ok(())
}
Self::try_mutate_account(who, |account, _is_new| -> DispatchResult {
Self::deposit_consequence(who, amount, &account).into_result()?;
Self::deposit_consequence(who, amount, &account, true).into_result()?;
account.free += amount;
Ok(())
})?;