loom_defi_pools/state_readers/
erc20.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
use alloy_primitives::{Address, U256};
use alloy_sol_types::SolCall;
use alloy_sol_types::SolInterface;
use eyre::Result;
use loom_defi_abi::IERC20;
use loom_evm_utils::evm::evm_call;
use revm::primitives::Env;
use revm::DatabaseRef;

pub struct ERC20StateReader {}

#[allow(dead_code)]
impl ERC20StateReader {
    pub fn balance_of<DB: DatabaseRef>(db: &DB, env: Env, erc20_token: Address, account: Address) -> Result<U256> {
        let call_data_result =
            evm_call(db, env, erc20_token, IERC20::IERC20Calls::balanceOf(IERC20::balanceOfCall { account }).abi_encode())?.0;
        let call_return = IERC20::balanceOfCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }

    pub fn allowance<DB: DatabaseRef>(db: &DB, env: Env, erc20_token: Address, owner: Address, spender: Address) -> Result<U256> {
        let call_data_result =
            evm_call(db, env, erc20_token, IERC20::IERC20Calls::allowance(IERC20::allowanceCall { owner, spender }).abi_encode())?.0;
        let call_return = IERC20::allowanceCall::abi_decode_returns(&call_data_result, false)?;
        Ok(call_return._0)
    }
}