Skip to content

Commit

Permalink
Fee Alignment - Shibuya/local
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Sep 13, 2023
1 parent df73ca4 commit a7634fa
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 5 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions pallets/dynamic-evm-base-fee/Cargo.toml
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",
]
118 changes: 118 additions & 0 deletions pallets/dynamic-evm-base-fee/src/lib.rs
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))
}
}
11 changes: 6 additions & 5 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub const MICROSBY: Balance = 1_000_000_000_000;
pub const MILLISBY: Balance = 1_000 * MICROSBY;
pub const SBY: Balance = 1_000 * MILLISBY;

pub const STORAGE_BYTE_FEE: Balance = 100 * MICROSBY;
pub const STORAGE_BYTE_FEE: Balance = MICROSBY;

/// Charge fee for stored bytes and items.
pub const fn deposit(items: u32, bytes: u32) -> Balance {
Expand All @@ -117,7 +117,7 @@ pub const fn deposit(items: u32, bytes: u32) -> Balance {
///
/// TODO: using this requires storage migration (good to test on Shibuya first!)
pub const fn contracts_deposit(items: u32, bytes: u32) -> Balance {
items as Balance * 4 * MILLISBY + (bytes as Balance) * STORAGE_BYTE_FEE
items as Balance * 40 * MICROSBY + (bytes as Balance) * STORAGE_BYTE_FEE
}

/// Change this to adjust the block time.
Expand Down Expand Up @@ -735,11 +735,12 @@ impl WeightToFeePolynomial for WeightToFee {
pub struct DealWithFees;
impl OnUnbalanced<NegativeImbalance> for DealWithFees {
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {
if let Some(mut fees) = fees_then_tips.next() {
if let Some(fees) = fees_then_tips.next() {
// Burn 80% of fees, rest goes to collators, including 100% of the tips.
let (to_burn, mut collators) = fees.ration(80, 20);
if let Some(tips) = fees_then_tips.next() {
tips.merge_into(&mut fees);
tips.merge_into(&mut collators);
}
let (to_burn, collators) = fees.ration(20, 80);

// burn part of fees
drop(to_burn);
Expand Down

0 comments on commit a7634fa

Please sign in to comment.