-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
[package] | ||
name = "pallet-dynamic-evm-base-fee" | ||
version = "0.1.0" | ||
license = "GPL-3.0-or-later" | ||
description = "Handler for dynamic EVM base fee for Astar tokenomics v2." | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true } | ||
|
||
# Substrate | ||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
sp-core = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
pallet-transaction-payment = { workspace = true } | ||
|
||
# Frontier | ||
fp-evm = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"scale-info/std", | ||
# Substrate | ||
"frame-support/std", | ||
"frame-system/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
"pallet-transaction-payment/std", | ||
# Frontier | ||
"fp-evm/std", | ||
] | ||
try-runtime = [ | ||
"frame-support/try-runtime", | ||
"frame-system/try-runtime", | ||
"pallet-transaction-payment/try-runtime", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use frame_support::{traits::Get, weights::Weight}; | ||
use sp_core::U256; | ||
use sp_runtime::{traits::Convert, traits::UniqueSaturatedInto}; | ||
|
||
pub use self::pallet::*; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
use super::*; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(PhantomData<T>); | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
type DefaultBaseFeePerGas: Get<U256>; | ||
type MinBaseFeePerGas: Get<U256>; | ||
type MaxBaseFeePerGas: Get<U256>; | ||
type AdjustmentLogic: Convert<u128, u128>; | ||
} | ||
|
||
#[pallet::genesis_config] | ||
pub struct GenesisConfig<T: Config> { | ||
pub base_fee_per_gas: U256, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T: Config> Default for GenesisConfig<T> { | ||
fn default() -> Self { | ||
Self { | ||
base_fee_per_gas: T::DefaultBaseFeePerGas::get(), | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
#[pallet::genesis_build] | ||
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> { | ||
fn build(&self) { | ||
BaseFeePerGas::<T>::put(self.base_fee_per_gas); | ||
} | ||
} | ||
|
||
#[pallet::type_value] | ||
pub fn DefaultBaseFeePerGas<T: Config>() -> U256 { | ||
T::DefaultBaseFeePerGas::get() | ||
} | ||
|
||
#[pallet::storage] | ||
pub type BaseFeePerGas<T> = StorageValue<_, U256, ValueQuery, DefaultBaseFeePerGas<T>>; | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
pub enum Event { | ||
NewBaseFeePerGas { fee: U256 }, | ||
} | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { | ||
fn on_initialize(_: T::BlockNumber) -> Weight { | ||
// TODO: benchmark this! | ||
let db_weight = <T as frame_system::Config>::DbWeight::get(); | ||
db_weight.reads_writes(2, 1) | ||
} | ||
|
||
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()); | ||
|
||
*base_fee_per_gas = U256::from(new_base_fee_per_gas) | ||
.clamp(T::MinBaseFeePerGas::get(), T::MaxBaseFeePerGas::get()); | ||
}) | ||
} | ||
} | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())] | ||
pub fn set_base_fee_per_gas(origin: OriginFor<T>, fee: U256) -> DispatchResult { | ||
ensure_root(origin)?; | ||
BaseFeePerGas::<T>::put(fee); | ||
Self::deposit_event(Event::NewBaseFeePerGas { fee }); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
impl<T: Config> fp_evm::FeeCalculator for Pallet<T> { | ||
fn min_gas_price() -> (U256, Weight) { | ||
(BaseFeePerGas::<T>::get(), T::DbWeight::get().reads(1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters