From 2b5b2127f28fda3c42302223f5337eee4291933f Mon Sep 17 00:00:00 2001 From: yihao Date: Wed, 9 Aug 2023 21:41:43 -0700 Subject: [PATCH] Add some comments --- bot/.env.example | 2 +- bot/crates/strategy/src/bot/mod.rs | 3 ++- bot/crates/strategy/src/managers/pool_manager.rs | 4 ++++ bot/crates/strategy/src/simulator/huff_sando.rs | 8 ++++++++ bot/crates/strategy/src/simulator/lil_router.rs | 1 + bot/crates/strategy/src/types.rs | 1 + bot/sando-bin/src/main.rs | 4 ++-- 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/bot/.env.example b/bot/.env.example index 539393e..d0e6895 100644 --- a/bot/.env.example +++ b/bot/.env.example @@ -1,4 +1,4 @@ -WSS_RPC=ws://localhost:8545 +WSS_RPC=wss://capable-crimson-vineyard.discover.quiknode.pro/b0f808fe30ebe9dd7b2a7122c563e20f1e9966da/ SEARCHER_PRIVATE_KEY=0000000000000000000000000000000000000000000000000000000000000001 FLASHBOTS_AUTH_KEY=0000000000000000000000000000000000000000000000000000000000000002 SANDWICH_CONTRACT=0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa diff --git a/bot/crates/strategy/src/bot/mod.rs b/bot/crates/strategy/src/bot/mod.rs index 5ec6c10..f0c2f98 100644 --- a/bot/crates/strategy/src/bot/mod.rs +++ b/bot/crates/strategy/src/bot/mod.rs @@ -30,7 +30,8 @@ pub struct SandoBot { sando_state_manager: SandoStateManager, } -impl SandoBot { +impl SandoBot where M : Middleware + 'static +{ /// Create a new instance pub fn new(client: Arc, config: StratConfig) -> Self { Self { diff --git a/bot/crates/strategy/src/managers/pool_manager.rs b/bot/crates/strategy/src/managers/pool_manager.rs index e5bc7fc..8703698 100644 --- a/bot/crates/strategy/src/managers/pool_manager.rs +++ b/bot/crates/strategy/src/managers/pool_manager.rs @@ -109,6 +109,10 @@ impl PoolManager { // right now bot can only sandwich `weth->token` trades // enhancement: add support for `token->weth` trades (using longtail or flashswaps sandos) if to > from { + // From and to are balances of the ERC20 Token in WETH's address. If t is increasing, that means the tx + // is trading WETH for the token. + // So a frontrun tx transfering weth->token, plus the victim tx, plus the backrun + // tx token->weth, forms the sandwich attack. sandwichable_pools.push(pool); } } diff --git a/bot/crates/strategy/src/simulator/huff_sando.rs b/bot/crates/strategy/src/simulator/huff_sando.rs index 5a6cf17..a12510a 100644 --- a/bot/crates/strategy/src/simulator/huff_sando.rs +++ b/bot/crates/strategy/src/simulator/huff_sando.rs @@ -41,6 +41,11 @@ pub fn create_recipe( #[allow(unused_mut)] let mut fork_db = CacheDB::new(shared_backend); + + // Where do we check the optimal_in is less thanthe weth_inventory in the + // searcher' address? + + // why below #[cfg(feature = "debug")] { inject_huff_sando( @@ -153,7 +158,9 @@ pub fn create_recipe( TransactTo::Call(rAddress::from_slice(&meat.to.unwrap_or_default().0)); evm.env.tx.data = meat.input.0.clone(); evm.env.tx.value = meat.value.into(); + // Why we set the chain id here? evm.env.tx.chain_id = meat.chain_id.map(|id| id.as_u64()); + // Should we check nonce to prevent any fake tx? //evm.env.tx.nonce = Some(meat.nonce.as_u64()); evm.env.tx.gas_limit = meat.gas.as_u64(); match meat.transaction_type { @@ -239,6 +246,7 @@ pub fn create_recipe( evm.env.tx = backrun_tx_env.clone(); // create access list + // Why we need to use access list here let mut access_list_inspector = AccessListTracer::new( Default::default(), searcher, diff --git a/bot/crates/strategy/src/simulator/lil_router.rs b/bot/crates/strategy/src/simulator/lil_router.rs index 724aea3..8762f46 100644 --- a/bot/crates/strategy/src/simulator/lil_router.rs +++ b/bot/crates/strategy/src/simulator/lil_router.rs @@ -237,6 +237,7 @@ async fn evaluate_sandwich_revenue( // evm.env.tx.nonce = Some(meat.nonce.as_u64()); /** ignore nonce check for now **/ evm.env.tx.gas_limit = meat.gas.as_u64(); match meat.transaction_type { + // 1. Why we use U64([0]) (an array) here; 2. why Some(0) is a legacy type Some(ethers::types::U64([0])) => { // legacy tx evm.env.tx.gas_price = meat.gas_price.unwrap_or_default().into(); diff --git a/bot/crates/strategy/src/types.rs b/bot/crates/strategy/src/types.rs index bedd612..66b8807 100644 --- a/bot/crates/strategy/src/types.rs +++ b/bot/crates/strategy/src/types.rs @@ -239,6 +239,7 @@ impl SandoRecipe { value: Some(self.frontrun.value.into()), data: Some(self.frontrun.data.into()), nonce: Some(nonce), + // What is the transaction_type with this constructor? access_list: access_list_to_ethers(self.frontrun.access_list), max_fee_per_gas: Some(self.target_block.base_fee_per_gas.into()), ..Default::default() diff --git a/bot/sando-bin/src/main.rs b/bot/sando-bin/src/main.rs index 9c77bd8..f3e6dc3 100644 --- a/bot/sando-bin/src/main.rs +++ b/bot/sando-bin/src/main.rs @@ -43,8 +43,8 @@ async fn main() -> Result<()> { engine.add_collector(Box::new(block_collector)); // Setup mempool collector - let mempool_collector = Box::new(MempoolCollector::new(provider.clone())); - let mempool_collector = CollectorMap::new(mempool_collector, Event::NewTransaction); + let mempool_collector: Box>> = Box::new(MempoolCollector::new(provider.clone())); + let mempool_collector = CollectorMap::new(mempool_collector, Event::NewTransaction); engine.add_collector(Box::new(mempool_collector)); // Setup strategy