virto-network / open-runtime-module-library

Substrate Open Runtime Module Library
Apache License 2.0
0 stars 1 forks source link

[feat] make fee handler generic #17

Closed stanly-johnson closed 2 years ago

stanly-johnson commented 2 years ago

Expands the FeeHandler trait to make it more generic, the trait is now

pub trait FeeHandler<T: pallet::Config> {
    /// Get the amount of fee to charge user
    fn get_fee_amount(
        from: &T::AccountId,
        to: &T::AccountId,
        detail: &PaymentDetail<T>,
        remark: Option<&[u8]>,
    ) -> BalanceOf<T>;

    /// Deduct the fee from the user transaction
    fn apply_fees(from: &T::AccountId, to: &T::AccountId, detail: &PaymentDetail<T>) -> DispatchResult;
}

During the initial creation of the payment, the get_fee_amount is called to store the fee_amount, when the payment is settled, the apply_fees function is called, depending on the implementation it may deduct fee from user and transfer to fee recipients.

The previous logic of transferring to recipients can be achieved by implementing apply_fees like this

    fn apply_fees(from: &AccountId, _to: &AccountId, detail: &PaymentDetail<Test>) -> DispatchResult {
        // transfer the fee amount to fee recipient account
        <Tokens as MultiCurrency<AccountId>>::transfer(
            detail.asset,
            from,                   // fee is paid by payment creator
            &FEE_RECIPIENT_ACCOUNT, // account of fee recipient
            detail.fee_amount,      // amount of fee
        )
    }

Every runtime will have to benchmark the pallet based on their implementation of apply_fees.

Closes #10 since any complex logic can be written in apply_fees.