Skip to main content

alloy_rpc_types_mev/
common.rs

1use alloy_primitives::Address;
2use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
3
4/// The version of the MEV-share API to use.
5#[derive(Deserialize, Debug, Serialize, Clone, Default, PartialEq, Eq)]
6pub enum ProtocolVersion {
7    #[default]
8    #[serde(rename = "beta-1")]
9    /// The beta-1 version of the API.
10    Beta1,
11    /// The 0.1 version of the API.
12    #[serde(rename = "v0.1")]
13    V0_1,
14}
15
16/// Represents information about when a bundle was considered by a builder.
17#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase")]
19pub struct ConsideredByBuildersAt {
20    /// The public key of the builder.
21    pub pubkey: String,
22    /// The timestamp indicating when the bundle was considered by the builder.
23    pub timestamp: String,
24}
25
26/// Represents information about when a bundle was sealed by a builder.
27#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct SealedByBuildersAt {
30    /// The public key of the builder.
31    pub pubkey: String,
32    /// The timestamp indicating when the bundle was sealed by the builder.
33    pub timestamp: String,
34}
35
36/// Requirements for the bundle to be included in the block.
37#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
38#[serde(rename_all = "camelCase")]
39pub struct Validity {
40    /// Specifies the minimum percent of a given bundle's earnings to redistribute
41    /// for it to be included in a builder's block.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub refund: Option<Vec<Refund>>,
44    /// Specifies what addresses should receive what percent of the overall refund for this bundle,
45    /// if it is enveloped by another bundle (eg. a searcher backrun).
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub refund_config: Option<Vec<RefundConfig>>,
48}
49
50impl Validity {
51    /// Sets refund
52    pub fn with_refund(mut self, refund: Option<Vec<Refund>>) -> Self {
53        self.refund = refund;
54        self
55    }
56
57    /// Sets refund config
58    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/// Preferences on what data should be shared about the bundle and its transactions
65#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
66#[serde(rename_all = "camelCase")]
67pub struct Privacy {
68    /// Hints on what data should be shared about the bundle and its transactions
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub hints: Option<PrivacyHint>,
71    /// Names of the builders that should be allowed to see the bundle/transaction.
72    /// <https://github.com/flashbots/dowg/blob/main/builder-registrations.json>
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub builders: Option<Vec<String>>,
75}
76
77impl Privacy {
78    /// Sets hints
79    pub const fn with_hints(mut self, hints: Option<PrivacyHint>) -> Self {
80        self.hints = hints;
81        self
82    }
83
84    /// Sets builders
85    pub fn with_builders(mut self, builders: Option<Vec<String>>) -> Self {
86        self.builders = builders;
87        self
88    }
89}
90
91/// Hints on what data should be shared about the bundle and its transactions
92#[derive(Clone, Debug, Default, PartialEq, Eq)]
93pub struct PrivacyHint {
94    /// The calldata of the bundle's transactions should be shared.
95    pub calldata: bool,
96    /// The address of the bundle's transactions should be shared.
97    pub contract_address: bool,
98    /// The logs of the bundle's transactions should be shared.
99    pub logs: bool,
100    /// The function selector of the bundle's transactions should be shared.
101    pub function_selector: bool,
102    /// The hash of the bundle's transactions should be shared.
103    pub hash: bool,
104    /// The hash of the bundle should be shared.
105    pub tx_hash: bool,
106    /// The swap logs of the bundle's transactions (Uniswap V2/V3, Curve and Balancer) should be
107    /// shared, stripped down to the pool and the swap event signature.
108    ///
109    /// Serialized as `special_logs`; `default_logs` is accepted as an alias.
110    pub special_logs: bool,
111}
112
113impl PrivacyHint {
114    /// Sets the flag indicating inclusion of calldata and returns the modified `PrivacyHint`
115    /// instance.
116    pub const fn with_calldata(mut self) -> Self {
117        self.calldata = true;
118        self
119    }
120
121    /// Sets the flag indicating inclusion of contract address and returns the modified
122    /// `PrivacyHint` instance.
123    pub const fn with_contract_address(mut self) -> Self {
124        self.contract_address = true;
125        self
126    }
127
128    /// Sets the flag indicating inclusion of logs and returns the modified `PrivacyHint` instance.
129    pub const fn with_logs(mut self) -> Self {
130        self.logs = true;
131        self
132    }
133
134    /// Sets the flag indicating inclusion of function selector and returns the modified
135    /// `PrivacyHint` instance.
136    pub const fn with_function_selector(mut self) -> Self {
137        self.function_selector = true;
138        self
139    }
140
141    /// Sets the flag indicating inclusion of hash and returns the modified `PrivacyHint` instance.
142    pub const fn with_hash(mut self) -> Self {
143        self.hash = true;
144        self
145    }
146
147    /// Sets the flag indicating inclusion of transaction hash and returns the modified
148    /// `PrivacyHint` instance.
149    pub const fn with_tx_hash(mut self) -> Self {
150        self.tx_hash = true;
151        self
152    }
153
154    /// Sets the flag indicating inclusion of special logs and returns the modified `PrivacyHint`
155    /// instance.
156    pub const fn with_special_logs(mut self) -> Self {
157        self.special_logs = true;
158        self
159    }
160
161    /// Checks if calldata inclusion flag is set.
162    pub const fn has_calldata(&self) -> bool {
163        self.calldata
164    }
165
166    /// Checks if contract address inclusion flag is set.
167    pub const fn has_contract_address(&self) -> bool {
168        self.contract_address
169    }
170
171    /// Checks if logs inclusion flag is set.
172    pub const fn has_logs(&self) -> bool {
173        self.logs
174    }
175
176    /// Checks if function selector inclusion flag is set.
177    pub const fn has_function_selector(&self) -> bool {
178        self.function_selector
179    }
180
181    /// Checks if hash inclusion flag is set.
182    pub const fn has_hash(&self) -> bool {
183        self.hash
184    }
185
186    /// Checks if transaction hash inclusion flag is set.
187    pub const fn has_tx_hash(&self) -> bool {
188        self.tx_hash
189    }
190
191    /// Checks if special logs inclusion flag is set.
192    pub const fn has_special_logs(&self) -> bool {
193        self.special_logs
194    }
195
196    /// Returns the hint flags in [`PRIVACY_HINT_VARIANTS`] order.
197    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    /// Returns mutable references to the hint flags in [`PRIVACY_HINT_VARIANTS`] order.
210    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
236/// All supported privacy hint variants.
237const PRIVACY_HINT_VARIANTS: &[&str] = &[
238    "calldata",
239    "contract_address",
240    "logs",
241    "function_selector",
242    "hash",
243    "tx_hash",
244    "special_logs",
245];
246
247/// Alias the MEV-Share node accepts for the `special_logs` hint.
248const 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/// Specifies the minimum percent of a given bundle's earnings to redistribute for it to be included
267/// in a builder's block.
268///
269/// Related endpoint: `mev_sendBundle`, `mev_simBundle`, `eth_sendPrivateTransaction`,
270/// `eth_sendPrivateRawTransaction`
271#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
272#[serde(rename_all = "camelCase")]
273pub struct Refund {
274    /// The index of the transaction in the bundle.
275    #[serde(with = "alloy_serde::quantity")]
276    pub body_idx: u64,
277    /// The minimum percent of the bundle's earnings to redistribute.
278    #[serde(with = "alloy_serde::quantity")]
279    pub percent: u64,
280}
281
282/// Specifies what addresses should receive what percent of the overall refund for this bundle,
283/// if it is enveloped by another bundle (eg. a searcher backrun).
284#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
285#[serde(rename_all = "camelCase")]
286pub struct RefundConfig {
287    /// The address to refund.
288    pub address: Address,
289    /// The minimum percent of the bundle's earnings to redistribute.
290    #[serde(with = "alloy_serde::quantity")]
291    pub percent: u64,
292}