-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.rs
225 lines (210 loc) · 6.87 KB
/
handler.rs
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
use std::{error::Error, str::FromStr};
use ethers_core::{
abi::{RawLog, Token},
types::{Bytes, Eip1559TransactionRequest, H160, H256, U256},
};
use ethers_providers::Middleware;
use ethers_signers::Signer;
use futures_util::Future;
use mev_share_rpc_api::{BundleItem, Inclusion, SendBundleRequest};
use crate::{
constants::{
BUNDLE_BLOCK_WINDOW, NEW_CONTRACT_INIT_CODE_HASH, PROGRESS, RELAY_CLIENT, RPC_CLIENT,
WALLET,
},
contracts::{MAGIC_NUMBER_ABI, NEW_CONTRACT_ABI},
signer::sign_transaction,
};
pub async fn backrun_create_contract_salt(tx_to_backrun: H256, to: H160, data: &Bytes) {
backrun_handler(tx_to_backrun, to, async move {
let nonce = RPC_CLIENT
.get_transaction_count(WALLET.address(), None)
.await?;
let salt = NEW_CONTRACT_ABI
.event("ActivateBySalt")?
.parse_log(RawLog {
topics: vec![H256::from_str(
"0x71fd33d3d871c60dc3d6ecf7c8e5bb086aeb6491528cce181c289a411582ff1c",
)?],
data: data.to_vec(),
})?
.params
.into_iter()
.next()
.unwrap()
.value
.into_fixed_bytes()
.unwrap();
let addr = ethers_core::utils::get_create2_address_from_hash(
to,
salt,
*NEW_CONTRACT_INIT_CODE_HASH,
);
let tx = Eip1559TransactionRequest::new()
.to(addr)
.data(Bytes::from_str("0xb88a802f")?)
.nonce(nonce);
let bytes = sign_transaction(tx).await?;
Ok(vec![BundleItem::Tx {
tx: bytes,
can_revert: false,
}])
})
.await;
}
pub async fn backrun_create_contract_addr(tx_to_backrun: H256, to: H160, data: &Bytes) {
backrun_handler(tx_to_backrun, to, async move {
let nonce = RPC_CLIENT
.get_transaction_count(WALLET.address(), None)
.await?;
let addr = NEW_CONTRACT_ABI
.event("Activate")?
.parse_log(RawLog {
topics: vec![H256::from_str(
"0xf7e9fe69e1d05372bc855b295bc4c34a1a0a5882164dd2b26df30a26c1c8ba15",
)?],
data: data.to_vec(),
})?
.params
.into_iter()
.next()
.unwrap()
.value
.into_address()
.unwrap();
let tx = Eip1559TransactionRequest::new()
.to(addr)
.data(Bytes::from_str("0xb88a802f")?)
.nonce(nonce);
let bytes = sign_transaction(tx).await?;
Ok(vec![BundleItem::Tx {
tx: bytes,
can_revert: false,
}])
})
.await;
}
pub async fn backrun_magic_numba(tx_to_backrun: H256, to: H160, bound_data: &Bytes) {
if let Err(err) = async {
let nonce = RPC_CLIENT
.get_transaction_count(WALLET.address(), None)
.await?;
let mut bounds = MAGIC_NUMBER_ABI
.event("Activate")?
.parse_log(RawLog {
topics: vec![H256::from_str(
"0x86a27c2047f889fafe51029e28e24f466422abe8a82c0c27de4683dda79a0b5d",
)?],
data: bound_data.to_vec(),
})?
.params
.into_iter();
let lower_b = bounds.next().unwrap().value.into_uint().unwrap();
let upper_b = bounds.next().unwrap().value.into_uint().unwrap();
let mut magic_number = lower_b;
while magic_number <= upper_b {
magic_number += U256::one();
tokio::spawn(async move {
backrun_handler(tx_to_backrun, to, async move {
let tx_body = Bytes::from(
MAGIC_NUMBER_ABI
.function("claimReward")?
.encode_input(&[Token::Uint(magic_number)])?,
);
let tx = Eip1559TransactionRequest::new()
.to(to)
.data(tx_body)
.nonce(nonce);
let bytes = sign_transaction(tx).await?;
Ok(vec![BundleItem::Tx {
tx: bytes,
can_revert: false,
}])
})
.await;
});
}
Result::<(), Box<dyn Error + Send + Sync>>::Ok(())
}
.await
{
println!("Error getting nonce: {:?}", err);
}
}
pub async fn backrun_simple_triple(tx_to_backrun: H256, to: H160) {
backrun_handler(tx_to_backrun, to, async move {
let nonce = RPC_CLIENT
.get_transaction_count(WALLET.address(), None)
.await?;
let mut bundle = vec![];
for i in 0..3 {
let tx = Eip1559TransactionRequest::new()
.to(to)
.data(Bytes::from_str("0xb88a802f")?)
.nonce(nonce + i);
let bytes = sign_transaction(tx).await?;
bundle.push(BundleItem::Tx {
tx: bytes,
can_revert: false,
});
}
Ok(bundle)
})
.await
}
pub async fn backrun_simple(tx_to_backrun: H256, to: H160) {
backrun_handler(tx_to_backrun, to, async move {
let nonce = RPC_CLIENT
.get_transaction_count(WALLET.address(), None)
.await?;
let tx = Eip1559TransactionRequest::new()
.to(to)
.data(Bytes::from_str("0xb88a802f")?)
.nonce(nonce);
let bytes = sign_transaction(tx).await?;
Ok(vec![BundleItem::Tx {
tx: bytes,
can_revert: false,
}])
})
.await
}
async fn backrun_handler<
O: Future<Output = Result<Vec<BundleItem>, Box<dyn Error + Send + Sync>>>,
>(
tx_to_backrun: H256,
to: H160,
items: O,
) {
if PROGRESS.get_progress_for_address(to).await {
//println!("Skipping address {}: Already processed", to);
return;
}
println!(
"Processing transaction to {:?} backrunning {:?}!",
to, tx_to_backrun
);
PROGRESS.set_is_processing(to, true).await;
if let Err(e) = async {
let mut bundle_body = vec![BundleItem::Hash {
hash: tx_to_backrun,
}];
bundle_body.append(&mut items.await?.to_vec());
let block = PROGRESS.get_latest_block().await;
let bundle = SendBundleRequest {
bundle_body,
inclusion: Inclusion {
block,
max_block: Some(block + BUNDLE_BLOCK_WINDOW),
},
..Default::default()
};
let resp = RELAY_CLIENT.as_ref().send_bundle(bundle.clone()).await?;
println!("Got a bundle response: {:?}", resp);
Result::<(), Box<dyn Error + Send + Sync>>::Ok(())
}
.await
{
println!("Error processing address {}: {:?}", to, e);
}
}