Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error for Currency::transfer #750

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallets/reserve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub mod pallet {
pub fn spend(origin: OriginFor<T>, to: T::AccountId, amount: BalanceOf<T, I>) -> DispatchResultWithPostInfo {
T::ExternalOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;

let _ = T::Currency::transfer(&Self::account_id(), &to, amount, ExistenceRequirement::KeepAlive);
T::Currency::transfer(&Self::account_id(), &to, amount, ExistenceRequirement::KeepAlive)?;

Self::deposit_event(Event::SpentFunds(to, amount));

Expand All @@ -90,7 +90,7 @@ pub mod pallet {
pub fn tip(origin: OriginFor<T>, amount: BalanceOf<T, I>) -> DispatchResultWithPostInfo {
let tipper = ensure_signed(origin)?;

let _ = T::Currency::transfer(&tipper, &Self::account_id(), amount, ExistenceRequirement::AllowDeath);
T::Currency::transfer(&tipper, &Self::account_id(), amount, ExistenceRequirement::AllowDeath)?;

Self::deposit_event(Event::TipReceived(tipper, amount));

Expand Down
24 changes: 24 additions & 0 deletions pallets/reserve/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ fn check_weight_info() {
assert_ne!(SubstrateWeight::<Test>::tip(), Weight::from_parts(0, 0));
assert_ne!(SubstrateWeight::<Test>::spend(), Weight::from_parts(0, 0));
}
#[test]
fn spend_too_much_funds_to_target() {
new_test_ext().execute_with(|| {
TestCurrency::make_free_balance_be(&TestModule::account_id(), 100);

assert_eq!(Balances::free_balance(TestModule::account_id()), 100);
assert_eq!(Balances::free_balance(3), 0);
assert!(TestModule::spend(RuntimeOrigin::signed(Admin::get()), 3, 200).is_err());
})
}

#[test]
fn spend_funds_to_target() {
Expand All @@ -145,6 +155,20 @@ fn spend_funds_to_target() {
})
}

#[test]
fn tip_to_much() {
simonsso marked this conversation as resolved.
Show resolved Hide resolved
new_test_ext().execute_with(|| {
TestCurrency::make_free_balance_be(&999, 100);

let ans = TestModule::tip(RuntimeOrigin::signed(999), 150);
assert!(ans.is_err());

// Balances should be unchanged
assert_eq!(Balances::free_balance(999), 100);
assert_eq!(Balances::free_balance(TestModule::account_id()), 0);
})
}

#[test]
fn tip() {
new_test_ext().execute_with(|| {
Expand Down