Skip to content

Instantly share code, notes, and snippets.

@korrio
Created October 2, 2024 06:52
Show Gist options
  • Save korrio/de87693d613f8d8fe0704ddbb53c8b53 to your computer and use it in GitHub Desktop.
Save korrio/de87693d613f8d8fe0704ddbb53c8b53 to your computer and use it in GitHub Desktop.
arb.rs
use web3::types::{H160, U256};
use web3::contract::{Contract, Options};
use std::str::FromStr;
use std::time::Duration;
use tokio::time;
// Define DEX struct
struct DEX {
name: String,
router_address: H160,
factory_address: H160,
}
// Define token pair struct
struct TokenPair {
token_a: H160,
token_b: H160,
}
// Main arbitrage function
async fn perform_arbitrage(web3: web3::Web3<web3::transports::Http>, dexes: Vec<DEX>, token_pair: TokenPair) -> web3::Result<()> {
loop {
let prices = fetch_prices(&web3, &dexes, &token_pair).await?;
if let Some((buy_dex, sell_dex, profit)) = find_arbitrage_opportunity(&prices) {
if profit > U256::from(0) {
execute_arbitrage(&web3, buy_dex, sell_dex, &token_pair, profit).await?;
}
}
time::sleep(Duration::from_secs(10)).await;
}
}
// Fetch prices from DEXes
async fn fetch_prices(web3: &web3::Web3<web3::transports::Http>, dexes: &[DEX], token_pair: &TokenPair) -> web3::Result<Vec<(String, U256)>> {
let mut prices = Vec::new();
for dex in dexes {
let router = Contract::from_json(
web3.eth(),
dex.router_address,
include_bytes!("path/to/router_abi.json"),
)?;
let price: U256 = router.query(
"getAmountsOut",
(U256::exp10(18), vec![token_pair.token_a, token_pair.token_b]),
None,
Options::default(),
None,
)
.await?;
prices.push((dex.name.clone(), price));
}
Ok(prices)
}
// Find arbitrage opportunity
fn find_arbitrage_opportunity(prices: &[(String, U256)]) -> Option<(&str, &str, U256)> {
let (min_dex, min_price) = prices.iter().min_by_key(|&(_, price)| price)?;
let (max_dex, max_price) = prices.iter().max_by_key(|&(_, price)| price)?;
let profit = max_price.saturating_sub(*min_price);
if profit > U256::zero() {
Some((min_dex, max_dex, profit))
} else {
None
}
}
// Execute arbitrage
async fn execute_arbitrage(
web3: &web3::Web3<web3::transports::Http>,
buy_dex: &str,
sell_dex: &str,
token_pair: &TokenPair,
amount: U256,
) -> web3::Result<()> {
// Implement buy and sell logic here
println!("Executing arbitrage: Buy on {}, Sell on {}, Amount: {}", buy_dex, sell_dex, amount);
Ok(())
}
#[tokio::main]
async fn main() -> web3::Result<()> {
let transport = web3::transports::Http::new("https://mainnet.infura.io/v3/YOUR-PROJECT-ID")?;
let web3 = web3::Web3::new(transport);
let dexes = vec![
DEX {
name: "ApeSwap".to_string(),
router_address: H160::from_str("APESWAP_ROUTER_ADDRESS").unwrap(),
factory_address: H160::from_str("APESWAP_FACTORY_ADDRESS").unwrap(),
},
DEX {
name: "PancakeSwap".to_string(),
router_address: H160::from_str("PANCAKESWAP_ROUTER_ADDRESS").unwrap(),
factory_address: H160::from_str("PANCAKESWAP_FACTORY_ADDRESS").unwrap(),
},
// Add other DEXes here
];
let token_pair = TokenPair {
token_a: H160::from_str("TOKEN_A_ADDRESS").unwrap(),
token_b: H160::from_str("TOKEN_B_ADDRESS").unwrap(),
};
perform_arbitrage(web3, dexes, token_pair).await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment