loom_defi_pools/state_readers/
uniswapv3.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use alloy_primitives::aliases::U24;
use alloy_primitives::Address;
use alloy_sol_types::{SolCall, SolInterface};
use revm::primitives::Env;
use revm::DatabaseRef;

use loom_defi_abi::uniswap3::IUniswapV3Pool;
use loom_defi_abi::uniswap3::IUniswapV3Pool::slot0Return;
use loom_evm_utils::evm::evm_call;

pub struct UniswapV3StateReader {}

impl UniswapV3StateReader {
    pub fn factory<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<Address> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::factory(IUniswapV3Pool::factoryCall {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::factoryCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }

    pub fn token0<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<Address> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::token0(IUniswapV3Pool::token0Call {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::token0Call::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }

    pub fn token1<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<Address> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::token1(IUniswapV3Pool::token1Call {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::token1Call::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }

    pub fn fee<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<U24> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::fee(IUniswapV3Pool::feeCall {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::feeCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }

    pub fn tick_spacing<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<u32> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::tickSpacing(IUniswapV3Pool::tickSpacingCall {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::tickSpacingCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0.try_into()?)
    }

    pub fn slot0<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<slot0Return> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::slot0(IUniswapV3Pool::slot0Call {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::slot0Call::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return)
    }
    pub fn liquidity<DB: DatabaseRef>(db: &DB, env: Env, pool: Address) -> eyre::Result<u128> {
        let call_data_result =
            evm_call(db, env, pool, IUniswapV3Pool::IUniswapV3PoolCalls::liquidity(IUniswapV3Pool::liquidityCall {}).abi_encode())?.0;
        let call_return = IUniswapV3Pool::liquidityCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }
}