use ethers::core::{rand::thread_rng, types::transaction::eip2718::TypedTransaction};
use ethers::prelude::*;
use ethers_flashbots::*;
use eyre::Result;
use std::convert::TryFrom;
use url::Url;
#[tokio::main]
async fn main() -> Result<()> {
let provider = Provider::<Http>::try_from("https://mainnet.eth.aragon.network")?;
let bundle_signer = LocalWallet::new(&mut thread_rng());
let wallet = LocalWallet::new(&mut thread_rng());
let client = SignerMiddleware::new(
FlashbotsMiddleware::new(
provider,
Url::parse("https://relay.flashbots.net")?,
bundle_signer,
),
wallet,
);
let block_number = client.get_block_number().await?;
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);
let simulated_bundle = client.inner().simulate_bundle(&bundle).await?;
println!("Simulated bundle: {:?}", simulated_bundle);
let pending_bundle = client.inner().send_bundle(&bundle).await?;
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(())
}