Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Nov 13, 2023
1 parent 4182c3f commit 121b7bb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 26 additions & 19 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -218,6 +218,8 @@ impl Cheatcode for isPersistentCall {

impl Cheatcode for rpcCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> 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"))?;
Expand All @@ -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())
}
}

Expand All @@ -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<U256> impl in alloy does not fix the situation, and it is not possible to impl From<FixedBytes<32>> 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::<Vec<_>>();

Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions crates/cli/src/opts/wallet/multi_wallet.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -220,7 +220,7 @@ impl MultiWallet {
script_wallets: &[LocalWallet],
) -> Result<HashMap<Address, WalletSigner>> {
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![];
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -359,7 +359,7 @@ impl MultiWallet {
mnemonic_index,
)?)
}
return Ok(Some(wallets))
return Ok(Some(wallets));
}
Ok(None)
}
Expand All @@ -376,15 +376,15 @@ impl MultiWallet {
}

create_hw_wallets!(args, chain_id, get_from_ledger, wallets);
return Ok(Some(wallets))
return Ok(Some(wallets));
}
Ok(None)
}

pub async fn trezors(&self, chain_id: u64) -> Result<Option<Vec<Trezor>>> {
if self.trezor {
create_hw_wallets!(self, chain_id, get_from_trezor, wallets);
return Ok(Some(wallets))
return Ok(Some(wallets));
}
Ok(None)
}
Expand All @@ -406,7 +406,7 @@ impl MultiWallet {
wallets.push(aws_signer)
}

return Ok(Some(wallets))
return Ok(Some(wallets));
}
Ok(None)
}
Expand Down
13 changes: 9 additions & 4 deletions crates/evm/core/src/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"));
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 121b7bb

Please sign in to comment.