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

feat: add builder style function to simulate payload args #1324

Merged
merged 1 commit into from
Sep 21, 2024
Merged
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
62 changes: 60 additions & 2 deletions crates/rpc-types-eth/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const MAX_SIMULATE_BLOCKS: u64 = 256;
/// Represents a batch of calls to be simulated sequentially within a block.
/// This struct includes block and state overrides as well as the transaction requests to be
/// executed.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct SimBlock {
Expand All @@ -24,6 +24,32 @@ pub struct SimBlock {
pub calls: Vec<TransactionRequest>,
}

impl SimBlock {
/// Enables state overrides
pub fn with_state_overrides(mut self, overrides: StateOverride) -> Self {
self.state_overrides = Some(overrides);
self
}

/// Enables block overrides
pub fn with_block_overrides(mut self, overrides: BlockOverrides) -> Self {
self.block_overrides = Some(overrides);
self
}

/// Adds a call to the block.
pub fn call(mut self, call: TransactionRequest) -> Self {
self.calls.push(call);
self
}

/// Adds multiple calls to the block.
pub fn extend_calls(mut self, calls: impl IntoIterator<Item = TransactionRequest>) -> Self {
self.calls.extend(calls);
self
}
}

/// Represents the result of simulating a block.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -62,7 +88,7 @@ pub struct SimCallResult {
///
/// This struct configures how simulations are executed, including whether to trace token transfers,
/// validate transaction sequences, and whether to return full transaction objects.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct SimulatePayload {
Expand All @@ -79,6 +105,38 @@ pub struct SimulatePayload {
pub return_full_transactions: bool,
}

impl SimulatePayload {
/// Adds a block to the simulation payload.
pub fn extend(mut self, block: SimBlock) -> Self {
self.block_state_calls.push(block);
self
}

/// Adds multiple blocks to the simulation payload.
pub fn extend_blocks(mut self, blocks: impl IntoIterator<Item = SimBlock>) -> Self {
self.block_state_calls.extend(blocks);
self
}

/// Enables tracing of token transfers.
pub const fn with_trace_transfers(mut self) -> Self {
self.trace_transfers = true;
self
}

/// Enables validation of the transaction sequence.
pub const fn with_validation(mut self) -> Self {
self.validation = true;
self
}

/// Enables returning full transactions.
pub const fn with_full_transactions(mut self) -> Self {
self.return_full_transactions = true;
self
}
}

/// The error response returned by the `eth_simulateV1` method.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down