Skip to content

Commit 8285019

Browse files
authored
Add existence requirement (#1014)
* add ExistenceRequirement * fix * update xcm-procedural:10.1.0 to work
1 parent 286c731 commit 8285019

File tree

11 files changed

+165
-45
lines changed

11 files changed

+165
-45
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
tar -zxvf cargo-tarpaulin-x86_64-unknown-linux-musl.tar.gz -C $HOME/.cargo/bin
3838
make Cargo.toml
3939
cargo update
40+
cargo update -p xcm-procedural --precise 10.1.0
4041
cargo tarpaulin --verbose --no-fail-fast --workspace --timeout 300 --out Xml
4142
- name: Upload to codecov.io
4243
uses: codecov/codecov-action@v3

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ jobs:
3434
- name: Install clippy
3535
run: rustup component add clippy
3636
- name: Update
37-
run: cargo update
37+
run: |
38+
cargo update
39+
cargo update -p xcm-procedural --precise 10.1.0
3840
- name: Run clippy
3941
run: cargo clippy -- -D warnings
4042
- name: Check for Wasm

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
}

currencies/src/tests.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ fn native_currency_should_work() {
217217
assert_eq!(NativeCurrency::free_balance(&ALICE), 50);
218218
assert_eq!(NativeCurrency::free_balance(&BOB), 150);
219219

220-
assert_ok!(NativeCurrency::transfer(&ALICE, &BOB, 10));
220+
assert_ok!(NativeCurrency::transfer(
221+
&ALICE,
222+
&BOB,
223+
10,
224+
ExistenceRequirement::AllowDeath
225+
));
221226
assert_eq!(NativeCurrency::free_balance(&ALICE), 40);
222227
assert_eq!(NativeCurrency::free_balance(&BOB), 160);
223228

@@ -251,12 +256,22 @@ fn basic_currency_adapting_pallet_balances_transfer() {
251256
.one_hundred_for_alice_n_bob()
252257
.build()
253258
.execute_with(|| {
254-
assert_ok!(AdaptedBasicCurrency::transfer(&ALICE, &BOB, 50));
259+
assert_ok!(AdaptedBasicCurrency::transfer(
260+
&ALICE,
261+
&BOB,
262+
50,
263+
ExistenceRequirement::AllowDeath
264+
));
255265
assert_eq!(PalletBalances::total_balance(&ALICE), 50);
256266
assert_eq!(PalletBalances::total_balance(&BOB), 150);
257267

258268
// creation fee
259-
assert_ok!(AdaptedBasicCurrency::transfer(&ALICE, &EVA, 10));
269+
assert_ok!(AdaptedBasicCurrency::transfer(
270+
&ALICE,
271+
&EVA,
272+
10,
273+
ExistenceRequirement::AllowDeath
274+
));
260275
assert_eq!(PalletBalances::total_balance(&ALICE), 40);
261276
assert_eq!(PalletBalances::total_balance(&EVA), 10);
262277
});
@@ -297,7 +312,11 @@ fn basic_currency_adapting_pallet_balances_withdraw() {
297312
.one_hundred_for_alice_n_bob()
298313
.build()
299314
.execute_with(|| {
300-
assert_ok!(AdaptedBasicCurrency::withdraw(&ALICE, 100));
315+
assert_ok!(AdaptedBasicCurrency::withdraw(
316+
&ALICE,
317+
100,
318+
ExistenceRequirement::AllowDeath
319+
));
301320
assert_eq!(PalletBalances::total_balance(&ALICE), 0);
302321
assert_eq!(PalletBalances::total_issuance(), 100);
303322
});
@@ -375,7 +394,11 @@ fn call_event_should_work() {
375394
}));
376395

377396
assert_ok!(<Currencies as MultiCurrency<AccountId>>::transfer(
378-
X_TOKEN_ID, &ALICE, &BOB, 10
397+
X_TOKEN_ID,
398+
&ALICE,
399+
&BOB,
400+
10,
401+
ExistenceRequirement::AllowDeath
379402
));
380403
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 40);
381404
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 160);
@@ -397,7 +420,10 @@ fn call_event_should_work() {
397420
}));
398421

