loom_strategy_backrun/
affected_pools.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
use std::collections::BTreeMap;

use alloy_primitives::Address;
use eyre::Result;
use loom_core_actors::SharedState;
use loom_types_blockchain::GethStateUpdateVec;
use loom_types_entities::{Market, PoolWrapper};

pub async fn get_affected_pools(
    market: SharedState<Market>,
    state_update: &GethStateUpdateVec,
) -> Result<BTreeMap<PoolWrapper, Vec<(Address, Address)>>> {
    let market_guard = market.read().await;

    let mut affected_pools: BTreeMap<PoolWrapper, Vec<(Address, Address)>> = BTreeMap::new();

    for state_update_record in state_update.iter() {
        for (address, _state_update_entry) in state_update_record.iter() {
            if let Some(pool) = market_guard.get_pool(address) {
                if affected_pools.contains_key(pool) || !market_guard.is_pool(address) {
                    continue;
                }
                let swap_directions = pool.get_swap_directions();
                affected_pools.insert(pool.clone(), swap_directions.clone());
            }
        }
    }

    Ok(affected_pools)
}