diff --git a/pallets/pallet-faucet/src/lib.rs b/pallets/pallet-faucet/src/lib.rs index aa04c352..d50456d3 100644 --- a/pallets/pallet-faucet/src/lib.rs +++ b/pallets/pallet-faucet/src/lib.rs @@ -89,7 +89,7 @@ pub mod pallet { #[pallet::error] pub enum Error { - SelfTransfer, + NoGenesisAccountAvailable, NotEnoughBalance, DontBeGreedy, PleaseWait, @@ -161,22 +161,20 @@ pub mod pallet { T::Currency::free_balance(&from) > amount, >::NotEnoughBalance ); - let genesis_accounts = GenesisAccounts::::get(); - ensure!(genesis_accounts[0] != from, >::SelfTransfer); - - T::Currency::transfer( - &from, - &genesis_accounts[0], - amount, - ExistenceRequirement::KeepAlive, - )?; - Self::deposit_event(Event::Transferred( - from, - genesis_accounts[0].clone(), - amount, - )); + let genesis_accounts = GenesisAccounts::::get(); + if let Some(genesis_account) = genesis_accounts.first() { + T::Currency::transfer( + &from, + genesis_account, + amount, + ExistenceRequirement::KeepAlive, + )?; + Self::deposit_event(Event::Transferred(from, genesis_account.clone(), amount)); + } else { + return Err(>::NoGenesisAccountAvailable.into()) + } Ok(()) } } diff --git a/pallets/pallet-faucet/src/tests.rs b/pallets/pallet-faucet/src/tests.rs index 727d5200..232e5780 100644 --- a/pallets/pallet-faucet/src/tests.rs +++ b/pallets/pallet-faucet/src/tests.rs @@ -1,7 +1,7 @@ -use std::{ops::Add}; +use std::ops::Add; use crate::{mock::*, Error}; -use frame_support::{assert_ok, traits::Currency, assert_err}; +use frame_support::{assert_err, assert_ok, traits::Currency}; use sp_runtime::AccountId32; #[test] @@ -18,53 +18,70 @@ fn faucet_works() { fn faucet_works_with_low_balance() { ExtBuilder::default().build_and_execute(|| { let sender = AccountId32::new([11; 32]); - let legit_balance = FAUCET_BALANCE/10 - 1u64; + let legit_balance = FAUCET_BALANCE / 10 - 1u64; let _ = pallet_balances::Pallet::::deposit_creating(&sender, legit_balance); assert_eq!(Balances::free_balance(&sender), legit_balance); assert_ok!(Faucet::faucet(RuntimeOrigin::signed(sender.clone()))); - assert_eq!(Balances::free_balance(&sender), FAUCET_BALANCE + legit_balance); + assert_eq!( + Balances::free_balance(&sender), + FAUCET_BALANCE + legit_balance + ); }) } - #[test] fn faucet_fail() { ExtBuilder::default().build_and_execute(|| { - let sender = AccountId32::new([11;32]); + let sender = AccountId32::new([11; 32]); let _ = pallet_balances::Pallet::::deposit_creating(&sender, FAUCET_BALANCE); - assert_err!(Faucet::faucet(RuntimeOrigin::signed(sender.clone())), >::DontBeGreedy); + assert_err!( + Faucet::faucet(RuntimeOrigin::signed(sender.clone())), + >::DontBeGreedy + ); }) } #[test] fn faucet_should_fail_when_still_in_cache_time() { ExtBuilder::default().build_and_execute(|| { - let sender = AccountId32::new([11;32]); + let sender = AccountId32::new([11; 32]); assert_ok!(Faucet::faucet(RuntimeOrigin::signed(sender.clone()))); - assert_err!(Faucet::faucet(RuntimeOrigin::signed(sender.clone())), >::PleaseWait); + assert_err!( + Faucet::faucet(RuntimeOrigin::signed(sender.clone())), + >::PleaseWait + ); }) } #[test] fn donate_work() { ExtBuilder::default().build_and_execute(|| { - let sender = AccountId32::new([11;32]); + let sender = AccountId32::new([11; 32]); let _ = pallet_balances::Pallet::::deposit_creating(&sender, FAUCET_BALANCE); let before_balance = Balances::free_balance(GENESIS_ACCOUNT.clone()); let _ = Faucet::donate(RuntimeOrigin::signed(sender.clone()), 400_000); - assert_eq!(Balances::free_balance(GENESIS_ACCOUNT.clone()), before_balance.add(400_000)) + assert_eq!( + Balances::free_balance(GENESIS_ACCOUNT.clone()), + before_balance.add(400_000) + ) }) } #[test] fn donate_fail() { ExtBuilder::default().build_and_execute(|| { - let sender = AccountId32::new([11;32]); + let sender = AccountId32::new([11; 32]); let _ = pallet_balances::Pallet::::deposit_creating(&sender, FAUCET_BALANCE); - assert_err!(Faucet::donate(RuntimeOrigin::signed(sender.clone()), FAUCET_BALANCE.add(100_000)), >::NotEnoughBalance); + assert_err!( + Faucet::donate( + RuntimeOrigin::signed(sender.clone()), + FAUCET_BALANCE.add(100_000) + ), + >::NotEnoughBalance + ); }) }