Skip to content

Commit 868b0fd

Browse files
authored
fix check when allow death (#559)
1 parent 1095b9c commit 868b0fd

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

tokens/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<T: Config> Pallet<T> {
597597
Self::ensure_can_withdraw(currency_id, from, amount)?;
598598

599599
let allow_death = existence_requirement == ExistenceRequirement::AllowDeath;
600-
let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(from);
600+
let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(from);
601601
// if from_account does not allow death and non_zero total is below existential
602602
// deposit, would return an error.
603603
ensure!(allow_death || from_account.total() >= ed, Error::<T>::KeepAlive);
@@ -1285,7 +1285,7 @@ where
12851285

12861286
let ed = T::ExistentialDeposits::get(&currency_id);
12871287
let allow_death = liveness == ExistenceRequirement::AllowDeath;
1288-
let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(who);
1288+
let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(who);
12891289
ensure!(allow_death || account.total() >= ed, Error::<T>::KeepAlive);
12901290

12911291
Ok(())

tokens/src/tests.rs

+48
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,26 @@ fn transfer_all_should_work() {
340340
});
341341
}
342342

343+
#[test]
344+
fn transfer_failed_when_allow_death_but_cannot_dec_provider() {
345+
ExtBuilder::default()
346+
.one_hundred_for_alice_n_bob()
347+
.build()
348+
.execute_with(|| {
349+
assert_eq!(System::can_dec_provider(&ALICE), true);
350+
assert_ok!(System::inc_consumers(&ALICE));
351+
assert_eq!(System::can_dec_provider(&ALICE), false);
352+
assert_noop!(
353+
Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100),
354+
Error::<Runtime>::KeepAlive
355+
);
356+
357+
assert_ok!(Tokens::deposit(BTC, &ALICE, 100));
358+
assert_eq!(System::can_dec_provider(&ALICE), true);
359+
assert_ok!(Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100));
360+
});
361+
}
362+
343363
#[test]
344364
fn deposit_should_work() {
345365
ExtBuilder::default()
@@ -593,6 +613,34 @@ fn currency_adapter_ensure_currency_adapter_should_work() {
593613
});
594614
}
595615

616+
#[test]
617+
fn currency_adapter_withdraw_failed_when_allow_death_but_cannot_dec_provider() {
618+
ExtBuilder::default()
619+
.one_hundred_for_alice_n_bob()
620+
.build()
621+
.execute_with(|| {
622+
assert_eq!(System::can_dec_provider(&ALICE), true);
623+
assert_ok!(System::inc_consumers(&ALICE));
624+
assert_eq!(System::can_dec_provider(&ALICE), false);
625+
assert!(TreasuryCurrencyAdapter::withdraw(
626+
&ALICE,
627+
100,
628+
WithdrawReasons::TRANSFER,
629+
ExistenceRequirement::AllowDeath
630+
)
631+
.is_err());
632+
assert_ok!(Tokens::deposit(BTC, &ALICE, 100));
633+
assert_eq!(System::can_dec_provider(&ALICE), true);
634+
assert!(TreasuryCurrencyAdapter::withdraw(
635+
&ALICE,
636+
100,
637+
WithdrawReasons::TRANSFER,
638+
ExistenceRequirement::AllowDeath
639+
)
640+
.is_ok());
641+
});
642+
}
643+
596644
#[test]
597645
fn currency_adapter_burn_must_work() {
598646
ExtBuilder::default()

0 commit comments

Comments
 (0)