1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use ethers_core::types::{transaction::response::Transaction, Address, Bytes, H256, U256, U64};
use serde::{Deserialize, Serialize, Serializer};
pub type BundleHash = H256;
#[derive(Debug, Clone)]
pub enum BundleTransaction {
Signed(Transaction),
Raw(Bytes),
}
impl From<Transaction> for BundleTransaction {
fn from(tx: Transaction) -> Self {
Self::Signed(tx)
}
}
impl From<Bytes> for BundleTransaction {
fn from(tx: Bytes) -> Self {
Self::Raw(tx)
}
}
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundleRequest {
#[serde(rename = "txs")]
#[serde(serialize_with = "serialize_txs")]
transactions: Vec<BundleTransaction>,
#[serde(skip)]
revertible_transactions: Vec<usize>,
#[serde(rename = "revertingTxHashes")]
revertible_transaction_hashes: Vec<Bytes>,
#[serde(rename = "blockNumber")]
target_block: Option<U64>,
min_timestamp: Option<u64>,
max_timestamp: Option<u64>,
#[serde(rename = "stateBlockNumber")]
simulation_block: Option<U64>,
#[serde(rename = "timestamp")]
simulation_timestamp: Option<u64>,
}
pub fn serialize_txs<S>(txs: &[BundleTransaction], s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let raw_txs: Vec<Bytes> = txs
.iter()
.map(|tx| match tx {
BundleTransaction::Signed(inner) => inner.rlp(),
BundleTransaction::Raw(inner) => inner.clone(),
})
.collect();
raw_txs.serialize(s)
}
impl BundleRequest {
pub fn new() -> Self {
Default::default()
}
pub fn push_transaction<T: Into<BundleTransaction>>(mut self, tx: T) -> Self {
self.transactions.push(tx.into());
self
}
pub fn push_revertible_transaction<T: Into<BundleTransaction>>(mut self, tx: T) -> Self {
self.transactions.push(tx.into());
self.revertible_transactions
.push(self.transactions.len() - 1);
self
}
pub fn transactions(&self) -> &Vec<BundleTransaction> {
&self.transactions
}
pub fn block(&self) -> Option<U64> {
self.target_block
}
pub fn set_block(mut self, block: U64) -> Self {
self.target_block = Some(block);
self
}
pub fn simulation_block(&self) -> Option<U64> {
self.simulation_block
}
pub fn set_simulation_block(mut self, block: U64) -> Self {
self.simulation_block = Some(block);
self
}
pub fn simulation_timestamp(&self) -> Option<u64> {
self.simulation_timestamp
}
pub fn set_simulation_timestamp(mut self, timestamp: u64) -> Self {
self.simulation_timestamp = Some(timestamp);
self
}
pub fn min_timestamp(&self) -> Option<u64> {
self.min_timestamp
}
pub fn set_min_timestamp(mut self, timestamp: u64) -> Self {
self.min_timestamp = Some(timestamp);
self
}
pub fn max_timestamp(&self) -> Option<u64> {
self.max_timestamp
}
pub fn set_max_timestamp(mut self, timestamp: u64) -> Self {
self.max_timestamp = Some(timestamp);
self
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct SimulatedTransaction {
#[serde(rename = "txHash")]
pub hash: H256,
#[serde(rename = "coinbaseDiff")]
pub coinbase_diff: U256,
#[serde(rename = "ethSentToCoinbase")]
pub coinbase_tip: U256,
#[serde(rename = "gasPrice")]
pub gas_price: U256,
#[serde(rename = "gasUsed")]
pub gas_used: U256,
#[serde(rename = "gasFees")]
pub gas_fees: U256,
#[serde(rename = "fromAddress")]
pub from: Address,
#[serde(rename = "toAddress")]
pub to: Address,
pub value: U256,
}
#[derive(Debug, Clone, Deserialize)]
pub struct SimulatedBundle {
#[serde(rename = "bundleHash")]
pub hash: BundleHash,
#[serde(rename = "coinbaseDiff")]
pub coinbase_diff: U256,
#[serde(rename = "ethSentToCoinbase")]
pub coinbase_tip: U256,
#[serde(rename = "bundleGasPrice")]
pub gas_price: U256,
#[serde(rename = "totalGasUsed")]
pub gas_used: U256,
#[serde(rename = "gasFees")]
pub gas_fees: U256,
#[serde(rename = "stateBlockNumber")]
pub simulation_block: U64,
#[serde(rename = "results")]
pub transactions: Vec<SimulatedTransaction>,
}