399422
assert_ok!(<Currencies as MultiCurrency<AccountId>>::withdraw(
400-
X_TOKEN_ID, &ALICE, 20
423+
X_TOKEN_ID,
424+
&ALICE,
425+
20,
426+
ExistenceRequirement::AllowDeath
401427
));
402428
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 120);
403429
System::assert_last_event(RuntimeEvent::Tokens(orml_tokens::Event::Withdrawn {

payments/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ pub mod pallet {
7474
weights::WeightInfo,
7575
};
7676
use frame_support::{
77-
dispatch::DispatchResultWithPostInfo, fail, pallet_prelude::*, require_transactional,
78-
storage::bounded_btree_map::BoundedBTreeMap, traits::tokens::BalanceStatus,
77+
dispatch::DispatchResultWithPostInfo,
78+
fail,
79+
pallet_prelude::*,
80+
require_transactional,
81+
storage::bounded_btree_map::BoundedBTreeMap,
82+
traits::{tokens::BalanceStatus, ExistenceRequirement},
7983
};
8084
use frame_system::pallet_prelude::*;
8185
use orml_traits::{MultiCurrency, MultiReservableCurrency};
@@ -634,6 +638,7 @@ pub mod pallet {
634638
from, // fee is paid by payment creator
635639
&fee_recipient, // account of fee recipient
636640
fee_amount, // amount of fee
641+
ExistenceRequirement::AllowDeath,
637642
)?;
638643
}
639644
}
@@ -648,7 +653,13 @@ pub mod pallet {
648653
let amount_to_recipient = recipient_share.mul_floor(payment.amount);
649654
let amount_to_sender = payment.amount.saturating_sub(amount_to_recipient);
650655
// send share to recipient
651-
T::Asset::transfer(payment.asset, to, from, amount_to_sender)?;
656+
T::Asset::transfer(
657+
payment.asset,
658+
to,
659+
from,
660+
amount_to_sender,
661+
ExistenceRequirement::AllowDeath,
662+
)?;
652663

653664
Ok(())
654665
})?;

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
}

tokens/src/tests_events.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ fn pallet_multicurrency_deposit_events() {
2020
.balances(vec![(ALICE, DOT, 100), (BOB, DOT, 100)])
2121
.build()
2222
.execute_with(|| {
23-
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(DOT, &ALICE, &BOB, 10));
23+
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(
24+
DOT,
25+
&ALICE,
26+
&BOB,
27+
10,
28+
ExistenceRequirement::AllowDeath
29+
));
2430
System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Transfer {
2531
currency_id: DOT,
2632
from: ALICE,
@@ -35,7 +41,12 @@ fn pallet_multicurrency_deposit_events() {
3541
amount: 10,
3642
}));
3743

38-
assert_ok!(<Tokens as MultiCurrency<AccountId>>::withdraw(DOT, &ALICE, 10));
44+
assert_ok!(<Tokens as MultiCurrency<AccountId>>::withdraw(
45+
DOT,
46+
&ALICE,
47+
10,
48+
ExistenceRequirement::AllowDeath
49+
));
3950
System::assert_last_event(RuntimeEvent::Tokens(crate::Event::Withdrawn {
4051
currency_id: DOT,
4152
who: ALICE,

tokens/src/tests_multicurrency.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn multicurrency_withdraw_work() {
2828
assert!(Accounts::<Runtime>::contains_key(ALICE, DOT));
2929
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
3030
assert_eq!(Tokens::total_issuance(DOT), 100);
31-
assert_ok!(Tokens::withdraw(DOT, &ALICE, 99));
31+
assert_ok!(Tokens::withdraw(DOT, &ALICE, 99, ExistenceRequirement::AllowDeath));
3232
assert!(!Accounts::<Runtime>::contains_key(ALICE, DOT));
3333
assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
3434
assert_eq!(Tokens::total_issuance(DOT), 1);
@@ -44,7 +44,13 @@ fn multicurrency_transfer_work() {
4444
assert!(Accounts::<Runtime>::contains_key(ALICE, DOT));
4545
assert_eq!(Tokens::free_balance(DOT, &ALICE), 100);
4646
assert_eq!(Tokens::free_balance(DOT, &BOB), 100);
47-
assert_ok!(<Tokens as MultiCurrency<_>>::transfer(DOT, &ALICE, &BOB, 99));
47+
assert_ok!(<Tokens as MultiCurrency<_>>::transfer(
48+
DOT,
49+
&ALICE,
50+
&BOB,
51+
99,
52+
ExistenceRequirement::AllowDeath
53+
));
4854
assert!(!Accounts::<Runtime>::contains_key(ALICE, DOT));
4955
assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
5056
assert_eq!(Tokens::free_balance(DOT, &BOB), 199);
@@ -379,7 +385,7 @@ fn no_op_if_amount_is_zero() {
379385
assert_ok!(Tokens::transfer(Some(ALICE).into(), BOB, DOT, 0));
380386
assert_ok!(Tokens::transfer(Some(ALICE).into(), ALICE, DOT, 0));
381387
assert_ok!(Tokens::deposit(DOT, &ALICE, 0));
382-
assert_ok!(Tokens::withdraw(DOT, &ALICE, 0));
388+
assert_ok!(Tokens::withdraw(DOT, &ALICE, 0, ExistenceRequirement::AllowDeath));
383389
assert_eq!(Tokens::slash(DOT, &ALICE, 0), 0);
384390
assert_eq!(Tokens::slash(DOT, &ALICE, 1), 1);
385391
assert_ok!(Tokens::update_balance(DOT, &ALICE, 0));

0 commit comments

Comments
 (0)