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

Update faucet #193

Open
wants to merge 3 commits into
base: devnet
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions pallets/faucet/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ mod benchmarks {
assert!(Requests::<T>::contains_key(&caller));
}

#[benchmark]
fn refill_faucet() {
let amount = 100u32.into();
let caller: T::AccountId = whitelisted_caller();
let who: T::AccountId = caller.clone();
#[extrinsic_call]
crate::benchmarking::benchmarks::refill_faucet(
RawOrigin::Signed(caller.clone()),
amount,
);

assert!(Requests::<T>::contains_key(&caller));
}

impl_benchmark_test_suite!(
Faucet,
crate::mock::ExtBuilder::default().build(),
Expand Down
28 changes: 23 additions & 5 deletions pallets/faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ pub mod pallet {
AmountTooHigh,
/// More than allowed funds requested during `Config::AccumulationPeriod`.
RequestLimitExceeded,
/// No account to send funds.
NoFaucetAccount,
/// No funds on faucet account.
NoAmountToTransfer,
}

#[pallet::call]
Expand Down Expand Up @@ -148,17 +148,35 @@ pub mod pallet {
ensure!(total <= T::FaucetAmount::get(), Error::<T>::RequestLimitExceeded);

let account_id = Self::account_id();
let faucet_balance = T::Currency::free_balance(&account_id);

let _ = T::Currency::make_free_balance_be(&account_id, amount);
let _ =
T::Currency::transfer(&account_id, &who, amount, ExistenceRequirement::AllowDeath);
// Ensure faucet has enough balance
ensure!(faucet_balance >= amount, Error::<T>::NoAmountToTransfer);

T::Currency::transfer(&account_id, &who, amount, ExistenceRequirement::AllowDeath)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow Death?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, I mixed up the meaning of the two flags. Changed


Requests::<T>::insert(&who, (total, now));

Self::deposit_event(Event::FundsSent { who, amount });

Ok(())
}

/// Refill the faucet account.
#[pallet::call_index(1)]
#[pallet::weight(
(<T as Config>::WeightInfo::refill_faucet(), DispatchClass::Normal, Pays::No)
)]
pub fn refill_faucet(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;

let account_id = Self::account_id();

// Transfer funds from the sender to the faucet account
T::Currency::transfer(&sender, &account_id, amount, ExistenceRequirement::AllowDeath)?;

Ok(())
}
}

#[pallet::validate_unsigned]
Expand Down
11 changes: 11 additions & 0 deletions pallets/faucet/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ use core::marker::PhantomData;
pub trait WeightInfo {
#[allow(missing_docs)]
fn request_funds() -> Weight;

#[allow(missing_docs)]
fn refill_faucet() -> Weight;
}

/// Weights for pallet_faucet using the Substrate node and recommended hardware.
Expand All @@ -44,11 +47,19 @@ impl<T: frame_system::Config> WeightInfo for FaucetWeight<T> {
fn request_funds() -> Weight {
Weight::zero()
}

fn refill_faucet() -> Weight {
Weight::zero()
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
fn request_funds() -> Weight {
Weight::zero()
}

fn refill_faucet() -> Weight {
Weight::zero()
}
}
22 changes: 12 additions & 10 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("atleta"),
impl_name: create_runtime_str!("atleta"),
authoring_version: 1,
spec_version: 5,
spec_version: 6,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 3,
Expand Down Expand Up @@ -754,14 +754,15 @@ parameter_types! {
pub const FaucetPalletId: PalletId = PalletId(*b"ATFAUCET");
}

// impl pallet_faucet::Config for Runtime {
// type AccumulationPeriod = AccumulationPeriod;
// type RuntimeEvent = RuntimeEvent;
// type Currency = Balances;
// type PalletId = FaucetPalletId;
// type FaucetAmount = FaucetAmount;
// type WeightInfo = pallet_faucet::weights::FaucetWeight<Runtime>;
// }
#[cfg(any(feature = "testnet-runtime", feature = "devnet-runtime"))]
hrls marked this conversation as resolved.
Show resolved Hide resolved
impl pallet_faucet::Config for Runtime {
type AccumulationPeriod = AccumulationPeriod;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type PalletId = FaucetPalletId;
type FaucetAmount = FaucetAmount;
type WeightInfo = pallet_faucet::weights::FaucetWeight<Runtime>;
}

// nomination pools
parameter_types! {
Expand Down Expand Up @@ -1569,7 +1570,8 @@ construct_runtime!(
Babe: pallet_babe,
Grandpa: pallet_grandpa,
Balances: pallet_balances,
// Faucet: pallet_faucet,
#[cfg(any(feature = "testnet-runtime", feature = "devnet-runtime"))]
Faucet: pallet_faucet,
TransactionPayment: pallet_transaction_payment,
Sudo: pallet_sudo,
Treasury: pallet_treasury,
Expand Down
Loading