Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate on user provided timestamp #27

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading