Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(anvil): extend alloy types until entry lib, cleanup warnings #6413

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::{
eth::subscription::SubscriptionId,
types::{EvmMineOptions, Forking, Index},
};
use alloy_primitives::{Address, Bytes, TxHash, B256, B64};
use alloy_primitives::{Address, Bytes, TxHash, B256, B64, U256};
use alloy_rpc_types::{
pubsub::{Params as SubscriptionParams, SubscriptionKind},
state::StateOverride,
BlockId, BlockNumberOrTag as BlockNumber, CallRequest, Filter, TransactionRequest,
};
use ethers_core::types::{transaction::eip712::TypedData, GethDebugTracingOptions, U256};
use ethers_core::types::{transaction::eip712::TypedData, GethDebugTracingOptions};

pub mod block;
pub mod proof;
Expand All @@ -23,10 +23,10 @@ pub mod state;
pub mod serde_helpers;

#[cfg(feature = "serde")]
use ethers_core::types::serde_helpers::{deserialize_number, deserialize_number_opt, deserialize_number_seq};
use self::serde_helpers::*;

#[cfg(feature = "serde")]
use self::serde_helpers::*;
use foundry_common::serde_helpers::{deserialize_number, deserialize_number_opt, deserialize_number_seq};

/// Wrapper type that ensures the type is named `params`
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -1377,7 +1377,6 @@ true}]}"#; let value: serde_json::Value = serde_json::from_str(s).unwrap
}

