Skip to content

Commit

Permalink
Merge pull request #277 from peaqnetwork/feature/1207996900912929_upd…
Browse files Browse the repository at this point in the history
…ate-evm-fees

Update evm fees
  • Loading branch information
lavish0000 authored Aug 19, 2024
2 parents e7f47d2 + 565be9a commit c964f09
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions runtime/peaq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
},
ApplyExtrinsicResult, Perbill, Percent, Permill,
ApplyExtrinsicResult, FixedPointNumber, Perbill, Percent, Permill,
};
use sp_std::{marker::PhantomData, prelude::*, vec, vec::Vec};
#[cfg(feature = "std")]
Expand Down Expand Up @@ -136,6 +136,32 @@ type Hash = peaq_primitives_xcm::Hash;
/// Note: this is really wild! You can define it here, but not in peaq_primitives_xcm...?!
pub type Block = generic::Block<Header, UncheckedExtrinsic>;

// Implementation from Moonbeam
/// peaq, the native token, uses 18 decimals of precision.
pub mod currency {
use super::Balance;

// Provide a common factor between runtimes based on a supply of 10_000_000 tokens.
pub const SUPPLY_FACTOR: Balance = 100;

pub const WEI: Balance = 1;
pub const KILOWEI: Balance = 1_000;
pub const MEGAWEI: Balance = 1_000_000;
pub const GIGAWEI: Balance = 1_000_000_000;
pub const MICROPEAQ: Balance = 1_000_000_000_000;
pub const MILLIPEAQ: Balance = 1_000_000_000_000_000;
pub const PEAQ: Balance = 1_000_000_000_000_000_000;
pub const KILOPEAQ: Balance = 1_000_000_000_000_000_000_000;

pub const TRANSACTION_BYTE_FEE: Balance = GIGAWEI * SUPPLY_FACTOR;
pub const STORAGE_BYTE_FEE: Balance = 100 * MICROPEAQ * SUPPLY_FACTOR;
pub const WEIGHT_FEE: Balance = 40 * KILOWEI * SUPPLY_FACTOR;

// pub const fn deposit(items: u32, bytes: u32) -> Balance {
// items as Balance * 100 * MILLIPEAQ * SUPPLY_FACTOR + (bytes as Balance) * STORAGE_BYTE_FEE
// }
}

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
Expand Down Expand Up @@ -633,8 +659,30 @@ parameter_types! {
pub GasLimitStorageGrowthRatio: u64 = 1;
}

pub struct TransactionPaymentAsGasPrice;
impl FeeCalculator for TransactionPaymentAsGasPrice {
fn min_gas_price() -> (U256, Weight) {
// note: transaction-payment differs from EIP-1559 in that its tip and length fees are not
// scaled by the multiplier, which means its multiplier will be overstated when
// applied to an ethereum transaction
// note: transaction-payment uses both a congestion modifier (next_fee_multiplier, which is
// updated once per block in on_finalize) and a 'WeightToFee' implementation. Our
// runtime implements this as a 'ConstantModifier', so we can get away with a simple
// multiplication here.
// It is imperative that `saturating_mul_int` be performed as late as possible in the
// expression since it involves fixed point multiplication with a division by a fixed
// divisor. This leads to truncation and subsequent precision loss if performed too early.
// This can lead to min_gas_price being same across blocks even if the multiplier changes.
// There's still some precision loss when the final `gas_price` (used_gas * min_gas_price)
// is computed in frontier, but that's currently unavoidable.
let min_gas_price = TransactionPayment::next_fee_multiplier()
.saturating_mul_int(currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS as u128));
(min_gas_price.into(), <Runtime as frame_system::Config>::DbWeight::get().reads(1))
}
}

impl pallet_evm::Config for Runtime {
type FeeCalculator = BaseFee;
type FeeCalculator = TransactionPaymentAsGasPrice;
type WeightPerGas = WeightPerGas;
type GasWeightMapping = PeaqGasWeightMapping;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
Expand Down

0 comments on commit c964f09

Please sign in to comment.