Skip to content

Commit

Permalink
add ExistenceRequirement
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Jan 8, 2025
1 parent 7d60899 commit 6f59368
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
30 changes: 24 additions & 6 deletions currencies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ pub mod module {
) -> DispatchResult {
let from = ensure_signed(origin)?;
let to = T::Lookup::lookup(dest)?;
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, amount)
<Self as MultiCurrency<T::AccountId>>::transfer(
currency_id,
&from,
&to,
amount,
ExistenceRequirement::AllowDeath,
)
}

/// Transfer some native currency to another account.
Expand Down Expand Up @@ -220,14 +226,15 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
from: &T::AccountId,
to: &T::AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult {
if amount.is_zero() || from == to {
return Ok(());
}
if currency_id == T::GetNativeCurrencyId::get() {
T::NativeCurrency::transfer(from, to, amount)
} else {
T::MultiCurrency::transfer(currency_id, from, to, amount)
T::MultiCurrency::transfer(currency_id, from, to, amount, existence_requirement)
}
}

Expand All @@ -242,14 +249,19 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
}
}

fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
fn withdraw(
currency_id: Self::CurrencyId,
who: &T::AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult {
if amount.is_zero() {
return Ok(());
}
if currency_id == T::GetNativeCurrencyId::get() {
T::NativeCurrency::withdraw(who, amount)
} else {
T::MultiCurrency::withdraw(currency_id, who, amount)
T::MultiCurrency::withdraw(currency_id, who, amount, existence_requirement)
}
}

Expand Down Expand Up @@ -476,15 +488,21 @@ where
}

fn transfer(from: &T::AccountId, to: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T> as MultiCurrency<T::AccountId>>::transfer(GetCurrencyId::get(), from, to, amount)
<Pallet<T> as MultiCurrency<T::AccountId>>::transfer(
GetCurrencyId::get(),
from,
to,
amount,
ExistenceRequirement::AllowDeath,
)
}

fn deposit(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T>>::deposit(GetCurrencyId::get(), who, amount)
}

fn withdraw(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T>>::withdraw(GetCurrencyId::get(), who, amount)
<Pallet<T>>::withdraw(GetCurrencyId::get(), who, amount, ExistenceRequirement::AllowDeath)
}

fn can_slash(who: &T::AccountId, amount: Self::Balance) -> bool {
Expand Down
14 changes: 10 additions & 4 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,10 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
from: &T::AccountId,
to: &T::AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult {
// allow death
Self::do_transfer(currency_id, from, to, amount, ExistenceRequirement::AllowDeath)
Self::do_transfer(currency_id, from, to, amount, existence_requirement)
}

fn deposit(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
Expand All @@ -1172,9 +1173,14 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
Ok(())
}

fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
fn withdraw(
currency_id: Self::CurrencyId,
who: &T::AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult {
// allow death
Self::do_withdraw(currency_id, who, amount, ExistenceRequirement::AllowDeath, true)
Self::do_withdraw(currency_id, who, amount, existence_requirement, true)
}

// Check if `value` amount of free balance can be slashed from `who`.
Expand Down Expand Up @@ -1269,7 +1275,7 @@ impl<T: Config> MultiCurrencyExtended<T::AccountId> for Pallet<T> {
if by_amount.is_positive() {
Self::deposit(currency_id, who, by_balance)
} else {
Self::withdraw(currency_id, who, by_balance).map(|_| ())
Self::withdraw(currency_id, who, by_balance, ExistenceRequirement::AllowDeath).map(|_| ())
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions traits/src/currency.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{arithmetic, Happened};
use frame_support::traits::tokens::Balance;
use frame_support::traits::{tokens::Balance, ExistenceRequirement};
pub use frame_support::{
traits::{BalanceStatus, DefensiveSaturating, LockIdentifier},
transactional,
Expand Down Expand Up @@ -56,6 +56,7 @@ pub trait MultiCurrency<AccountId> {
from: &AccountId,
to: &AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult;

/// Add `amount` to the balance of `who` under `currency_id` and increase
Expand All @@ -64,7 +65,12 @@ pub trait MultiCurrency<AccountId> {

/// Remove `amount` from the balance of `who` under `currency_id` and reduce
/// total issuance.
fn withdraw(currency_id: Self::CurrencyId, who: &AccountId, amount: Self::Balance) -> DispatchResult;
fn withdraw(
currency_id: Self::CurrencyId,
who: &AccountId,
amount: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> DispatchResult;

/// Same result as `slash(currency_id, who, value)` (but without the
/// side-effects) assuming there are no balance changes in the meantime and
Expand Down
15 changes: 11 additions & 4 deletions xcm-support/src/currency_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use frame_support::traits::Get;
use frame_support::traits::{ExistenceRequirement, Get};
use parity_scale_codec::FullCodec;
use sp_runtime::{
traits::{Convert, MaybeSerializeDeserialize, SaturatedConversion},
Expand Down Expand Up @@ -173,7 +173,8 @@ impl<
let amount: MultiCurrency::Balance = Match::matches_fungible(asset)
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
.saturated_into();
MultiCurrency::withdraw(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))
MultiCurrency::withdraw(currency_id, &who, amount, ExistenceRequirement::AllowDeath)
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))
})?;

Ok(asset.clone().into())
Expand All @@ -194,8 +195,14 @@ impl<
let amount: MultiCurrency::Balance = Match::matches_fungible(asset)
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
.saturated_into();
MultiCurrency::transfer(currency_id, &from_account, &to_account, amount)
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
MultiCurrency::transfer(
currency_id,
&from_account,
&to_account,
amount,
ExistenceRequirement::AllowDeath,
)
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;

Ok(asset.clone().into())
}
Expand Down

0 comments on commit 6f59368

Please sign in to comment.