FlashLoan , Uniswap get pari and reserves

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;


interface UniswapV2Factory{
    
    
    function getPair(address tokenA, address tokenB) external view  returns (address pair);
       
    
    
}


interface UniswapV2Pair{
    
    
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestamoLast);
       
    
    
}


/*
factory: 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
router01: 0xf164fC0Ec4E93095b804a4795bBe1e041497b92a
router02: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D

address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;


pair = 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11

*/

contract UniswapExample{
    
    address private factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
    address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    
    function getReserves() external view returns (uint, uint){
        
        address pair = UniswapV2Factory(factory).getPair(dai, weth);
        
        (uint reserve0, uint reserve1, ) = UniswapV2Pair(pair).getReserves();
        
        return (reserve0, reserve1);
        
    }
    
    
}

我们通过继承Uniswap的UniswapV2Factory 路由合约找到pair地址,然后调用getReserve方法取到配对合约中的reserve0和reserve1 取出来。 这是非常关键。