Skip to content

Commit 6f59368

Browse files
committed
add ExistenceRequirement
1 parent 7d60899 commit 6f59368

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

currencies/src/lib.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ pub mod module {
134134
) -> DispatchResult {
135135
let from = ensure_signed(origin)?;
136136
let to = T::Lookup::lookup(dest)?;
137-
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, amount)
137+
<Self as MultiCurrency<T::AccountId>>::transfer(
138+
currency_id,
139+
&from,
140+
&to,
141+
amount,
142+
ExistenceRequirement::AllowDeath,
143+
)
138144
}
139145

140146
/// Transfer some native currency to another account.
@@ -220,14 +226,15 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
220226
from: &T::AccountId,
221227
to: &T::AccountId,
222228
amount: Self::Balance,
229+
existence_requirement: ExistenceRequirement,
223230
) -> DispatchResult {
224231
if amount.is_zero() || from == to {
225232
return Ok(());
226233
}
227234
if currency_id == T::GetNativeCurrencyId::get() {
228235
T::NativeCurrency::transfer(from, to, amount)
229236
} else {
230-
T::MultiCurrency::transfer(currency_id, from, to, amount)
237+
T::MultiCurrency::transfer(currency_id, from, to, amount, existence_requirement)
231238
}
232239
}
233240

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

245-
fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
252+
fn withdraw(
253+
currency_id: Self::CurrencyId,
254+
who: &T::AccountId,
255+
amount: Self::Balance,
256+
existence_requirement: ExistenceRequirement,
257+
) -> DispatchResult {
246258
if amount.is_zero() {
247259
return Ok(());
248260
}
249261
if currency_id == T::GetNativeCurrencyId::get() {
250262
T::NativeCurrency::withdraw(who, amount)
251263
} else {
252-
T::MultiCurrency::withdraw(currency_id, who, amount)
264+
T::MultiCurrency::withdraw(currency_id, who, amount, existence_requirement)
253265
}
254266
}
255267

@@ -476,15 +488,21 @@ where
476488
}
477489

478490
fn transfer(from: &T::AccountId, to: &T::AccountId, amount: Self::Balance) -> DispatchResult {
479-
<Pallet<T> as MultiCurrency<T::AccountId>>::transfer(GetCurrencyId::get(), from, to, amount)
491+
<Pallet<T> as MultiCurrency<T::AccountId>>::transfer(
492+
GetCurrencyId::get(),
493+
from,
494+
to,
495+
amount,
496+
ExistenceRequirement::AllowDeath,
497+
)
480498
}
481499

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

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

490508
fn can_slash(who: &T::AccountId, amount: Self::Balance) -> bool {

tokens/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,10 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
11611161
from: &T::AccountId,
11621162
to: &T::AccountId,
11631163
amount: Self::Balance,
1164+
existence_requirement: ExistenceRequirement,
11641165
) -> DispatchResult {
11651166
// allow death
1166-
Self::do_transfer(currency_id, from, to, amount, ExistenceRequirement::AllowDeath)
1167+
Self::do_transfer(currency_id, from, to, amount, existence_requirement)
11671168
}
11681169

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

1175-
fn withdraw(currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
1176+
fn withdraw(
1177+
currency_id: Self::CurrencyId,
1178+
who: &T::AccountId,
1179+
amount: Self::Balance,
1180+
existence_requirement: ExistenceRequirement,
1181+
) -> DispatchResult {
11761182
// allow death
1177-
Self::do_withdraw(currency_id, who, amount, ExistenceRequirement::AllowDeath, true)
1183+
Self::do_withdraw(currency_id, who, amount, existence_requirement, true)
11781184
}
11791185

11801186
// Check if `value` amount of free balance can be slashed from `who`.
@@ -1269,7 +1275,7 @@ impl<T: Config> MultiCurrencyExtended<T::AccountId> for Pallet<T> {
12691275
if by_amount.is_positive() {
12701276
Self::deposit(currency_id, who, by_balance)
12711277
} else {
1272-
Self::withdraw(currency_id, who, by_balance).map(|_| ())
1278+
Self::withdraw(currency_id, who, by_balance, ExistenceRequirement::AllowDeath).map(|_| ())
12731279
}
12741280
}
12751281
}

traits/src/currency.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{arithmetic, Happened};
2-
use frame_support::traits::tokens::Balance;
2+
use frame_support::traits::{tokens::Balance, ExistenceRequirement};
33
pub use frame_support::{
44
traits::{BalanceStatus, DefensiveSaturating, LockIdentifier},
55
transactional,
@@ -56,6 +56,7 @@ pub trait MultiCurrency<AccountId> {
5656
from: &AccountId,
5757
to: &AccountId,
5858
amount: Self::Balance,
59+
existence_requirement: ExistenceRequirement,
5960
) -> DispatchResult;
6061

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

6566
/// Remove `amount` from the balance of `who` under `currency_id` and reduce
6667
/// total issuance.
67-
fn withdraw(currency_id: Self::CurrencyId, who: &AccountId, amount: Self::Balance) -> DispatchResult;
68+
fn withdraw(
69+
currency_id: Self::CurrencyId,
70+
who: &AccountId,
71+
amount: Self::Balance,
72+
existence_requirement: ExistenceRequirement,
73+
) -> DispatchResult;
6874

6975
/// Same result as `slash(currency_id, who, value)` (but without the
7076
/// side-effects) assuming there are no balance changes in the meantime and

xcm-support/src/currency_adapter.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use frame_support::traits::Get;
1+
use frame_support::traits::{ExistenceRequirement, Get};
22
use parity_scale_codec::FullCodec;
33
use sp_runtime::{
44
traits::{Convert, MaybeSerializeDeserialize, SaturatedConversion},
@@ -173,7 +173,8 @@ impl<
173173
let amount: MultiCurrency::Balance = Match::matches_fungible(asset)
174174
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
175175
.saturated_into();
176-
MultiCurrency::withdraw(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))
176+
MultiCurrency::withdraw(currency_id, &who, amount, ExistenceRequirement::AllowDeath)
177+
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))
177178
})?;
178179

179180
Ok(asset.clone().into())
@@ -194,8 +195,14 @@ impl<
194195
let amount: MultiCurrency::Balance = Match::matches_fungible(asset)
195196
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
196197
.saturated_into();
197-
MultiCurrency::transfer(currency_id, &from_account, &to_account, amount)
198-
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
198+
MultiCurrency::transfer(
199+
currency_id,
200+
&from_account,
201+
&to_account,
202+
amount,
203+
ExistenceRequirement::AllowDeath,
204+
)
205+
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
199206

200207
Ok(asset.clone().into())
201208
}

0 commit comments

Comments
 (0)