#[test]
#[test]
fn test_serde_debug_trace_call() {
let s = r#"{"method": "debug_traceCall", "params": [{"data":"0xcfae3217","from":"0xd84de507f3fada7df80908082d3239466db55a71","to":"0xcbe828fdc46e3b1c351ec90b1a5e7d9742c0398d"}]}"#;
let value: serde_json::Value = serde_json::from_str(s).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions crates/anvil/core/src/eth/transaction/ethers_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::eth::{
};
use alloy_primitives::{U128 as rU128, U256 as rU256, U64 as rU64};
use alloy_rpc_types::{
AccessList as AlloyAccessList, AccessListItem as AlloyAccessListItem, CallRequest,
EIP1186StorageProof, Signature, Transaction as AlloyTransaction,
AccessList as AlloyAccessList, CallRequest, Signature, Transaction as AlloyTransaction,
TransactionRequest as AlloyTransactionRequest, state::{StateOverride, AccountOverride as AlloyAccountOverride},
};
use ethers_core::types::{
Expand Down
12 changes: 7 additions & 5 deletions crates/anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::{
};
use anvil_server::ServerConfig;
use clap::Parser;
use foundry_utils::types::ToAlloy;
use core::fmt;
use ethers::{
signers::coins_bip39::{English, Mnemonic},
utils::WEI_IN_ETHER,
};
use alloy_primitives::U256;
use foundry_config::{Chain, Config};
use futures::FutureExt;
use rand::{rngs::StdRng, SeedableRng};
Expand Down Expand Up @@ -192,14 +194,14 @@ impl NodeArgs {
};

NodeConfig::default()
.with_gas_limit(self.evm_opts.gas_limit)
.with_gas_limit(self.evm_opts.gas_limit.map(|g| U256::from(g)))
.disable_block_gas_limit(self.evm_opts.disable_block_gas_limit)
.with_gas_price(self.evm_opts.gas_price)
.with_gas_price(self.evm_opts.gas_price.map(|g| U256::from(g)))
.with_hardfork(self.hardfork)
.with_blocktime(self.block_time.map(Duration::from_secs))
.with_no_mining(self.no_mining)
.with_account_generator(self.account_generator())
.with_genesis_balance(genesis_balance)
.with_genesis_balance(genesis_balance.to_alloy())
.with_genesis_timestamp(self.timestamp)
.with_port(self.port)
.with_fork_block_number(
Expand All @@ -208,13 +210,13 @@ impl NodeArgs {
.or_else(|| self.evm_opts.fork_url.as_ref().and_then(|f| f.block)),
)
.with_fork_headers(self.evm_opts.fork_headers)
.with_fork_chain_id(self.evm_opts.fork_chain_id.map(u64::from))
.with_fork_chain_id(self.evm_opts.fork_chain_id.map(u64::from).map(|c| U256::from(c)))
Evalir marked this conversation as resolved.
Show resolved Hide resolved
.fork_request_timeout(self.evm_opts.fork_request_timeout.map(Duration::from_millis))
.fork_request_retries(self.evm_opts.fork_request_retries)
.fork_retry_backoff(self.evm_opts.fork_retry_backoff.map(Duration::from_millis))
.fork_compute_units_per_second(compute_units_per_second)
.with_eth_rpc_url(self.evm_opts.fork_url.map(|fork| fork.url))
.with_base_fee(self.evm_opts.block_base_fee_per_gas)
.with_base_fee(self.evm_opts.block_base_fee_per_gas.map(|f| U256::from(f)))
.with_storage_caching(self.evm_opts.no_storage_caching)
.with_server_config(self.server_config)
.with_host(self.host)
Expand Down
44 changes: 22 additions & 22 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::{
mem::in_memory_db::MemDb,
FeeManager, Hardfork,
};
use alloy_primitives::U64;
use alloy_primitives::U256;
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::BlockNumberOrTag;
use alloy_transport::TransportError;
use anvil_server::ServerConfig;
use ethers::{
core::k256::ecdsa::SigningKey,
prelude::{rand::thread_rng, Wallet, U256},
prelude::{rand::thread_rng, Wallet},
signers::{
coins_bip39::{English, Mnemonic},
MnemonicBuilder, Signer,
Expand All @@ -38,7 +38,7 @@ use foundry_evm::{
constants::DEFAULT_CREATE2_DEPLOYER,
fork::{BlockchainDb, BlockchainDbMeta, SharedBackend},
revm,
revm::primitives::{BlockEnv, CfgEnv, SpecId, TxEnv, U256 as rU256},
revm::primitives::{BlockEnv, CfgEnv, SpecId, TxEnv},
utils::apply_chain_and_block_specific_env_changes,
};
use foundry_utils::types::{ToAlloy, ToEthers};
Expand Down Expand Up @@ -193,7 +193,7 @@ Available Accounts
==================
"#
);
let balance = format_ether(self.genesis_balance);
let balance = format_ether(self.genesis_balance.to_ethers());
for (idx, wallet) in self.genesis_accounts.iter().enumerate() {
let _ = write!(
config_string,
Expand Down Expand Up @@ -376,7 +376,7 @@ impl Default for NodeConfig {
genesis_timestamp: None,
genesis_accounts,
// 100ETH default balance
genesis_balance: WEI_IN_ETHER.saturating_mul(100u64.into()),
genesis_balance: WEI_IN_ETHER.to_alloy().saturating_mul(U256::from(100u64)),
block_time: None,
no_mining: false,
port: NODE_PORT,
Expand Down Expand Up @@ -418,12 +418,12 @@ impl NodeConfig {
pub fn get_base_fee(&self) -> U256 {
self.base_fee
.or_else(|| self.genesis.as_ref().and_then(|g| g.base_fee_per_gas))
.unwrap_or_else(|| INITIAL_BASE_FEE.into())
.unwrap_or_else(|| U256::from(INITIAL_BASE_FEE))
}

/// Returns the base fee to use
pub fn get_gas_price(&self) -> U256 {
self.gas_price.unwrap_or_else(|| INITIAL_GAS_PRICE.into())
self.gas_price.unwrap_or_else(|| U256::from(INITIAL_GAS_PRICE))
}

/// Returns the base fee to use
Expand Down Expand Up @@ -473,7 +473,7 @@ impl NodeConfig {

/// Sets the gas limit
#[must_use]
pub fn with_gas_limit<U: Into<U256>>(mut self, gas_limit: Option<U>) -> Self {
pub fn with_gas_limit(mut self, gas_limit: Option<U256>) -> Self {
if let Some(gas_limit) = gas_limit {
self.gas_limit = gas_limit.into();
}
Expand All @@ -491,7 +491,7 @@ impl NodeConfig {

/// Sets the gas price
#[must_use]
pub fn with_gas_price<U: Into<U256>>(mut self, gas_price: Option<U>) -> Self {
pub fn with_gas_price(mut self, gas_price: Option<U256>) -> Self {
self.gas_price = gas_price.map(Into::into);
self
}
Expand All @@ -515,7 +515,7 @@ impl NodeConfig {

/// Sets the base fee
#[must_use]
pub fn with_base_fee<U: Into<U256>>(mut self, base_fee: Option<U>) -> Self {
pub fn with_base_fee(mut self, base_fee: Option<U256>) -> Self {
self.base_fee = base_fee.map(Into::into);
self
}
Expand Down Expand Up @@ -660,7 +660,7 @@ impl NodeConfig {

/// Sets the `fork_chain_id` to use to fork off local cache from
#[must_use]
pub fn with_fork_chain_id<U: Into<U256>>(mut self, fork_chain_id: Option<U>) -> Self {
pub fn with_fork_chain_id(mut self, fork_chain_id: Option<U256>) -> Self {
self.fork_chain_id = fork_chain_id.map(Into::into);
self
}
Expand Down Expand Up @@ -807,13 +807,13 @@ impl NodeConfig {
let mut env = revm::primitives::Env {
cfg,
block: BlockEnv {
gas_limit: self.gas_limit.to_alloy(),
basefee: self.get_base_fee().to_alloy(),
gas_limit: self.gas_limit,
basefee: self.get_base_fee(),
..Default::default()
},
tx: TxEnv { chain_id: self.get_chain_id().into(), ..Default::default() },
};
let fees = FeeManager::new(env.cfg.spec_id, self.get_base_fee(), self.get_gas_price());
let fees = FeeManager::new(env.cfg.spec_id, self.get_base_fee().to_ethers(), self.get_gas_price().to_ethers());

let (db, fork): (Arc<tokio::sync::RwLock<Box<dyn Db>>>, Option<ClientFork>) =
if let Some(eth_rpc_url) = self.eth_rpc_url.clone() {
Expand All @@ -829,8 +829,8 @@ impl NodeConfig {

let genesis = GenesisConfig {
timestamp: self.get_genesis_timestamp(),
balance: self.genesis_balance.to_alloy(),
accounts: self.genesis_accounts.iter().map(|acc| acc.address()).collect(),
balance: self.genesis_balance,
accounts: self.genesis_accounts.iter().map(|acc| acc.address().to_alloy()).collect(),
fork_genesis_account_infos: Arc::new(Default::default()),
genesis_init: self.genesis.clone(),
};
Expand Down Expand Up @@ -934,7 +934,7 @@ impl NodeConfig {
.expect("Failed to fetch network chain id")
.to::<u64>(),
);
if chain_id == ethers::types::Chain::Mainnet.into() {
if chain_id.to_ethers() == ethers::types::Chain::Mainnet.into() {
let hardfork: Hardfork = fork_block_number.into();
env.cfg.spec_id = hardfork.into();
self.hardfork = Some(hardfork);
Expand Down Expand Up @@ -981,13 +981,13 @@ latest block number: {latest_block}"
// limit is enabled, since there are networks where this is not used and is always
// `0x0` which would inevitably result in `OutOfGas` errors as soon as the evm is about to record gas, See also <https://github.com/foundry-rs/foundry/issues/3247>
let gas_limit = if self.disable_block_gas_limit || block.header.gas_limit.is_zero() {
rU256::from(u64::MAX)
U256::from(u64::MAX)
} else {
block.header.gas_limit
};

env.block = BlockEnv {
number: rU256::from(fork_block_number),
number: U256::from(fork_block_number),
timestamp: block.header.timestamp,
difficulty: block.header.difficulty,
// ensures prevrandao is set
Expand All @@ -1005,7 +1005,7 @@ latest block number: {latest_block}"
// 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.header.base_fee_per_gas {
self.base_fee = Some(base_fee.to_ethers());
self.base_fee = Some(base_fee);
env.block.basefee = base_fee;
// this is the base fee of the current block, but we need the base fee of
// the next block
Expand All @@ -1022,7 +1022,7 @@ latest block number: {latest_block}"
// use remote gas price
if self.gas_price.is_none() {
if let Ok(gas_price) = provider.get_gas_price().await {
self.gas_price = Some(gas_price.to_ethers());
self.gas_price = Some(gas_price);
fees.set_gas_price(gas_price.to_ethers());
}
}
Expand All @@ -1033,7 +1033,7 @@ latest block number: {latest_block}"
chain_id
} else {
let chain_id = if let Some(fork_chain_id) = fork_chain_id {
fork_chain_id.as_u64()
fork_chain_id.to::<u64>()
} else {
provider.get_chain_id().await.unwrap().to::<u64>()
};
Expand Down
Loading
Loading