Skip to content

Commit

Permalink
Get fork url from evm instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ape-dev-cs committed Jul 26, 2023
1 parent 28a2139 commit 1eb842b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ethers::abi::{Address, Uint};
use ethers::core::types::Log;
use ethers::types::Bytes;
use foundry_config::Chain;
use foundry_evm::executor::backend::DatabaseExt;
use foundry_evm::executor::{fork::CreateFork, Executor};
use foundry_evm::executor::{opts::EvmOpts, Backend, ExecutorBuilder};
use foundry_evm::trace::identifier::{EtherscanIdentifier, SignaturesIdentifier};
Expand Down Expand Up @@ -225,4 +226,8 @@ impl Evm {
pub fn get_chain_id(&self) -> Uint {
self.executor.env().cfg.chain_id.into()
}

pub fn get_fork_url(&self) -> Option<String> {
self.executor.backend().active_fork_url().into()
}
}
18 changes: 15 additions & 3 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ethers::abi::{Address, Uint};
use ethers::core::types::Log;
use ethers::providers::{Http, Middleware, Provider};
use ethers::types::{Bytes};
use eyre::anyhow;
use foundry_evm::CallKind;
use revm::interpreter::InstructionResult;
use revm::primitives::bitvec::macros::internal::funty::Fundamental;
Expand Down Expand Up @@ -184,7 +185,7 @@ pub async fn simulate(transaction: SimulationRequest, config: Config) -> Result<
.unwrap_or(chain_id_to_fork_url(transaction.chain_id)?);
let mut evm = Evm::new(
None,
fork_url,
fork_url.clone(),
transaction.block_number,
transaction.gas_limit,
true,
Expand All @@ -195,7 +196,14 @@ pub async fn simulate(transaction: SimulationRequest, config: Config) -> Result<
return Err(warp::reject::custom(IncorrectChainIdError()));
}

let response = run(&mut evm, transaction, false).await?;
let response: SimulationResponse = if transaction.transaction_block_index.is_some() {
let mut arr_resp = Vec::with_capacity(1);
apply_block_transactions(&fork_url, &transaction, &mut evm, &mut arr_resp).await?;
arr_resp.pop().ok_or_else(|| anyhow!("No simulated transaction")).unwrap()
} else {
run(&mut evm, transaction, false).await?
};


Ok(warp::reply::json(&response))
}
Expand Down Expand Up @@ -386,7 +394,11 @@ pub async fn simulate_stateful(
.await
.expect("Failed to set block timestamp");
}
response.push(run(&mut evm, transaction, true).await?);
if transaction.clone().transaction_block_index.is_some() {
apply_block_transactions(&evm.get_fork_url().expect("No fork URL"), &transaction, &mut evm, &mut response).await?;
} else {
response.push(run(&mut evm, transaction, true).await?);
}
}

Ok(warp::reply::json(&response))
Expand Down

0 comments on commit 1eb842b

Please sign in to comment.