diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index b37052a75c08d28c87921643fd3617fe88554b15..65ff4bd3b905fe9524408bf772e99f0ff6042b02 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -597,7 +597,7 @@ impl<T: Config> Pallet<T> { Self::ensure_can_withdraw(currency_id, from, amount)?; let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(from); + let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(from); // if from_account does not allow death and non_zero total is below existential // deposit, would return an error. ensure!(allow_death || from_account.total() >= ed, Error::<T>::KeepAlive); @@ -1285,7 +1285,7 @@ where let ed = T::ExistentialDeposits::get(¤cy_id); let allow_death = liveness == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(who); + let allow_death = allow_death && frame_system::Pallet::<T>::can_dec_provider(who); ensure!(allow_death || account.total() >= ed, Error::<T>::KeepAlive); Ok(()) diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index e021a052223a17dc521e3c040a9542d57d086a39..16590f187ab42da36363d03da201c038227eb914 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -340,6 +340,26 @@ fn transfer_all_should_work() { }); } +#[test] +fn transfer_failed_when_allow_death_but_cannot_dec_provider() { + ExtBuilder::default() + .one_hundred_for_alice_n_bob() + .build() + .execute_with(|| { + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(System::inc_consumers(&ALICE)); + assert_eq!(System::can_dec_provider(&ALICE), false); + assert_noop!( + Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100), + Error::<Runtime>::KeepAlive + ); + + assert_ok!(Tokens::deposit(BTC, &ALICE, 100)); + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(Tokens::transfer(Some(ALICE).into(), BOB, DOT, 100)); + }); +} + #[test] fn deposit_should_work() { ExtBuilder::default() @@ -593,6 +613,34 @@ fn currency_adapter_ensure_currency_adapter_should_work() { }); } +#[test] +fn currency_adapter_withdraw_failed_when_allow_death_but_cannot_dec_provider() { + ExtBuilder::default() + .one_hundred_for_alice_n_bob() + .build() + .execute_with(|| { + assert_eq!(System::can_dec_provider(&ALICE), true); + assert_ok!(System::inc_consumers(&ALICE)); + assert_eq!(System::can_dec_provider(&ALICE), false); + assert!(TreasuryCurrencyAdapter::withdraw( + &ALICE, + 100, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath + ) + .is_err()); + assert_ok!(Tokens::deposit(BTC, &ALICE, 100)); + assert_eq!(System::can_dec_provider(&ALICE), true); + assert!(TreasuryCurrencyAdapter::withdraw( + &ALICE, + 100, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath + ) + .is_ok()); + }); +} + #[test] fn currency_adapter_burn_must_work() { ExtBuilder::default()