From d3293d6863c9a134776818e2d0ec763ad33c4b57 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 21 Sep 2024 06:35:10 +0200 Subject: [PATCH] feat: add builder style function to simulate payload args --- crates/rpc-types-eth/src/simulate.rs | 62 +++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/crates/rpc-types-eth/src/simulate.rs b/crates/rpc-types-eth/src/simulate.rs index 798f2ac0870..ca5428f1822 100644 --- a/crates/rpc-types-eth/src/simulate.rs +++ b/crates/rpc-types-eth/src/simulate.rs @@ -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 { @@ -24,6 +24,32 @@ pub struct SimBlock { pub calls: Vec, } +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) -> 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))] @@ -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 { @@ -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) -> 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))]