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 if self.block().is_some() {
90            2
91        } else {
92            1
93        };
94
95        let mut seq = serializer.serialize_seq(Some(len))?;
96        seq.serialize_element(&self.data())?;
97
98        if let Some(block_overrides) = self.block_overrides() {
99            seq.serialize_element(&self.block().unwrap_or_default())?;
100            seq.serialize_element(self.overrides().unwrap_or(&StateOverride::default()))?;
101            seq.serialize_element(block_overrides)?;
102        } else if let Some(overrides) = self.overrides() {
103            seq.serialize_element(&self.block().unwrap_or_default())?;
104            seq.serialize_element(overrides)?;
105        } else if let Some(block) = self.block() {
106            seq.serialize_element(&block)?;
107        }
108
109        seq.end()
110    }
111}
112
113/// The parameters for an `"eth_callMany"` RPC request.
114#[derive(Clone, Debug)]
115pub struct EthCallManyParams<'req> {
116    bundles: Cow<'req, [Bundle]>,
117    context: Option<StateContext>,
118    overrides: Option<Cow<'req, StateOverride>>,
119}
120
121impl<'req> EthCallManyParams<'req> {
122    /// Instantiates a new `EthCallManyParams` with the given bundles.
123    pub const fn new(bundles: &'req [Bundle]) -> Self {
124        Self { bundles: Cow::Borrowed(bundles), context: None, overrides: None }
125    }
126
127    /// Sets the block in the [`StateContext`] for this call.
128    pub fn with_block(mut self, block: BlockId) -> Self {
129        let mut context = self.context.unwrap_or_default();
130        context.block_number = Some(block);
131        self.context = Some(context);
132        self
133    }
134
135    /// Sets the transaction index in the [`StateContext`] for this call.
136    pub fn with_transaction_index(mut self, tx_index: TransactionIndex) -> Self {
137        let mut context = self.context.unwrap_or_default();
138        context.transaction_index = Some(tx_index);
139        self.context = Some(context);
140        self
141    }
142
143    /// Sets the state context for this call.
144    pub const fn with_context(mut self, context: StateContext) -> Self {
145        self.context = Some(context);
146        self
147    }
148
149    /// Sets the state overrides for this call.
150    pub fn with_overrides(mut self, overrides: &'req StateOverride) -> Self {
151        self.overrides = Some(Cow::Borrowed(overrides));
152        self
153    }
154
155    /// Returns a reference to the state context if set.
156    pub const fn context(&self) -> Option<&StateContext> {
157        self.context.as_ref()
158    }
159
160    /// Returns a reference to the bundles.
161    pub fn bundles(&self) -> &[Bundle] {
162        &self.bundles
163    }
164
165    /// Returns a mutable reference to the bundles.
166    pub fn bundles_mut(&mut self) -> &mut Vec<Bundle> {
167        Cow::to_mut(&mut self.bundles)
168    }
169
170    /// Returns a reference to the state overrides if set.
171    pub fn overrides(&self) -> Option<&StateOverride> {
172        self.overrides.as_deref()
173    }
174
175    /// Clones the bundles, context, and overrides into owned data.
176    pub fn into_owned(self) -> EthCallManyParams<'static> {
177        EthCallManyParams {
178            bundles: Cow::Owned(self.bundles.into_owned()),
179            context: self.context,
180            overrides: self.overrides.map(|o| Cow::Owned(o.into_owned())),
181        }
182    }
183}
184
185impl serde::Serialize for EthCallManyParams<'_> {
186    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
187        let len = if self.overrides().is_some() {
188            3
189        } else if self.context().is_some() {
190            2
191        } else {
192            1
193        };
194
195        let mut seq = serializer.serialize_seq(Some(len))?;
196        seq.serialize_element(&self.bundles())?;
197
198        if let Some(context) = self.context() {
199            seq.serialize_element(context)?;
200        } else if self.overrides().is_some() {
201            seq.serialize_element(&StateContext::default())?;
202        }
203
204        if let Some(overrides) = self.overrides() {
205            seq.serialize_element(overrides)?;
206        }
207
208        seq.end()
209    }
210}