alloy_provider/provider/eth_call/
params.rs

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