alloy_provider/provider/eth_call/
params.rs1use 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#[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 pub const fn new(data: N::TransactionRequest) -> Self {
21 Self { data, block: None, overrides: None }
22 }
23
24 pub const fn with_block(mut self, block: BlockId) -> Self {
26 self.block = Some(block);
27 self
28 }
29
30 pub fn with_overrides(mut self, overrides: StateOverride) -> Self {
32 self.overrides = Some(overrides);
33 self
34 }
35
36 pub const fn overrides(&self) -> Option<&StateOverride> {
38 self.overrides.as_ref()
39 }
40
41 pub const fn data(&self) -> &N::TransactionRequest {
43 &self.data
44 }
45
46 pub fn into_data(self) -> N::TransactionRequest {
48 self.data
49 }
50
51 pub const fn block(&self) -> Option<BlockId> {
53 self.block
54 }
55}
56
57impl<N: Network> serde::Serialize for EthCallParams<N> {
58 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
59 let len = if self.overrides().is_some() { 3 } else { 2 };
60
61 let mut seq = serializer.serialize_seq(Some(len))?;
62 seq.serialize_element(&self.data())?;
63
64 if let Some(overrides) = self.overrides() {
65 seq.serialize_element(&self.block().unwrap_or_default())?;
66 seq.serialize_element(overrides)?;
67 } else if let Some(block) = self.block() {
68 seq.serialize_element(&block)?;
69 }
70
71 seq.end()
72 }
73}
74
75#[derive(Clone, Debug)]
77pub struct EthCallManyParams<'req> {
78 bundles: Cow<'req, [Bundle]>,
79 context: Option<StateContext>,
80 overrides: Option<Cow<'req, StateOverride>>,
81}
82
83impl<'req> EthCallManyParams<'req> {
84 pub const fn new(bundles: &'req [Bundle]) -> Self {
86 Self { bundles: Cow::Borrowed(bundles), context: None, overrides: None }
87 }
88
89 pub fn with_block(mut self, block: BlockId) -> Self {
91 let mut context = self.context.unwrap_or_default();
92 context.block_number = Some(block);
93 self.context = Some(context);
94 self
95 }
96
97 pub fn with_transaction_index(mut self, tx_index: TransactionIndex) -> Self {
99 let mut context = self.context.unwrap_or_default();
100 context.transaction_index = Some(tx_index);
101 self.context = Some(context);
102 self
103 }
104
105 pub const fn with_context(mut self, context: StateContext) -> Self {
107 self.context = Some(context);
108 self
109 }
110
111 pub fn with_overrides(mut self, overrides: &'req StateOverride) -> Self {
113 self.overrides = Some(Cow::Borrowed(overrides));
114 self
115 }
116
117 pub const fn context(&self) -> Option<&StateContext> {
119 self.context.as_ref()
120 }
121
122 pub fn bundles(&self) -> &[Bundle] {
124 &self.bundles
125 }
126
127 pub fn bundles_mut(&mut self) -> &mut Vec<Bundle> {
129 Cow::to_mut(&mut self.bundles)
130 }
131
132 pub fn overrides(&self) -> Option<&StateOverride> {
134 self.overrides.as_deref()
135 }
136
137 pub fn into_owned(self) -> EthCallManyParams<'static> {
139 EthCallManyParams {
140 bundles: Cow::Owned(self.bundles.into_owned()),
141 context: self.context,
142 overrides: self.overrides.map(|o| Cow::Owned(o.into_owned())),
143 }
144 }
145}
146
147impl serde::Serialize for EthCallManyParams<'_> {
148 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
149 let len = if self.overrides().is_some() { 3 } else { 2 };
150
151 let mut seq = serializer.serialize_seq(Some(len))?;
152 seq.serialize_element(&self.bundles())?;
153
154 if let Some(context) = self.context() {
155 seq.serialize_element(context)?;
156 }
157
158 if let Some(overrides) = self.overrides() {
159 seq.serialize_element(overrides)?;
160 }
161
162 seq.end()
163 }
164}