Skip to content

Commit

Permalink
fix(anvil): use base fee of fork by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jun 7, 2022
1 parent 07b3520 commit fe73020
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct NodeConfig {
/// Default gas price for all txs
pub gas_price: U256,
/// Default base fee
pub base_fee: U256,
pub base_fee: Option<U256>,
/// The hardfork to use
pub hardfork: Hardfork,
/// Signer accounts that will be initialised with `genesis_balance` in the genesis block
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Default for NodeConfig {
eth_rpc_url: None,
fork_block_number: None,
account_generator: None,
base_fee: INITIAL_BASE_FEE.into(),
base_fee: None,
enable_tracing: true,
no_storage_caching: false,
server_config: Default::default(),
Expand All @@ -157,6 +157,11 @@ impl NodeConfig {
Self::default()
}

/// Returns the base fee to use
pub fn get_base_fee(&self) -> U256 {
self.base_fee.unwrap_or_else(|| INITIAL_BASE_FEE.into())
}

/// Sets the chain ID
#[must_use]
pub fn with_chain_id<U: Into<u64>>(mut self, chain_id: U) -> Self {
Expand Down Expand Up @@ -196,9 +201,7 @@ impl NodeConfig {
/// Sets the base fee
#[must_use]
pub fn with_base_fee<U: Into<U256>>(mut self, base_fee: Option<U>) -> Self {
if let Some(base_fee) = base_fee {
self.base_fee = base_fee.into();
}
self.base_fee = base_fee.map(Into::into);
self
}

Expand Down Expand Up @@ -375,7 +378,7 @@ Base Fee
==================
{}
"#,
Paint::green(format!("{}", self.base_fee))
Paint::green(format!("{}", self.get_base_fee()))
);
print!(
r#"
Expand Down Expand Up @@ -443,12 +446,12 @@ Chain ID: {}
},
block: BlockEnv {
gas_limit: self.gas_limit,
basefee: self.base_fee,
basefee: self.get_base_fee(),
..Default::default()
},
tx: TxEnv { chain_id: Some(self.chain_id), ..Default::default() },
};
let fees = FeeManager::new(self.base_fee, self.gas_price);
let fees = FeeManager::new(self.get_base_fee(), self.gas_price);
let mut fork_timestamp = None;

let (db, fork): (Arc<RwLock<dyn Db>>, Option<ClientFork>) = if let Some(eth_rpc_url) =
Expand All @@ -475,6 +478,15 @@ Chain ID: {}
env.block.number = fork_block_number.into();
fork_timestamp = Some(block.timestamp);

// if not set explicitly we use the base fee of the latest block
if self.base_fee.is_none() {
if let Some(base_fee) = block.base_fee_per_gas {
self.base_fee = Some(base_fee);
fees.set_base_fee(base_fee);
env.block.basefee = base_fee;
}
}

let block_hash = block.hash.unwrap();
let chain_id = provider.get_chainid().await.unwrap().as_u64();
// need to update the dev signers and env with the chain id
Expand Down

0 comments on commit fe73020

Please sign in to comment.