1use alloy_primitives::Address;
2use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
3
4#[derive(Deserialize, Debug, Serialize, Clone, Default, PartialEq, Eq)]
6pub enum ProtocolVersion {
7 #[default]
8 #[serde(rename = "beta-1")]
9 Beta1,
11 #[serde(rename = "v0.1")]
13 V0_1,
14}
15
16#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase")]
19pub struct ConsideredByBuildersAt {
20 pub pubkey: String,
22 pub timestamp: String,
24}
25
26#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct SealedByBuildersAt {
30 pub pubkey: String,
32 pub timestamp: String,
34}
35
36#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
38#[serde(rename_all = "camelCase")]
39pub struct Validity {
40 #[serde(skip_serializing_if = "Option::is_none")]
43 pub refund: Option<Vec<Refund>>,
44 #[serde(skip_serializing_if = "Option::is_none")]
47 pub refund_config: Option<Vec<RefundConfig>>,
48}
49
50impl Validity {
51 pub fn with_refund(mut self, refund: Option<Vec<Refund>>) -> Self {
53 self.refund = refund;
54 self
55 }
56
57 pub fn with_refund_config(mut self, refund_config: Option<Vec<RefundConfig>>) -> Self {
59 self.refund_config = refund_config;
60 self
61 }
62}
63
64#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
66#[serde(rename_all = "camelCase")]
67pub struct Privacy {
68 #[serde(skip_serializing_if = "Option::is_none")]
70 pub hints: Option<PrivacyHint>,
71 #[serde(skip_serializing_if = "Option::is_none")]
74 pub builders: Option<Vec<String>>,
75}
76
77impl Privacy {
78 pub const fn with_hints(mut self, hints: Option<PrivacyHint>) -> Self {
80 self.hints = hints;
81 self
82 }
83
84 pub fn with_builders(mut self, builders: Option<Vec<String>>) -> Self {
86 self.builders = builders;
87 self
88 }
89}
90
91#[derive(Clone, Debug, Default, PartialEq, Eq)]
93pub struct PrivacyHint {
94 pub calldata: bool,
96 pub contract_address: bool,
98 pub logs: bool,
100 pub function_selector: bool,
102 pub hash: bool,
104 pub tx_hash: bool,
106 pub special_logs: bool,
111}
112
113impl PrivacyHint {
114 pub const fn with_calldata(mut self) -> Self {
117 self.calldata = true;
118 self
119 }
120
121 pub const fn with_contract_address(mut self) -> Self {
124 self.contract_address = true;
125 self
126 }
127
128 pub const fn with_logs(mut self) -> Self {
130 self.logs = true;
131 self
132 }
133
134 pub const fn with_function_selector(mut self) -> Self {
137 self.function_selector = true;
138 self
139 }
140
141 pub const fn with_hash(mut self) -> Self {
143 self.hash = true;
144 self
145 }
146
147 pub const fn with_tx_hash(mut self) -> Self {
150 self.tx_hash = true;
151 self
152 }
153
154 pub const fn with_special_logs(mut self) -> Self {
157 self.special_logs = true;
158 self
159 }
160
161 pub const fn has_calldata(&self) -> bool {
163 self.calldata
164 }
165
166 pub const fn has_contract_address(&self) -> bool {
168 self.contract_address
169 }
170
171 pub const fn has_logs(&self) -> bool {
173 self.logs
174 }
175
176 pub const fn has_function_selector(&self) -> bool {
178 self.function_selector
179 }
180
181 pub const fn has_hash(&self) -> bool {
183 self.hash
184 }
185
186 pub const fn has_tx_hash(&self) -> bool {
188 self.tx_hash
189 }
190
191 pub const fn has_special_logs(&self) -> bool {
193 self.special_logs
194 }
195
196 const fn flags(&self) -> [bool; 7] {
198 [
199 self.calldata,
200 self.contract_address,
201 self.logs,
202 self.function_selector,
203 self.hash,
204 self.tx_hash,
205 self.special_logs,
206 ]
207 }
208
209 const fn flags_mut(&mut self) -> [&mut bool; 7] {
211 [
212 &mut self.calldata,
213 &mut self.contract_address,
214 &mut self.logs,
215 &mut self.function_selector,
216 &mut self.hash,
217 &mut self.tx_hash,
218 &mut self.special_logs,
219 ]
220 }
221}
222
223impl Serialize for PrivacyHint {
224 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
225 let flags = self.flags();
226 let mut seq = serializer.serialize_seq(Some(flags.iter().filter(|set| **set).count()))?;
227 for (name, set) in PRIVACY_HINT_VARIANTS.iter().zip(flags) {
228 if set {
229 seq.serialize_element(name)?;
230 }
231 }
232 seq.end()
233 }
234}
235
236const PRIVACY_HINT_VARIANTS: &[&str] = &[
238 "calldata",
239 "contract_address",
240 "logs",
241 "function_selector",
242 "hash",
243 "tx_hash",
244 "special_logs",
245];
246
247const DEFAULT_LOGS_HINT: &str = "default_logs";
249
250impl<'de> Deserialize<'de> for PrivacyHint {
251 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
252 let hints = Vec::<String>::deserialize(deserializer)?;
253 let mut privacy_hint = Self::default();
254 for hint in hints {
255 let name = if hint == DEFAULT_LOGS_HINT { "special_logs" } else { hint.as_str() };
256 let Some(idx) = PRIVACY_HINT_VARIANTS.iter().position(|variant| *variant == name)
257 else {
258 return Err(serde::de::Error::unknown_variant(&hint, PRIVACY_HINT_VARIANTS));
259 };
260 *privacy_hint.flags_mut()[idx] = true;
261 }
262 Ok(privacy_hint)
263 }
264}
265
266#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
272#[serde(rename_all = "camelCase")]
273pub struct Refund {
274 #[serde(with = "alloy_serde::quantity")]
276 pub body_idx: u64,
277 #[serde(with = "alloy_serde::quantity")]
279 pub percent: u64,
280}
281
282#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
285#[serde(rename_all = "camelCase")]
286pub struct RefundConfig {
287 pub address: Address,
289 #[serde(with = "alloy_serde::quantity")]
291 pub percent: u64,
292}