alloy_provider/provider/eth_call/
params.rs

1use alloy_eips::BlockId;
2use alloy_network::Network;
3use alloy_rpc_types_eth::{
4    state::StateOverride, BlockOverrides, Bundle, StateContext, TransactionIndex,
5};
6use serde::ser::SerializeSeq;
7use std::borrow::Cow;
8
9/// The parameters for an `"eth_call"` RPC request.
10#[derive(Clone, Debug)]
11pub struct EthCallParams<N: Network> {
12    data: N::TransactionRequest,
13    pub(crate) block: Option<BlockId>,
14    pub(crate) overrides: Option<StateOverride>,
15    pub(crate) block_overrides: Option<BlockOverrides>,
16}
17
18impl<N> EthCallParams<N>
19where
20    N: Network,
21{
22    /// Instantiates a new `EthCallParams` with the given data (transaction).
23    pub const fn new(data: N::TransactionRequest) -> Self {
24        Self { data, block: None, overrides: None, block_overrides: None }
25    }
26
27    /// Sets the block to use for this call.
28    pub const fn with_block(mut self, block: BlockId) -> Self {
29        self.block = Some(block);
30        self
31    }
32
33    /// Sets the state overrides for this call.
34    pub fn with_overrides(mut self, overrides: StateOverride) -> Self {
35        self.overrides = Some(overrides);
36        self
37    }
38
39    /// Sets the state overrides for this call, if any.
40    pub fn with_overrides_opt(mut self, overrides: Option<StateOverride>) -> Self {
41        self.overrides = overrides;
42        self
43    }
44
45    /// Returns a reference to the state overrides if set.
46    pub const fn overrides(&self) -> Option<&StateOverride> {
47        self.overrides.as_ref()
48    }
49
50    /// Returns a reference to the transaction data.
51    pub const fn data(&self) -> &N::TransactionRequest {
52        &self.data
53    }
54
55    /// Consumes the `EthCallParams` and returns the transaction data.
56    pub fn into_data(self) -> N::TransactionRequest {
57        self.data
58    }
59
60    /// Returns the block.
61    pub const fn block(&self) -> Option<BlockId> {
62        self.block
63    }
64
65    /// Sets the block overrides for this call.
66    pub fn with_block_overrides(mut self, overrides: BlockOverrides) -> Self {
67        self.block_overrides = Some(overrides);
68        self
69    }
70
71    /// Sets the block overrides for this call, if any.
72    pub fn with_block_overrides_opt(mut self, overrides: Option<BlockOverrides>) -> Self {
73        self.block_overrides = overrides;
74        self
75    }
76
77    /// Returns a reference to the block overrides if set.
78    pub const fn block_overrides(&self) -> Option<&BlockOverrides> {
79        self.block_overrides.as_ref()
80    }
81}
82
83impl<N: Network> serde::Serialize for EthCallParams<N> {
84    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
85        let len = if self.block_overrides().is_some() {
86            4
87        } else if self.overrides().is_some() {
88            3
89        } else {
90            2
91        };
92
93        let mut seq = serializer.serialize_seq(Some(len))?;
94        seq.serialize_element(&self.data())?;
95
96        if let Some(block_overrides) = self.block_overrides() {
97            seq.serialize_element(&self.block().unwrap_or_default())?;
98            seq.serialize_element(self.overrides().unwrap_or(&StateOverride::default()))?;
99            seq.serialize_element(block_overrides)?;
100        } else if let Some(overrides) = self.overrides() {
101            seq.serialize_element(&self.block().unwrap_or_default())?;
102            seq.serialize_element(overrides)?;
103        } else if let Some(block) = self.block() {
104            seq.serialize_element(&block)?;
105        }
106
107        seq.end()
108    }
109}
110
111/// The parameters for an `"eth_callMany"` RPC request.
112#[derive(Clone, Debug)]
113pub struct EthCallManyParams<'req> {
114    bundles: Cow<'req, [Bundle]>,
115    context: Option<StateContext>,
116    overrides: Option<Cow<'req, StateOverride>>,
117}
118
119impl<'req> EthCallManyParams<'req> {
120    /// Instantiates a new `EthCallManyParams` with the given bundles.
121    pub const fn new(bundles: &'req [Bundle]) -> Self {
122        Self { bundles: Cow::Borrowed(bundles), context: None, overrides: None }
123    }
124
125    /// Sets the block in the [`StateContext`] for this call.
126    pub fn with_block(mut self, block: BlockId) -> Self {
127        let mut context = self.context.unwrap_or_default();
128        context.block_number = Some(block);
129        self.context = Some(context);
130        self
131    }
132
133    /// Sets the transaction index in the [`StateContext`] for this call.
134    pub fn with_transaction_index(mut self, tx_index: TransactionIndex) -> Self {
135        let mut context = self.context.unwrap_or_default();
136        context.transaction_index = Some(tx_index);
137        self.context = Some(context);
138        self
139    }
140
141    /// Sets the state context for this call.
142    pub const fn with_context(mut self, context: StateContext) -> Self {
143        self.context = Some(context);
144        self
145    }
146
147    /// Sets the state overrides for this call.
148    pub fn with_overrides(mut self, overrides: &'req StateOverride) -> Self {
149        self.overrides = Some(Cow::Borrowed(overrides));
150        self
151    }
152
153    /// Returns a reference to the state context if set.
154    pub const fn context(&self) -> Option<&StateContext> {
155        self.context.as_ref()
156    }
157
158    /// Returns a reference to the bundles.
159    pub fn bundles(&self) -> &[Bundle] {
160        &self.bundles
161    }
162
163    /// Returns a mutable reference to the bundles.
164    pub fn bundles_mut(&mut self) -> &mut Vec<Bundle> {
165        Cow::to_mut(&mut self.bundles)
166    }
167
168    /// Returns a reference to the state overrides if set.
169    pub fn overrides(&self) -> Option<&StateOverride> {
170        self.overrides.as_deref()
171    }
172
173    /// Clones the bundles, context, and overrides into owned data.
174    pub fn into_owned(self) -> EthCallManyParams<'static> {
175        EthCallManyParams {
176            bundles: Cow::Owned(self.bundles.into_owned()),
177            context: self.context,
178            overrides: self.overrides.map(|o| Cow::Owned(o.into_owned())),
179        }
180    }
181}
182
183impl serde::Serialize for EthCallManyParams<'_> {
184    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
185        let len = if self.overrides().is_some() { 3 } else { 2 };
186
187        let mut seq = serializer.serialize_seq(Some(len))?;
188        seq.serialize_element(&self.bundles())?;
189
190        if let Some(context) = self.context() {
191            seq.serialize_element(context)?;
192        }
193
194        if let Some(overrides) = self.overrides() {
195            seq.serialize_element(overrides)?;
196        }
197
198        seq.end()
199    }
200}