Skip to content

Commit

Permalink
draft: allow simulations within a block
Browse files Browse the repository at this point in the history
  • Loading branch information
ape-dev-cs committed Jul 20, 2023
1 parent 64fe96a commit cd90045
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::sync::Arc;
use dashmap::mapref::one::RefMut;
use ethers::abi::{Address, Uint};
use ethers::core::types::Log;
use ethers::types::Bytes;
use ethers::providers::{Http, Middleware, Provider};
use ethers::types::{Bytes, H256};
use foundry_evm::CallKind;
use revm::interpreter::InstructionResult;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -37,6 +38,8 @@ pub struct SimulationRequest {
pub block_number: Option<u64>,
#[serde(rename = "formatTrace")]
pub format_trace: Option<bool>,
#[serde(rename = "transactionBlockIndex")]
pub transaction_block_index: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -208,7 +211,7 @@ pub async fn simulate_bundle(
.unwrap_or(chain_id_to_fork_url(first_chain_id)?);
let mut evm = Evm::new(
None,
fork_url,
fork_url.clone(),
first_block_number,
transactions[0].gas_limit,
true,
Expand Down Expand Up @@ -239,7 +242,65 @@ pub async fn simulate_bundle(
.await
.expect("Failed to set block timestamp");
}
response.push(run(&mut evm, transaction, true).await?);

if transaction.clone().transaction_block_index.is_some()
{
let provider = Provider::<Http>::try_from(&fork_url);
let pre_transactions = provider
.unwrap()
.get_block_with_txs(
transaction
.clone()
.block_number
.expect("Transaction has no block number"),
)
.await
.unwrap()
.unwrap();
let relevant_transactions: Vec<_> = pre_transactions
.transactions
.iter()
.map(|x| SimulationRequest {
chain_id: x.chain_id.unwrap().as_u64(),
from: x.from,
to: x.to.unwrap(),
value: Some(x.value.to_string()),
data: Some(x.input.clone()),
gas_limit: x.gas.as_u64(),
block_number: None,
format_trace: None,
transaction_block_index: None,
})
.collect();

let transaction_block_index = transaction.clone().transaction_block_index.unwrap();
let transactions_before_index = relevant_transactions
.iter()
.take(transaction_block_index as usize)
.collect::<Vec<_>>();

let transactions_after_index = relevant_transactions.iter().skip(
transaction_block_index
.checked_add(1)
.expect("Overflow")
as usize,
);

for before_tx in transactions_before_index {
let result = run(&mut evm, before_tx.clone(), true).await;
result.expect("Failed to run transactions in block prior to transaction index");
}

response.push(run(&mut evm, transaction, true).await?);

for after_tx in transactions_after_index {
let result = run(&mut evm, after_tx.clone(), true).await;
result.expect("Failed to run transactions in block after transaction index");
}

} else {
response.push(run(&mut evm, transaction, true).await?);
}
}

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

0 comments on commit cd90045

Please sign in to comment.