From 96aba0bf4cc151c9aa743e605a356f848241922f Mon Sep 17 00:00:00 2001 From: Dzmitry Rybakou Date: Fri, 15 Nov 2024 17:37:19 +0300 Subject: [PATCH 1/3] Update faucet --- pallets/faucet/src/benchmarking.rs | 15 +++++++++++++++ pallets/faucet/src/lib.rs | 28 +++++++++++++++++++++++----- pallets/faucet/src/weights.rs | 11 +++++++++++ runtime/src/lib.rs | 20 +++++++++++--------- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/pallets/faucet/src/benchmarking.rs b/pallets/faucet/src/benchmarking.rs index 51106e7b..7145e6d6 100644 --- a/pallets/faucet/src/benchmarking.rs +++ b/pallets/faucet/src/benchmarking.rs @@ -21,6 +21,21 @@ mod benchmarks { assert!(Requests::::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::request_funds( + RawOrigin::Signed(caller.clone()), + who, + amount, + ); + + assert!(Requests::::contains_key(&caller)); + } + impl_benchmark_test_suite!( Faucet, crate::mock::ExtBuilder::default().build(), diff --git a/pallets/faucet/src/lib.rs b/pallets/faucet/src/lib.rs index 655907ca..53b8ffef 100644 --- a/pallets/faucet/src/lib.rs +++ b/pallets/faucet/src/lib.rs @@ -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] @@ -148,10 +148,12 @@ pub mod pallet { ensure!(total <= T::FaucetAmount::get(), Error::::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::::NoAmountToTransfer); + + T::Currency::transfer(&account_id, &who, amount, ExistenceRequirement::AllowDeath)?; Requests::::insert(&who, (total, now)); @@ -159,6 +161,22 @@ pub mod pallet { Ok(()) } + + /// Refill the faucet account. + #[pallet::call_index(1)] + #[pallet::weight( + (::WeightInfo::refill_faucet(), DispatchClass::Normal, Pays::No) + )] + pub fn refill_faucet(origin: OriginFor, amount: BalanceOf) -> 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] diff --git a/pallets/faucet/src/weights.rs b/pallets/faucet/src/weights.rs index 3a3f210f..1e2c4490 100644 --- a/pallets/faucet/src/weights.rs +++ b/pallets/faucet/src/weights.rs @@ -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. @@ -44,6 +47,10 @@ impl WeightInfo for FaucetWeight { fn request_funds() -> Weight { Weight::zero() } + + fn refill_faucet() -> Weight { + Weight::zero() + } } // For backwards compatibility and tests @@ -51,4 +58,8 @@ impl WeightInfo for () { fn request_funds() -> Weight { Weight::zero() } + + fn refill_faucet() -> Weight { + Weight::zero() + } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9065da28..977e5a18 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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; -// } +#[cfg(any(feature = "testnet-runtime", feature = "devnet-runtime"))] +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; +} // nomination pools parameter_types! { @@ -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, From dca28d5f758be9737bdb626cca3a96976f54f3ea Mon Sep 17 00:00:00 2001 From: Dzmitry Rybakou Date: Fri, 15 Nov 2024 17:37:49 +0300 Subject: [PATCH 2/3] Increase spec_version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 977e5a18..33b4f044 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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, From e1ba1ae9dcba01aa93b5a3ee41c9e70b01f6360c Mon Sep 17 00:00:00 2001 From: Dzmitry Rybakou Date: Fri, 15 Nov 2024 17:40:25 +0300 Subject: [PATCH 3/3] Update benchmarks --- pallets/faucet/src/benchmarking.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pallets/faucet/src/benchmarking.rs b/pallets/faucet/src/benchmarking.rs index 7145e6d6..ecf93730 100644 --- a/pallets/faucet/src/benchmarking.rs +++ b/pallets/faucet/src/benchmarking.rs @@ -27,9 +27,8 @@ mod benchmarks { let caller: T::AccountId = whitelisted_caller(); let who: T::AccountId = caller.clone(); #[extrinsic_call] - crate::benchmarking::benchmarks::request_funds( + crate::benchmarking::benchmarks::refill_faucet( RawOrigin::Signed(caller.clone()), - who, amount, );