Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update utils.rs #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Construct evm-based sandwich attacks using Rust and Huff.

#### Getting Started

> Before going further you should have the following packages installed on your system: Yarn, Cargo, Rust, Forge, Huff Compiler, Solc=0.8.15

[subway-rs](https://github.com/refcell/subway-rs) is a port of [libevm](https://twitter.com/libevm)'s original [subway](https://github.com/libevm/subway), implemented with [ethers-rs](https://github.com/gakonst/ethers-rs) and [huff](https://github.com/huff-language).

> Having highly optimized contracts is just one part of the equation, a tech stack is just as important as the contracts to execute on the opportunities.
Expand Down
14 changes: 11 additions & 3 deletions bot/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A module containing common utilities

use std::{str::FromStr, sync::Arc};

use dotenv::dotenv; // read .env files
use ethers::{prelude::*, types::transaction::eip2718::TypedTransaction};
use eyre::Result;
use rand::Rng;
Expand All @@ -16,13 +16,15 @@ pub fn sort_tokens(a: &mut Address, b: &mut Address) {
/// Returns the WETH Contract Address
///
/// Although this function unwraps the address conversion, it is safe as the string is checked.
/// Edit this address to match the one on the chain you're operating on
pub fn get_weth_address() -> Address {
Address::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap()
}

/// Returns the usdc Contract Address
///
/// Although this function unwraps the address conversion, it is safe as the string is checked.
/// Edit this address to match the one on the chain you're operating on
pub fn get_usdc_address() -> Address {
Address::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap()
}
Expand Down Expand Up @@ -69,12 +71,13 @@ pub fn calculate_next_block_base_fee(block: Block<TxHash>) -> eyre::Result<U256>

/// Read environment variables
pub fn read_env_vars() -> Result<Vec<(String, String)>> {
dotenv().ok(); // read .env file
let mut env_vars = Vec::new();
let keys = vec![
"RPC_URL",
"RPC_URL_WSS",
"PRIVATE_KEY",
"FLASHBOTS_AUTH_KEY",
"PRIVATE_KEY", // remove the 0x from the key in the .env file
"FLASHBOTS_AUTH_KEY", // remove the 0x from the key in the .env file
"SANDWICH_CONTRACT",
];
for key in keys {
Expand All @@ -88,20 +91,23 @@ pub fn read_env_vars() -> Result<Vec<(String, String)>> {

/// Returns the configured Sandwich Contract Address
pub fn get_sandwich_contract_address() -> Result<Address> {
dotenv().ok(); // read .env
let addr = std::env::var("SANDWICH_CONTRACT")
.map_err(|_| eyre::eyre!("Required environment variable \"SANDWICH_CONTRACT\" not set"))?;
Address::from_str(&addr).map_err(|_| eyre::eyre!("Invalid address \"{}\"", addr))
}

/// Return a Provider for the given URL
pub fn get_http_provider() -> Result<Provider<Http>> {
dotenv().ok(); // read .env
let url = std::env::var("RPC_URL")
.map_err(|_| eyre::eyre!("Required environment variable \"RPC_URL\" not set"))?;
Provider::<Http>::try_from(url).map_err(|_| eyre::eyre!("Invalid RPC URL"))
}

/// Return a Provider for the given Websocket URL
pub async fn get_ws_provider() -> Result<Provider<Ws>> {
dotenv().ok(); // read .env
let url = std::env::var("RPC_URL_WSS")
.map_err(|_| eyre::eyre!("Required environment variable \"RPC_URL_WSS\" not set"))?;
Provider::<Ws>::connect(&url)
Expand All @@ -117,6 +123,7 @@ pub async fn create_websocket_client() -> Result<Arc<Provider<Ws>>> {

/// Construct the searcher wallet
pub fn get_searcher_wallet() -> Result<LocalWallet> {
dotenv().ok(); // read .env
let private_key = std::env::var("PRIVATE_KEY")
.map_err(|_| eyre::eyre!("Required environment variable \"PRIVATE_KEY\" not set"))?;
private_key
Expand All @@ -127,6 +134,7 @@ pub fn get_searcher_wallet() -> Result<LocalWallet> {
/// Construct the bundle signer
/// This is your flashbots searcher identity
pub fn get_bundle_signer() -> Result<LocalWallet> {
dotenv().ok(); // read .env
let private_key = std::env::var("FLASHBOTS_AUTH_KEY")
.map_err(|_| eyre::eyre!("Required environment variable \"FLASHBOTS_AUTH_KEY\" not set"))?;
private_key
Expand Down