Solidity
在單個界面中聲明兩個不同智能合約的功能
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 ); } }
在上面的介面
IUniswap
中,函式swapExactTokensForETH
取自IUniswapV2Router02.sol,而函式WETH
取自IUniswapV2Router01.sol。這已在EatTheBlocks的 YouTube 頻道中顯示。這是影片。我的問題是如何從同一個介面呼叫不同智能合約的兩個不同功能?
這是因為 IUniswapV2Router02.sol 繼承了 IUniswapV2Router01.sol。因此,您可能會認為Router02 介面是Router01 介面的擴展,具有其所有功能。