Skip to content

Commit

Permalink
Ab/faucet without treasury (#384)
Browse files Browse the repository at this point in the history
* remove treasury dependency from faucet pallet. introduce catch all account for pallet

* bump crate version

* fix ci fmt to nightly

* bump 6.2.0 because the change breaks behavior
  • Loading branch information
brenzi authored Apr 10, 2024
1 parent a995ad7 commit c7aa9b4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 132 deletions.
23 changes: 1 addition & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions faucet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-encointer-faucet"
version = "6.1.0"
version = "6.2.0"
authors = ["Encointer Association <info@encointer.org>"]
edition = "2021"
description = "Faucet pallet for the Encointer blockchain runtime"
Expand All @@ -23,7 +23,6 @@ pallet-encointer-reputation-commitments = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-treasury = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
Expand All @@ -43,7 +42,6 @@ std = [
"log/std",
"pallet-encointer-communities/std",
"pallet-encointer-reputation-commitments/std",
"pallet-treasury/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-core/std",
Expand All @@ -57,7 +55,6 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"pallet-encointer-communities/runtime-benchmarks",
"pallet-encointer-reputation-commitments/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
]
try-runtime = [
"frame-system/try-runtime",
Expand Down
26 changes: 18 additions & 8 deletions faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ use frame_system::{self as frame_system, ensure_signed};
use log::info;
use parity_scale_codec::Decode;
use sp_core::H256;
use sp_runtime::{traits::Hash, SaturatedConversion, Saturating};
use sp_runtime::{
traits::{AccountIdConversion, Hash},
SaturatedConversion, Saturating,
};
use sp_std::convert::TryInto;
pub use weights::WeightInfo;

// Logger target
const LOG: &str = "encointer";

pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
Expand Down Expand Up @@ -71,12 +75,13 @@ pub mod pallet {
frame_system::Config
+ pallet_encointer_reputation_commitments::Config
+ pallet_encointer_communities::Config
+ pallet_treasury::Config
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId> + NamedReservableCurrency<Self::AccountId>;
type ControllerOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type WeightInfo: WeightInfo;

/// The treasury's pallet id, used for deriving its sovereign account ID.
#[pallet::constant]
type PalletId: Get<PalletId>;
}
Expand All @@ -89,7 +94,7 @@ pub mod pallet {
ReserveIdentifierOf<T>: From<[u8; 8]>,
{
#[pallet::call_index(0)]
#[pallet::weight((<T as Config>::WeightInfo::create_faucet(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((< T as Config >::WeightInfo::create_faucet(), DispatchClass::Normal, Pays::Yes))]
pub fn create_faucet(
origin: OriginFor<T>,
name: FaucetNameType,
Expand Down Expand Up @@ -152,7 +157,7 @@ pub mod pallet {
Ok(().into())
}
#[pallet::call_index(1)]
#[pallet::weight((<T as Config>::WeightInfo::drip(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((< T as Config >::WeightInfo::drip(), DispatchClass::Normal, Pays::Yes))]
pub fn drip(
origin: OriginFor<T>,
faucet_account: T::AccountId,
Expand Down Expand Up @@ -195,7 +200,7 @@ pub mod pallet {
}

#[pallet::call_index(2)]
#[pallet::weight((<T as Config>::WeightInfo::dissolve_faucet(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((< T as Config >::WeightInfo::dissolve_faucet(), DispatchClass::Normal, Pays::Yes))]
pub fn dissolve_faucet(
origin: OriginFor<T>,
faucet_account: T::AccountId,
Expand Down Expand Up @@ -224,7 +229,7 @@ pub mod pallet {
}

#[pallet::call_index(3)]
#[pallet::weight((<T as Config>::WeightInfo::close_faucet(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((< T as Config >::WeightInfo::close_faucet(), DispatchClass::Normal, Pays::Yes))]
pub fn close_faucet(
origin: OriginFor<T>,
faucet_account: T::AccountId,
Expand All @@ -247,7 +252,7 @@ pub mod pallet {
<Faucets<T>>::remove(&faucet_account);
<T as Config>::Currency::transfer(
&faucet_account,
&<pallet_treasury::Pallet<T>>::account_id(),
&Self::catch_basin_account_id(),
<T as Config>::Currency::free_balance(&faucet_account),
AllowDeath,
)?;
Expand Down Expand Up @@ -283,6 +288,11 @@ pub mod pallet {
.expect("[u8; 32] can always be converted to [u8; 8]");
reserve_id.into()
}

/// returns the account id where remaining funds of closed faucets go
pub fn catch_basin_account_id() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}
}

#[derive(frame_support::DefaultNoBound)]
Expand All @@ -301,7 +311,7 @@ pub mod pallet {
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// faucet dripped | facuet account, receiver account, balance
Dripped(T::AccountId, T::AccountId, BalanceOf<T>),
Expand Down
99 changes: 3 additions & 96 deletions faucet/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@

use crate as dut;
use encointer_primitives::balances::BalanceType;
use frame_support::{
pallet_prelude::Get,
parameter_types,
traits::{
tokens::{ConversionFromAssetBalance, Pay, PaymentStatus},
ConstU64,
},
PalletId,
};
use sp_runtime::{BuildStorage, Permill};
use std::{cell::RefCell, collections::BTreeMap, marker::PhantomData};
use frame_support::{parameter_types, PalletId};
use sp_runtime::BuildStorage;

use test_utils::*;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
Expand All @@ -45,97 +37,12 @@ frame_support::construct_runtime!(
EncointerCeremonies: pallet_encointer_ceremonies::{Pallet, Call, Storage, Config<T>, Event<T>},
EncointerReputationCommitments:pallet_encointer_reputation_commitments::{Pallet, Call, Storage, Event<T>},
EncointerCommunities: pallet_encointer_communities::{Pallet, Call, Storage, Event<T>},
Treasury: pallet_treasury::{Pallet, Call, Storage, Config<T>, Event<T>},
}
);

thread_local! {
pub static PAID: RefCell<BTreeMap<(u128, u32), u64>> = RefCell::new(BTreeMap::new());
pub static STATUS: RefCell<BTreeMap<u64, PaymentStatus>> = RefCell::new(BTreeMap::new());
pub static LAST_ID: RefCell<u64> = RefCell::new(0u64);
}

/// set status for a given payment id
#[cfg(feature = "runtime-benchmarks")]
fn set_status(id: u64, s: PaymentStatus) {
STATUS.with(|m| m.borrow_mut().insert(id, s));
}
pub struct TestPay;
impl Pay for TestPay {
type Beneficiary = u128;
type Balance = u64;
type Id = u64;
type AssetKind = u32;
type Error = ();

fn pay(
_who: &Self::Beneficiary,
_asset_kind: Self::AssetKind,
_amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
Ok(LAST_ID.with(|lid| {
let x = *lid.borrow();
lid.replace(x + 1);
x
}))
}
fn check_payment(id: Self::Id) -> PaymentStatus {
STATUS.with(|s| s.borrow().get(&id).cloned().unwrap_or(PaymentStatus::Unknown))
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(_: &Self::Beneficiary, _: Self::AssetKind, _: Self::Balance) {}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::Id) {
set_status(id, PaymentStatus::Failure)
}
}
pub struct MulBy<N>(PhantomData<N>);
impl<N: Get<u64>> ConversionFromAssetBalance<u64, u32, u64> for MulBy<N> {
type Error = ();
fn from_asset_balance(balance: u64, _asset_id: u32) -> Result<u64, Self::Error> {
return balance.checked_mul(N::get()).ok_or(())
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(_: u32) {}
}

parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
pub const Burn: Permill = Permill::from_percent(50);
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub const SpendPayoutPeriod: u64 = 5;
}
impl pallet_treasury::Config for TestRuntime {
type PalletId = TreasuryPalletId;
type Currency = pallet_balances::Pallet<TestRuntime>;
type ApproveOrigin = EnsureAlice;
type RejectOrigin = EnsureAlice;
type RuntimeEvent = RuntimeEvent;
type OnSlash = ();
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ConstU64<1>;
type ProposalBondMaximum = ();
type SpendPeriod = ConstU64<2>;
type Burn = Burn;
type BurnDestination = (); // Just gets burned.
type WeightInfo = ();
type SpendFunds = ();
type MaxApprovals = ConstU32<100>;
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
type AssetKind = u32;
type Beneficiary = u128;
type BeneficiaryLookup = IdentityLookup<Self::Beneficiary>;
type Paymaster = TestPay;
type BalanceConverter = MulBy<ConstU64<2>>;
type PayoutPeriod = SpendPayoutPeriod;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
pub const FaucetPalletId: PalletId = PalletId(*b"faucetId");
}

impl dut::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type ControllerOrigin = EnsureAlice;
Expand Down
4 changes: 2 additions & 2 deletions faucet/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Unit tests for the encointer_faucet module.

use super::*;
use crate::mock::{Balances, EncointerFaucet, EncointerReputationCommitments, System, Treasury};
use crate::mock::{Balances, EncointerFaucet, EncointerReputationCommitments, System};
use encointer_primitives::{
ceremonies::Reputation,
faucet::FromStr,
Expand Down Expand Up @@ -621,7 +621,7 @@ fn close_faucet_works() {
assert_eq!(Balances::free_balance(&faucet_account), 0);
assert_eq!(Balances::free_balance(&alice), 12);
assert_eq!(Balances::free_balance(&bob), 965);
assert_eq!(Balances::free_balance(&Treasury::account_id()), 23);
assert_eq!(Balances::free_balance(&EncointerFaucet::catch_basin_account_id()), 23);
assert_eq!(Balances::reserved_balance(&bob), 0);

assert_eq!(
Expand Down

0 comments on commit c7aa9b4

Please sign in to comment.