Skip to content

Commit

Permalink
fix faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
mutobui committed Oct 2, 2023
1 parent df9e5d7 commit 39ebdd8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
28 changes: 13 additions & 15 deletions pallets/pallet-faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
SelfTransfer,
NoGenesisAccountAvailable,
NotEnoughBalance,
DontBeGreedy,
PleaseWait,
Expand Down Expand Up @@ -161,22 +161,20 @@ pub mod pallet {
T::Currency::free_balance(&from) > amount,
<Error<T>>::NotEnoughBalance
);
let genesis_accounts = GenesisAccounts::<T>::get();
ensure!(genesis_accounts[0] != from, <Error<T>>::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::<T>::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(<Error<T>>::NoGenesisAccountAvailable.into())
}
Ok(())
}
}
Expand Down
43 changes: 30 additions & 13 deletions pallets/pallet-faucet/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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::<Test>::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::<Test>::deposit_creating(&sender, FAUCET_BALANCE);
assert_err!(Faucet::faucet(RuntimeOrigin::signed(sender.clone())), <Error<Test>>::DontBeGreedy);
assert_err!(
Faucet::faucet(RuntimeOrigin::signed(sender.clone())),
<Error<Test>>::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())), <Error<Test>>::PleaseWait);
assert_err!(
Faucet::faucet(RuntimeOrigin::signed(sender.clone())),
<Error<Test>>::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::<Test>::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::<Test>::deposit_creating(&sender, FAUCET_BALANCE);

assert_err!(Faucet::donate(RuntimeOrigin::signed(sender.clone()), FAUCET_BALANCE.add(100_000)), <Error<Test>>::NotEnoughBalance);
assert_err!(
Faucet::donate(
RuntimeOrigin::signed(sender.clone()),
FAUCET_BALANCE.add(100_000)
),
<Error<Test>>::NotEnoughBalance
);
})
}

0 comments on commit 39ebdd8

Please sign in to comment.