Skip to content

Commit

Permalink
Fees API Improvement (#1623)
Browse files Browse the repository at this point in the history
* fee information in types

* modify method name
  • Loading branch information
lemunozm authored Nov 27, 2023
1 parent 1e65036 commit a38a6de
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ pub mod fees {
fee: Fee<Self::Balance, Self::FeeKey>,
) -> DispatchResult;
}

/// Trait to pay fees
/// This trait can be used by pallet to just pay fees without worring about
/// the value or where the fee goes.
pub trait PayFee<AccountId> {
fn pay(who: &AccountId) -> DispatchResult;
}
}

/// Trait to determine whether a sending account and currency have a
Expand Down
2 changes: 2 additions & 0 deletions libs/types/src/fee_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ impl Default for FeeKey {
FeeKey::AnchorsCommit
}
}

pub type Fee = cfg_traits::fees::Fee<cfg_primitives::Balance, FeeKey>;
39 changes: 38 additions & 1 deletion runtime/common/src/fees.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use cfg_primitives::{
constants::{CENTI_CFG, TREASURY_FEE_RATIO},
types::Balance,
AccountId,
};
use cfg_traits::fees::{Fee, Fees, PayFee};
use cfg_types::fee_keys::FeeKey;
use frame_support::{
traits::{Currency, Imbalance, OnUnbalanced},
dispatch::DispatchResult,
traits::{Currency, Get, Imbalance, OnUnbalanced},
weights::{
constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
Expand Down Expand Up @@ -82,6 +86,39 @@ impl WeightToFeePolynomial for WeightToFee {
}
}

pub struct FeeToTreasury<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
V: Get<Fee<Balance, FeeKey>>,
> PayFee<AccountId> for FeeToTreasury<F, V>
{
fn pay(who: &AccountId) -> DispatchResult {
F::fee_to_treasury(who, V::get())
}
}

pub struct FeeToAuthor<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
V: Get<Fee<Balance, FeeKey>>,
> PayFee<AccountId> for FeeToAuthor<F, V>
{
fn pay(who: &AccountId) -> DispatchResult {
F::fee_to_author(who, V::get())
}
}

pub struct FeeToBurn<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
V: Get<Fee<Balance, FeeKey>>,
> PayFee<AccountId> for FeeToBurn<F, V>
{
fn pay(who: &AccountId) -> DispatchResult {
F::fee_to_burn(who, V::get())
}
}

#[cfg(test)]
mod test {
use cfg_primitives::{AccountId, TREASURY_FEE_RATIO};
Expand Down

0 comments on commit a38a6de

Please sign in to comment.