Skip to content

Commit

Permalink
Simulate on user provided timestamp (#27)
Browse files Browse the repository at this point in the history
* Simulate on user provided timestamp

* Allow tests to set the required config
  • Loading branch information
MartinquaXD authored Dec 20, 2023
1 parent a38f77d commit ee12a28
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export type SimulationRequest = {
value: string;
accessList?: AccessListItem[];
blockNumber?: number; // if not specified, latest used,
blockTimestamp?: number; // if not specified, timestamp of latest block is used,
stateOverrides?: Record<string, StateOverride>;
formatTrace?: boolean;
};
Expand Down
22 changes: 21 additions & 1 deletion src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct SimulationRequest {
pub value: Option<PermissiveUint>,
pub access_list: Option<AccessList>,
pub block_number: Option<u64>,
pub block_timestamp: Option<u64>,
pub state_overrides: Option<HashMap<Address, StateOverride>>,
pub format_trace: Option<bool>,
}
Expand All @@ -60,6 +61,7 @@ pub struct StatefulSimulationRequest {
pub chain_id: u64,
pub gas_limit: u64,
pub block_number: Option<u64>,
pub block_timestamp: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -241,6 +243,12 @@ pub async fn simulate(transaction: SimulationRequest, config: Config) -> Result<
return Err(warp::reject::custom(IncorrectChainIdError()));
}

if let Some(timestamp) = transaction.block_timestamp {
evm.set_block_timestamp(timestamp)
.await
.expect("failed to set block timestamp");
}

let response = run(&mut evm, transaction, false).await?;

Ok(warp::reply::json(&response))
Expand All @@ -252,6 +260,7 @@ pub async fn simulate_bundle(
) -> Result<Json, Rejection> {
let first_chain_id = transactions[0].chain_id;
let first_block_number = transactions[0].block_number;
let first_block_timestamp = transactions[0].block_timestamp;

let fork_url = config
.fork_url
Expand All @@ -269,6 +278,12 @@ pub async fn simulate_bundle(
return Err(warp::reject::custom(IncorrectChainIdError()));
}

if let Some(timestamp) = first_block_timestamp {
evm.set_block_timestamp(timestamp)
.await
.expect("failed to set block timestamp");
}

let mut response = Vec::with_capacity(transactions.len());
for transaction in transactions {
if transaction.chain_id != first_chain_id {
Expand Down Expand Up @@ -303,14 +318,19 @@ pub async fn simulate_stateful_new(
let fork_url = config
.fork_url
.unwrap_or(chain_id_to_fork_url(stateful_simulation_request.chain_id)?);
let evm = Evm::new(
let mut evm = Evm::new(
None,
fork_url,
stateful_simulation_request.block_number,
stateful_simulation_request.gas_limit,
true,
config.etherscan_key,
);

if let Some(timestamp) = stateful_simulation_request.block_timestamp {
evm.set_block_timestamp(timestamp).await?;
}

let new_id = Uuid::new_v4();
state.evms.insert(new_id, Arc::new(Mutex::new(evm)));

Expand Down
Loading

0 comments on commit ee12a28

Please sign in to comment.