From 6f593688c1d7d20930eef3516bbd12985ecae5e7 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 8 Jan 2025 11:01:03 +0800 Subject: [PATCH] add ExistenceRequirement --- currencies/src/lib.rs | 30 +++++++++++++++++++++++------ tokens/src/lib.rs | 14 ++++++++++---- traits/src/currency.rs | 10 ++++++++-- xcm-support/src/currency_adapter.rs | 15 +++++++++++---- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/currencies/src/lib.rs b/currencies/src/lib.rs index 10a109158..5b9b34bb3 100644 --- a/currencies/src/lib.rs +++ b/currencies/src/lib.rs @@ -134,7 +134,13 @@ pub mod module { ) -> DispatchResult { let from = ensure_signed(origin)?; let to = T::Lookup::lookup(dest)?; - >::transfer(currency_id, &from, &to, amount) + >::transfer( + currency_id, + &from, + &to, + amount, + ExistenceRequirement::AllowDeath, + ) } /// Transfer some native currency to another account. @@ -220,6 +226,7 @@ impl MultiCurrency for Pallet { from: &T::AccountId, to: &T::AccountId, amount: Self::Balance, + existence_requirement: ExistenceRequirement, ) -> DispatchResult { if amount.is_zero() || from == to { return Ok(()); @@ -227,7 +234,7 @@ impl MultiCurrency for Pallet { 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) } } @@ -242,14 +249,19 @@ impl MultiCurrency for Pallet { } } - 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) } } @@ -476,7 +488,13 @@ where } fn transfer(from: &T::AccountId, to: &T::AccountId, amount: Self::Balance) -> DispatchResult { - as MultiCurrency>::transfer(GetCurrencyId::get(), from, to, amount) + as MultiCurrency>::transfer( + GetCurrencyId::get(), + from, + to, + amount, + ExistenceRequirement::AllowDeath, + ) } fn deposit(who: &T::AccountId, amount: Self::Balance) -> DispatchResult { @@ -484,7 +502,7 @@ where } fn withdraw(who: &T::AccountId, amount: Self::Balance) -> DispatchResult { - >::withdraw(GetCurrencyId::get(), who, amount) + >::withdraw(GetCurrencyId::get(), who, amount, ExistenceRequirement::AllowDeath) } fn can_slash(who: &T::AccountId, amount: Self::Balance) -> bool { diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index 314066671..315effc5d 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -1161,9 +1161,10 @@ impl MultiCurrency for Pallet { 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 { @@ -1172,9 +1173,14 @@ impl MultiCurrency for Pallet { 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`. @@ -1269,7 +1275,7 @@ impl MultiCurrencyExtended for Pallet { 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(|_| ()) } } } diff --git a/traits/src/currency.rs b/traits/src/currency.rs index e1772d27c..3cfe7a60e 100644 --- a/traits/src/currency.rs +++ b/traits/src/currency.rs @@ -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, @@ -56,6 +56,7 @@ pub trait MultiCurrency { from: &AccountId, to: &AccountId, amount: Self::Balance, + existence_requirement: ExistenceRequirement, ) -> DispatchResult; /// Add `amount` to the balance of `who` under `currency_id` and increase @@ -64,7 +65,12 @@ pub trait MultiCurrency { /// 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 diff --git a/xcm-support/src/currency_adapter.rs b/xcm-support/src/currency_adapter.rs index ad78676a7..01c95ef6a 100644 --- a/xcm-support/src/currency_adapter.rs +++ b/xcm-support/src/currency_adapter.rs @@ -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}, @@ -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()) @@ -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()) }