forked from onbjerg/ethers-flashbots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced.rs
64 lines (55 loc) · 2.07 KB
/
advanced.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
use anyhow::Result;
use ethers::core::{rand::thread_rng, types::transaction::eip2718::TypedTransaction};
use ethers::prelude::*;
use ethers_flashbots::*;
use std::convert::TryFrom;
use url::Url;
#[tokio::main]
async fn main() -> Result<()> {
// Connect to the network
let provider = Provider::<Http>::try_from("https://mainnet.eth.aragon.network")?;
// This is your searcher identity
let bundle_signer = LocalWallet::new(&mut thread_rng());
// This signs transactions
let wallet = LocalWallet::new(&mut thread_rng());
// Add signer and Flashbots middleware
let client = SignerMiddleware::new(
FlashbotsMiddleware::new(
provider,
Url::parse("https://relay.flashbots.net")?,
bundle_signer,
),
wallet,
);
// get last block number
let block_number = client.get_block_number().await?;
// Build a custom bundle that pays 0x0000000000000000000000000000000000000000
let tx = {
let mut inner: TypedTransaction = TransactionRequest::pay(Address::zero(), 100).into();
client.fill_transaction(&mut inner, None).await?;
inner
};
let signature = client.signer().sign_transaction(&tx).await?;
let bundle = BundleRequest::new()
.push_transaction(tx.rlp_signed(&signature))
.set_block(block_number + 1)
.set_simulation_block(block_number)
.set_simulation_timestamp(0);
// Simulate it
let simulated_bundle = client.inner().simulate_bundle(&bundle).await?;
println!("Simulated bundle: {:?}", simulated_bundle);
// Send it
let pending_bundle = client.inner().send_bundle(&bundle).await?;
// You can also optionally wait to see if the bundle was included
match pending_bundle.await {
Ok(bundle_hash) => println!(
"Bundle with hash {:?} was included in target block",
bundle_hash
),
Err(PendingBundleError::BundleNotIncluded) => {
println!("Bundle was not included in target block.")
}
Err(e) => println!("An error occured: {}", e),
}
Ok(())
}