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 fn overrides(&self) -> Option<&StateOverride> {
38 self.overrides.as_ref()
39 }
40
41 pub fn data(&self) -> &N::TransactionRequest {
43 &self.data
44 }
45
46 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#[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 pub const fn new(bundles: &'req Vec<Bundle>) -> Self {
81 Self { bundles: Cow::Borrowed(bundles), context: None, overrides: None }
82 }
83
84 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 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 pub const fn with_context(mut self, context: StateContext) -> Self {
102 self.context = Some(context);
103 self
104 }
105
106 pub fn with_overrides(mut self, overrides: &'req StateOverride) -> Self {
108 self.overrides = Some(Cow::Borrowed(overrides));
109 self
110 }
111
112 pub const fn context(&self) -> Option<&StateContext> {
114 self.context.as_ref()
115 }
116
117 pub fn bundles(&self) -> &[Bundle] {
119 &self.bundles
120 }
121
122 pub fn bundles_mut(&mut self) -> &mut Vec<Bundle> {
124 Cow::to_mut(&mut self.bundles)
125 }
126
127 pub fn overrides(&self) -> Option<&StateOverride> {
129 self.overrides.as_deref()
130 }
131
132 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}