Skip to main content

flashbots_sdk/
bundler.rs

1use crate::types::Bundle;
2use ethers::types::{H256, U64};
3
4/// A builder for creating `Bundle` instances with a fluent interface.
5///
6/// # Example
7/// ```
8/// use bundle_builder::BundleBuilder;
9/// use ethers::types::U64;
10///
11/// let bundle = BundleBuilder::new()
12///     .add_transaction("0x1234...".to_string())
13///     .block_number(U64::from(12345))
14///     .min_timestamp(1625097600)
15///     .build();
16/// ```
17#[derive(Debug)]
18pub struct BundleBuilder {
19    txs: Vec<String>,
20    block_number: Option<U64>,
21    min_timestamp: Option<u64>,
22    max_timestamp: Option<u64>,
23    reverting_tx_hashes: Option<Vec<H256>>,
24    replacement_uuid: Option<String>,
25}
26
27impl BundleBuilder {
28    /// Creates a new `BundleBuilder` with default values.
29    ///
30    /// # Example
31    /// ```
32    /// use bundle_builder::BundleBuilder;
33    ///
34    /// let builder = BundleBuilder::new();
35    /// ```
36    pub fn new() -> Self {
37        Self {
38            txs: Vec::new(),
39            block_number: None,
40            min_timestamp: None,
41            max_timestamp: None,
42            reverting_tx_hashes: None,
43            replacement_uuid: None,
44        }
45    }
46
47    /// Adds a single transaction to the bundle.
48    ///
49    /// # Example
50    /// ```
51    /// use bundle_builder::BundleBuilder;
52    ///
53    /// let builder = BundleBuilder::new()
54    ///     .add_transaction("0xabcd...".to_string());
55    /// ```
56    pub fn add_transaction(mut self, tx: String) -> Self {
57        self.txs.push(tx);
58        self
59    }
60
61    /// Adds multiple transactions to the bundle.
62    ///
63    /// # Example
64    /// ```
65    /// use bundle_builder::BundleBuilder;
66    ///
67    /// let transactions = vec![
68    ///     "0x1234...".to_string(),
69    ///     "0x5678...".to_string(),
70    /// ];
71    /// let builder = BundleBuilder::new()
72    ///     .add_transactions(transactions);
73    /// ```
74    pub fn add_transactions(mut self, transactions: Vec<String>) -> Self {
75        self.txs.extend(transactions);
76        self
77    }
78
79    /// Sets the target block number for the bundle.
80    ///
81    /// # Example
82    /// ```
83    /// use bundle_builder::BundleBuilder;
84    /// use ethers::types::U64;
85    ///
86    /// let builder = BundleBuilder::new()
87    ///     .block_number(U64::from(12345));
88    /// ```
89    pub fn block_number(mut self, block_number: U64) -> Self {
90        self.block_number = Some(block_number);
91        self
92    }
93
94    /// Sets the minimum timestamp for the bundle.
95    ///
96    /// # Example
97    /// ```
98    /// use bundle_builder::BundleBuilder;
99    ///
100    /// let builder = BundleBuilder::new()
101    ///     .min_timestamp(1625097600);
102    /// ```
103    pub fn min_timestamp(mut self, timestamp: u64) -> Self {
104        self.min_timestamp = Some(timestamp);
105        self
106    }
107
108    /// Sets the maximum timestamp for the bundle.
109    ///
110    /// # Example
111    /// ```
112    /// use bundle_builder::BundleBuilder;
113    ///
114    /// let builder = BundleBuilder::new()
115    ///     .max_timestamp(1625184000);
116    /// ```
117    pub fn max_timestamp(mut self, timestamp: u64) -> Self {
118        self.max_timestamp = Some(timestamp);
119        self
120    }
121
122    /// Sets the reverting transaction hashes for the bundle.
123    ///
124    /// # Example
125    /// ```
126    /// use bundle_builder::BundleBuilder;
127    /// use ethers::types::H256;
128    ///
129    /// let hashes = vec![
130    ///     H256::zero(),
131    ///     H256::repeat_byte(0xab),
132    /// ];
133    /// let builder = BundleBuilder::new()
134    ///     .reverting_tx_hashes(hashes);
135    /// ```
136    pub fn reverting_tx_hashes(mut self, hashes: Vec<H256>) -> Self {
137        self.reverting_tx_hashes = Some(hashes);
138        self
139    }
140
141    /// Sets the replacement UUID for the bundle.
142    ///
143    /// # Example
144    /// ```
145    /// use bundle_builder::BundleBuilder;
146    ///
147    /// let builder = BundleBuilder::new()
148    ///     .replacement_uuid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".to_string());
149    /// ```
150    pub fn replacement_uuid(mut self, uuid: String) -> Self {
151        self.replacement_uuid = Some(uuid);
152        self
153    }
154
155    /// Builds and returns the final `Bundle` instance.
156    ///
157    /// # Example
158    /// ```
159    /// use bundle_builder::BundleBuilder;
160    /// use ethers::types::U64;
161    ///
162    /// let bundle = BundleBuilder::new()
163    ///     .add_transaction("0x1234...".to_string())
164    ///     .block_number(U64::from(12345))
165    ///     .build();
166    /// ```
167    pub fn build(self) -> Bundle {
168        Bundle {
169            txs: self.txs,
170            block_number: self.block_number,
171            min_timestamp: self.min_timestamp,
172            max_timestamp: self.max_timestamp,
173            reverting_tx_hashes: self.reverting_tx_hashes,
174            replacement_uuid: self.replacement_uuid,
175        }
176    }
177}