Skip to content

Commit 07ad5c4

Browse files
committed
add ExistenceRequirement
1 parent 286c731 commit 07ad5c4

File tree

4 files changed

+90
-29
lines changed

4 files changed

+90
-29
lines changed

currencies/src/lib.rs

Lines changed: 54 additions & 17 deletions
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.
@@ -150,7 +156,7 @@ pub mod module {
150156
) -> DispatchResult {
151157
let from = ensure_signed(origin)?;
152158
let to = T::Lookup::lookup(dest)?;
153-
T::NativeCurrency::transfer(&from, &to, amount)
159+
T::NativeCurrency::transfer(&from, &to, amount, ExistenceRequirement::AllowDeath)
154160
}
155161

156162
/// update amount of account `who` under `currency_id`.
@@ -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() {
228-
T::NativeCurrency::transfer(from, to, amount)
235+
T::NativeCurrency::transfer(from, to, amount, existence_requirement)
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() {
250-
T::NativeCurrency::withdraw(who, amount)
262+
T::NativeCurrency::withdraw(who, amount, existence_requirement)
251263
} else {
252-
T::MultiCurrency::withdraw(currency_id, who, amount)
264+
T::MultiCurrency::withdraw(currency_id, who, amount, existence_requirement)
253265
}
254266
}
255267

@@ -475,16 +487,31 @@ where
475487
<Pallet<T>>::ensure_can_withdraw(GetCurrencyId::get(), who, amount)
476488
}
477489

478-
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)
490+
fn transfer(
491+
from: &T::AccountId,
492+
to: &T::AccountId,
493+
amount: Self::Balance,
494+
existence_requirement: ExistenceRequirement,
495+
) -> DispatchResult {
496+
<Pallet<T> as MultiCurrency<T::AccountId>>::transfer(
497+
GetCurrencyId::get(),
498+
from,
499+
to,
500+
amount,
501+
existence_requirement,
502+
)
480503
}
481504

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

486-
fn withdraw(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
487-
<Pallet<T>>::withdraw(GetCurrencyId::get(), who, amount)
509+
fn withdraw(
510+
who: &T::AccountId,
511+
amount: Self::Balance,
512+
existence_requirement: ExistenceRequirement,
513+
) -> DispatchResult {
514+
<Pallet<T>>::withdraw(GetCurrencyId::get(), who, amount, existence_requirement)
488515
}
489516

490517
fn can_slash(who: &T::AccountId, amount: Self::Balance) -> bool {
@@ -653,8 +680,13 @@ where
653680
Currency::ensure_can_withdraw(who, amount, WithdrawReasons::all(), new_balance)
654681
}
655682

656-
fn transfer(from: &AccountId, to: &AccountId, amount: Self::Balance) -> DispatchResult {
657-
Currency::transfer(from, to, amount, ExistenceRequirement::AllowDeath)
683+
fn transfer(
684+
from: &AccountId,
685+
to: &AccountId,
686+
amount: Self::Balance,
687+
existence_requirement: ExistenceRequirement,
688+
) -> DispatchResult {
689+
Currency::transfer(from, to, amount, existence_requirement)
658690
}
659691

660692
fn deposit(who: &AccountId, amount: Self::Balance) -> DispatchResult {
@@ -666,8 +698,8 @@ where
666698
Ok(())
667699
}
668700

669-
fn withdraw(who: &AccountId, amount: Self::Balance) -> DispatchResult {
670-
Currency::withdraw(who, amount, WithdrawReasons::all(), ExistenceRequirement::AllowDeath).map(|_| ())
701+
fn withdraw(who: &AccountId, amount: Self::Balance, existence_requirement: ExistenceRequirement) -> DispatchResult {
702+
Currency::withdraw(who, amount, WithdrawReasons::all(), existence_requirement).map(|_| ())
671703
}
672704

673705
fn can_slash(who: &AccountId, amount: Self::Balance) -> bool {
@@ -707,7 +739,7 @@ where
707739
if by_amount.is_positive() {
708740
Self::deposit(who, by_balance)
709741
} else {
710-
Self::withdraw(who, by_balance)
742+
Self::withdraw(who, by_balance, ExistenceRequirement::AllowDeath)
711743
}
712744
}
713745
}
@@ -817,7 +849,12 @@ impl<T: Config> TransferAll<T::AccountId> for Pallet<T> {
817849
T::MultiCurrency::transfer_all(source, dest)?;
818850

819851
// transfer all free to dest
820-
T::NativeCurrency::transfer(source, dest, T::NativeCurrency::free_balance(source))
852+
T::NativeCurrency::transfer(
853+
source,
854+
dest,
855+
T::NativeCurrency::free_balance(source),
856+
ExistenceRequirement::AllowDeath,
857+
)
821858
})
822859
}
823860
}

tokens/src/lib.rs

Lines changed: 10 additions & 4 deletions
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

Lines changed: 15 additions & 4 deletions
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
@@ -381,13 +387,18 @@ pub trait BasicCurrency<AccountId> {
381387
// Public mutables
382388

383389
/// Transfer some amount from one account to another.
384-
fn transfer(from: &AccountId, to: &AccountId, amount: Self::Balance) -> DispatchResult;
390+
fn transfer(
391+
from: &AccountId,
392+
to: &AccountId,
393+
amount: Self::Balance,
394+
existence_requirement: ExistenceRequirement,
395+
) -> DispatchResult;
385396

386397
/// Add `amount` to the balance of `who` and increase total issuance.
387398
fn deposit(who: &AccountId, amount: Self::Balance) -> DispatchResult;
388399

389400
/// Remove `amount` from the balance of `who` and reduce total issuance.
390-
fn withdraw(who: &AccountId, amount: Self::Balance) -> DispatchResult;
401+
fn withdraw(who: &AccountId, amount: Self::Balance, existence_requirement: ExistenceRequirement) -> DispatchResult;
391402

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

xcm-support/src/currency_adapter.rs

Lines changed: 11 additions & 4 deletions
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)