From 121b7bb1467256bf9ac52b086a3ac0db0023dba8 Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Mon, 13 Nov 2023 02:09:01 +0100 Subject: [PATCH] wip --- Cargo.lock | 3 ++ crates/cheatcodes/Cargo.toml | 2 + crates/cheatcodes/src/evm/fork.rs | 45 +++++++++++++--------- crates/cli/Cargo.toml | 1 + crates/cli/src/opts/wallet/multi_wallet.rs | 20 +++++----- crates/evm/core/src/fork/backend.rs | 13 +++++-- 6 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index adc7dde0b69b..9c1a90f1e5b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2760,6 +2760,8 @@ dependencies = [ "alloy-dyn-abi", "alloy-json-abi", "alloy-primitives", + "alloy-providers", + "alloy-rpc-types", "alloy-sol-types", "const-hex", "ethers-core", @@ -2798,6 +2800,7 @@ dependencies = [ "alloy-dyn-abi", "alloy-json-abi", "alloy-primitives", + "alloy-providers", "async-trait", "clap", "color-eyre", diff --git a/crates/cheatcodes/Cargo.toml b/crates/cheatcodes/Cargo.toml index aeae87e547c1..807696909970 100644 --- a/crates/cheatcodes/Cargo.toml +++ b/crates/cheatcodes/Cargo.toml @@ -23,6 +23,8 @@ alloy-dyn-abi.workspace = true alloy-json-abi.workspace = true alloy-primitives.workspace = true alloy-sol-types.workspace = true +alloy-providers.workspace = true +alloy-rpc-types.workspace = true ethers-core.workspace = true ethers-providers.workspace = true diff --git a/crates/cheatcodes/src/evm/fork.rs b/crates/cheatcodes/src/evm/fork.rs index 25b224182b1d..fbd444968182 100644 --- a/crates/cheatcodes/src/evm/fork.rs +++ b/crates/cheatcodes/src/evm/fork.rs @@ -1,8 +1,8 @@ use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Result, Vm::*}; -use alloy_primitives::B256; +use alloy_primitives::{FixedBytes, B256, U256}; +use alloy_providers::provider::TempProvider; +use alloy_rpc_types::{Filter, Topic}; use alloy_sol_types::SolValue; -use ethers_core::types::Filter; -use ethers_providers::Middleware; use foundry_common::ProviderBuilder; use foundry_compilers::utils::RuntimeOrHandle; use foundry_evm_core::fork::CreateFork; @@ -218,6 +218,8 @@ impl Cheatcode for isPersistentCall { impl Cheatcode for rpcCall { fn apply_full(&self, ccx: &mut CheatsCtxt) -> Result { + /* + todo what do we do here let Self { method, params } = self; let url = ccx.data.db.active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?; @@ -232,6 +234,8 @@ impl Cheatcode for rpcCall { .map_err(|err| fmt_err!("failed to parse result: {err}"))?; Ok(result_as_tokens.abi_encode()) + */ + Ok(1.abi_encode()) } } @@ -250,35 +254,38 @@ impl Cheatcode for eth_getLogsCall { let url = ccx.data.db.active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?; let provider = ProviderBuilder::new(&url).build()?; - let mut filter = - Filter::new().address(addr.to_ethers()).from_block(from_block).to_block(to_block); + let mut filter = Filter::new().address(*addr).from_block(from_block).to_block(to_block); for (i, topic) in topics.iter().enumerate() { let topic = topic.to_ethers(); + // todo: needed because rust wants to convert FixedBytes<32> to U256 to convert it back to FixedBytes<32> and then to Topic for some reason + // removing the From impl in alloy does not fix the situation, and it is not possible to impl From> either because of a conflicting impl match i { - 0 => filter = filter.topic0(topic), - 1 => filter = filter.topic1(topic), - 2 => filter = filter.topic2(topic), - 3 => filter = filter.topic3(topic), + 0 => filter = filter.event_signature(U256::from_be_bytes(topic.to_fixed_bytes())), + 1 => filter = filter.topic1(U256::from_be_bytes(topic.to_fixed_bytes())), + 2 => filter = filter.topic2(U256::from_be_bytes(topic.to_fixed_bytes())), + 3 => filter = filter.topic3(U256::from_be_bytes(topic.to_fixed_bytes())), _ => unreachable!(), }; } + // todo: handle the errors somehow let logs = RuntimeOrHandle::new() - .block_on(provider.get_logs(&filter)) - .map_err(|e| fmt_err!("eth_getLogs: {e}"))?; + .block_on(provider.get_logs(filter)) + .success() + .ok_or_else(|| eyre::eyre!("failed to get logs"))?; let eth_logs = logs .into_iter() .map(|log| EthGetLogs { - emitter: log.address.to_alloy(), - topics: log.topics.into_iter().map(ToAlloy::to_alloy).collect(), + emitter: log.address, + topics: log.topics.into_iter().collect(), data: log.data.0.into(), - blockHash: log.block_hash.unwrap_or_default().to_alloy(), - blockNumber: log.block_number.unwrap_or_default().to_alloy().to(), - transactionHash: log.transaction_hash.unwrap_or_default().to_alloy(), - transactionIndex: log.transaction_index.unwrap_or_default().to_alloy().to(), - logIndex: log.log_index.unwrap_or_default().to_alloy(), - removed: log.removed.unwrap_or(false), + blockHash: log.block_hash.unwrap_or_default(), + blockNumber: log.block_number.unwrap_or_default().to(), + transactionHash: log.transaction_hash.unwrap_or_default(), + transactionIndex: log.transaction_index.unwrap_or_default().to(), + logIndex: log.log_index.unwrap_or_default(), + removed: log.removed, }) .collect::>(); diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 08432493f925..7b22e5207627 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -22,6 +22,7 @@ foundry-compilers = { workspace = true, features = ["full"] } alloy-dyn-abi.workspace = true alloy-json-abi.workspace = true alloy-primitives.workspace = true +alloy-providers.workspace = true ethers-core.workspace = true ethers-providers.workspace = true diff --git a/crates/cli/src/opts/wallet/multi_wallet.rs b/crates/cli/src/opts/wallet/multi_wallet.rs index 701a55dd45ae..aa4e7792623a 100644 --- a/crates/cli/src/opts/wallet/multi_wallet.rs +++ b/crates/cli/src/opts/wallet/multi_wallet.rs @@ -1,7 +1,7 @@ use super::{WalletSigner, WalletTrait}; use alloy_primitives::Address; +use alloy_providers::provider::TempProvider; use clap::Parser; -use ethers_providers::Middleware; use ethers_signers::{ AwsSigner, HDPath as LedgerHDPath, Ledger, LocalWallet, Signer, Trezor, TrezorHDPath, }; @@ -220,7 +220,7 @@ impl MultiWallet { script_wallets: &[LocalWallet], ) -> Result> { println!("\n###\nFinding wallets for all the necessary addresses..."); - let chain = provider.get_chainid().await?.as_u64(); + let chain = provider.get_chain_id().await?.as_u64(); let mut local_wallets = HashMap::new(); let mut unused_wallets = vec![]; @@ -246,7 +246,7 @@ impl MultiWallet { local_wallets.insert(address.to_alloy(), signer); if addresses.is_empty() { - return Ok(local_wallets) + return Ok(local_wallets); } } else { // Just to show on error. @@ -277,7 +277,7 @@ impl MultiWallet { for _ in 0..self.interactives { wallets.push(self.get_from_interactive()?); } - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -288,7 +288,7 @@ impl MultiWallet { for private_key in private_keys.iter() { wallets.push(self.get_from_private_key(private_key.trim())?); } - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -324,7 +324,7 @@ impl MultiWallet { let wallet = self.get_from_keystore(Some(&path), passwords_iter.next().as_ref(), password_files_iter.next().as_ref())?.wrap_err("Keystore paths do not have the same length as provided passwords or password files.")?; wallets.push(wallet); } - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -359,7 +359,7 @@ impl MultiWallet { mnemonic_index, )?) } - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -376,7 +376,7 @@ impl MultiWallet { } create_hw_wallets!(args, chain_id, get_from_ledger, wallets); - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -384,7 +384,7 @@ impl MultiWallet { pub async fn trezors(&self, chain_id: u64) -> Result>> { if self.trezor { create_hw_wallets!(self, chain_id, get_from_trezor, wallets); - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } @@ -406,7 +406,7 @@ impl MultiWallet { wallets.push(aws_signer) } - return Ok(Some(wallets)) + return Ok(Some(wallets)); } Ok(None) } diff --git a/crates/evm/core/src/fork/backend.rs b/crates/evm/core/src/fork/backend.rs index c21e07a3c479..e9bd72c3fe91 100644 --- a/crates/evm/core/src/fork/backend.rs +++ b/crates/evm/core/src/fork/backend.rs @@ -244,7 +244,7 @@ where let provider = self.provider.clone(); let fut = Box::pin(async move { let block = provider - .get_block_by_number(number.into(), true) + .get_block(number, true) .await .success() .ok_or_else(|| eyre::eyre!("could not fetch block {number:?}")); @@ -280,24 +280,29 @@ where entry.insert(vec![listener]); let provider = self.provider.clone(); let fut = Box::pin(async move { - let block = provider.get_block_by_number(number, false).await; + let block = provider + .get_block_by_number(number, false) + .await + .success() + .ok_or_else(|| eyre::eyre!("failed to get block")); let block_hash = match block { Ok(Some(block)) => Ok(block + .header .hash .expect("empty block hash on mined block, this should never happen")), Ok(None) => { warn!(target: "backendhandler", ?number, "block not found"); // if no block was returned then the block does not exist, in which case // we return empty hash - Ok(KECCAK_EMPTY.to_ethers()) + Ok(KECCAK_EMPTY) } Err(err) => { error!(target: "backendhandler", ?err, ?number, "failed to get block"); Err(err) } }; - (block_hash.map(|h| h.to_alloy()), number) + (block_hash, number) }); self.pending_requests.push(ProviderRequest::BlockHash(fut)); }