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
+11 -3
View File
@@ -103,16 +103,24 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Remove
}
/// Returns `true` when the balance of `account` can be increased by `amount`.
///
/// - `id`: The id of the asset that should be increased.
/// - `who`: The account of which the balance should be increased.
/// - `amount`: The amount by which the balance should be increased.
/// - `increase_supply`: Will the supply of the asset be increased by `amount` at the same time
/// as crediting the `account`.
pub(super) fn can_increase(
id: T::AssetId,
who: &T::AccountId,
amount: T::Balance,
increase_supply: bool,
) -> DepositConsequence {
let details = match Asset::<T, I>::get(id) {
Some(details) => details,
None => return DepositConsequence::UnknownAsset,
};
if details.supply.checked_add(&amount).is_none() {
if increase_supply && details.supply.checked_add(&amount).is_none() {
return DepositConsequence::Overflow
}
if let Some(balance) = Self::maybe_balance(id, who) {
@@ -283,7 +291,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
(true, Some(dust)) => (amount, Some(dust)),
_ => (debit, None),
};
Self::can_increase(id, &dest, credit).into_result()?;
Self::can_increase(id, &dest, credit, false).into_result()?;
Ok((credit, maybe_burn))
}
@@ -379,7 +387,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
return Ok(())
}
Self::can_increase(id, beneficiary, amount).into_result()?;
Self::can_increase(id, beneficiary, amount, true).into_result()?;
Asset::<T, I>::try_mutate(id, |maybe_details| -> DispatchResult {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;