Skip to content

Commit

Permalink
Further modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Sep 14, 2023
1 parent a7634fa commit 04cfd6d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pallets/dynamic-evm-base-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

use frame_support::{traits::Get, weights::Weight};
use sp_core::U256;
use sp_runtime::{traits::Convert, traits::UniqueSaturatedInto};
use sp_runtime::{traits::Convert, traits::UniqueSaturatedInto, Perquintill};

// TODO: not sure if Perbill will be precise enough here. Need to write tests to check.

pub use self::pallet::*;

Expand All @@ -40,7 +42,11 @@ pub mod pallet {
type DefaultBaseFeePerGas: Get<U256>;
type MinBaseFeePerGas: Get<U256>;
type MaxBaseFeePerGas: Get<U256>;
type AdjustmentLogic: Convert<u128, u128>;

type AdjustmentFactor: Get<u128>;
type WeightFactor: Get<u128>;

type MaxRatio: Perquintill;
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -89,10 +95,17 @@ pub mod pallet {

fn on_finalize(_n: <T as frame_system::Config>::BlockNumber) {
BaseFeePerGas::<T>::mutate(|base_fee_per_gas| {
let new_base_fee_per_gas =
T::AdjustmentLogic::convert(base_fee_per_gas.clone().unique_saturated_into());
let old_bfpg = *base_fee_per_gas;
let new_bfpg = T::AdjustmentFactor::get()
.saturating_mul(T::WeightFactor::get())
.saturating_mul(25)
.saturating_div(98974);

// TODO: continue here - I can either use Perquintill or Rational but have to see which one is better for my purpose.
// The problem with Perquintill is that it saturates at 1, so I would have to express the "opposite" ratio - smaller_value/larger_value for it to work. Which is fine...
// if new_bfpg > old_bfpg &&

*base_fee_per_gas = U256::from(new_base_fee_per_gas)
*base_fee_per_gas = U256::from(new_bfpg)
.clamp(T::MinBaseFeePerGas::get(), T::MaxBaseFeePerGas::get());
})
}
Expand Down

0 comments on commit 04cfd6d

Please sign in to comment.