Solidity – Declaring Functions of Multiple Smart Contracts Inside a Single Interface

contract-developmentinterfacessolidityuniswap

pragma solidity ^0.7.0;

interface IUniswap {
  function swapExactTokensForETH(
    uint amountIn, 
    uint amountOutMin, 
    address[] calldata path, 
    address to, 
    uint deadline)
    external
    returns (uint[] memory amounts);
  function WETH() external pure returns (address);
}

interface IERC20 {
  function transferFrom(
    address sender, 
    address recipient, 
    uint256 amount) 
    external 
    returns (bool);
}

contract MyDeFiProject {
  IUniswap uniswap;

  constructor(address _uniswap) {
    uniswap = IUniswap(_uniswap);
  }

  function swapTokensForEth(address token, uint amountIn, uint amountOutMin, uint deadline) external {
    IERC20(token).transferFrom(msg.sender, address(this), amountIn);
    address[] memory path = new address[](2);
    path[0] = address(DAI);
    path[1] = uniswap.WETH();
    IERC20(token).approve(address(uniswap), amountIn);
    uniswap.swapExactTokensForETH(
      amountIn, 
      amountOutMin, 
      path, 
      msg.sender, 
      deadline
    );
  }
}

In the above interface IUniswap, the function swapExactTokensForETH has been taken from IUniswapV2Router02.sol, whereas, the function WETH has been taken from IUniswapV2Router01.sol. This has been shown in the YouTube channel of EatTheBlocks. This is the video.

My question is how is it possible to call two different functions of separate smart contracts from the same interface?

Best Answer

This is because IUniswapV2Router02.sol inherits IUniswapV2Router01.sol. Therefore you may think of Router02 interface as an extension of Router01 interface with all of its functions.

Related Topic