loom_defi_pools/protocols/
uniswapv2.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
31
32
33
34
35
36
use alloy_primitives::{b256, Address, Bytes, B256};
use alloy_sol_types::SolCall;
use loom_defi_abi::uniswap2::IUniswapV2Pair;
use loom_defi_address_book::FactoryAddress;

use crate::protocols::helper::get_uniswap2pool_address;
use crate::protocols::match_abi;
use crate::protocols::protocol::Protocol;

pub struct UniswapV2Protocol {}

impl UniswapV2Protocol {
    pub fn is_code(code: &Bytes) -> bool {
        match_abi(
            code,
            vec![
                IUniswapV2Pair::swapCall::SELECTOR,
                IUniswapV2Pair::mintCall::SELECTOR,
                IUniswapV2Pair::syncCall::SELECTOR,
                IUniswapV2Pair::token0Call::SELECTOR,
                IUniswapV2Pair::factoryCall::SELECTOR,
            ],
        )
    }

    pub fn get_pool_address_for_tokens(token0: Address, token1: Address) -> Address {
        let init_code: B256 = b256!("96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f");
        get_uniswap2pool_address(token0, token1, FactoryAddress::UNISWAP_V2, init_code)
    }
}

impl Protocol for UniswapV2Protocol {
    fn get_pool_address_vec_for_tokens(token0: Address, token1: Address) -> Vec<Address> {
        vec![UniswapV2Protocol::get_pool_address_for_tokens(token0, token1)]
    }
}