Skip to content

Commit

Permalink
Fixup Protocol Fee pallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Jan 14, 2025
1 parent f8ebbed commit c031047
Showing 1 changed file with 36 additions and 45 deletions.
81 changes: 36 additions & 45 deletions pallets/protocol-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ pub mod pallet {
type Subsidiser: SubsidiserTrait<Self::AccountId, Self::RuntimeCall>;
}

#[pallet::error]
pub enum Error<T> {
/// Insufficient account balance to pay the fee.
InsufficientAccountBalance,
/// Not able to handled the imbalances
UnHandledImbalances,
/// Insufficient subsidy balance to pay the fee.
InsufficientSubsidyBalance,
}

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand All @@ -99,6 +109,30 @@ pub mod pallet {
#[pallet::getter(fn coefficient)]
pub type Coefficient<T: Config> = StorageValue<_, PosRatio, ValueQuery>;

#[pallet::genesis_config]
#[derive(Default)]
pub struct GenesisConfig {
pub base_fees: Vec<(ProtocolOp, Balance)>,
pub coefficient: PosRatio,
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
// Set initial base fees
for (op, fee) in &self.base_fees {
BaseFees::<T>::insert(op, fee);
}

// Set initial coefficient
if self.coefficient.1 == 0 {
Coefficient::<T>::put(&PosRatio(1, 1));
} else {
Coefficient::<T>::put(&self.coefficient);
}
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand All @@ -110,24 +144,14 @@ pub mod pallet {
FeeCharged(T::AccountId, Balance),
}

#[pallet::error]
pub enum Error<T> {
/// Insufficient account balance to pay the fee.
InsufficientAccountBalance,
/// Not able to handled the imbalances
UnHandledImbalances,
/// Insufficient subsidy balance to pay the fee.
InsufficientSubsidyBalance,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Changes the fee coefficient for the root origin.
///
/// # Errors
/// * `BadOrigin` - Only root allowed.
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::change_coefficient())]
#[pallet::call_index(0)]
pub fn change_coefficient(
origin: OriginFor<T>,
coefficient: PosRatio,
Expand All @@ -142,8 +166,8 @@ pub mod pallet {
///
/// # Errors
/// * `BadOrigin` - Only root allowed.
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::change_base_fee())]
#[pallet::call_index(1)]
pub fn change_base_fee(
origin: OriginFor<T>,
op: ProtocolOp,
Expand All @@ -155,39 +179,6 @@ pub mod pallet {
Ok(().into())
}
}

#[pallet::genesis_config]
pub struct GenesisConfig {
pub base_fees: Vec<(ProtocolOp, Balance)>,
pub coefficient: PosRatio,
}

#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self {
base_fees: vec![],
coefficient: PosRatio(1, 1),
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
// Set initial base fees
for (op, fee) in &self.base_fees {
BaseFees::<T>::insert(op, fee);
}

// Set initial coefficient
if self.coefficient.1 == 0 {
Coefficient::<T>::put(&PosRatio(1, 1));
} else {
Coefficient::<T>::put(&self.coefficient);
}
}
}
}

impl<T: Config> Pallet<T> {
Expand Down

0 comments on commit c031047

Please sign in to comment.