pallet_evm_tracker/
fees.rs

1//! Fees module for EVM domain
2
3use crate::Config;
4use core::marker::PhantomData;
5use domain_runtime_primitives::Balance;
6use pallet_block_fees::Pallet as BlockFees;
7use pallet_evm::FeeCalculator;
8use pallet_transaction_payment::Pallet as TransactionPayment;
9use sp_core::U256;
10use sp_evm_tracker::WEIGHT_PER_GAS;
11use sp_runtime::traits::Get;
12use sp_runtime::{FixedPointNumber, Perbill};
13use sp_weights::Weight;
14
15/// Evm gas price calculator for EVM domains.
16/// TransactionWeightFee is the fee for 1 unit of Weight.
17/// GasPerByte is the gas for 1 byte
18pub struct EvmGasPriceCalculator<T, TransactionWeightFee, GasPerByte, StorageFeePercent>(
19    PhantomData<(T, TransactionWeightFee, GasPerByte, StorageFeePercent)>,
20);
21
22impl<T, TransactionWeightFee, GasPerByte, StorageFeePercent> FeeCalculator
23    for EvmGasPriceCalculator<T, TransactionWeightFee, GasPerByte, StorageFeePercent>
24where
25    T: Config
26        + frame_system::Config
27        + pallet_transaction_payment::Config
28        + pallet_block_fees::Config<Balance = Balance>,
29    TransactionWeightFee: Get<T::Balance>,
30    GasPerByte: Get<T::Balance>,
31{
32    fn min_gas_price() -> (U256, Weight) {
33        // spread the storage fee across the gas price based on the Gas Per Byte.
34        let storage_fee_per_gas =
35            BlockFees::<T>::final_domain_transaction_byte_fee().div_ceil(GasPerByte::get());
36        // adjust the fee per weight using the multiplier
37        let weight_fee = TransactionWeightFee::get().saturating_mul(WEIGHT_PER_GAS.into());
38        let adjusted_weight_fee =
39            TransactionPayment::<T>::next_fee_multiplier().saturating_mul_int(weight_fee);
40
41        // finally add the storage_fee_per_gas and adjusted_weight_fee to calculate the final
42        // min_gas_price.
43        let min_gas_price = adjusted_weight_fee.saturating_add(storage_fee_per_gas);
44        (
45            min_gas_price.into(),
46            <T as frame_system::Config>::DbWeight::get().reads(2),
47        )
48    }
49}
50
51impl<T, TransactionWeightFee, BytePerFee, StorageFeePercent>
52    EvmGasPriceCalculator<T, TransactionWeightFee, BytePerFee, StorageFeePercent>
53where
54    StorageFeePercent: Get<Perbill>,
55{
56    pub fn split_fee_into_storage_and_execution(fee: Balance) -> (Balance, Balance) {
57        let ratio = StorageFeePercent::get();
58        let storage_fee = ratio.mul_ceil(fee);
59        (storage_fee, fee.saturating_sub(storage_fee))
60    }
61}