使用智能合约调用Uniswap交换token时出现错误

在调用时遇到问题 eth -> tokens可以正常调用, tokens -> eth 一直报错 Fail with error 'TransferHelper: ETH_TRANSFER_FAILED' 请问是什么原因呢?

pragma solidity 0.8.0;

import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";

contract test {

    address internal owner;
    address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
    IUniswapV2Router02 private uniswapRouter;

    constructor() {
        uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
        owner = msg.sender; 
        }

    //owner
    modifier isOwner(){
        require(msg.sender == owner, "Caller is not owner");
        _;
        }

    //eth -> tokens -> eth
    function swap(address tokenAddress, uint EthInAmount, uint EthOutAmount, uint TokenOutAmountMin, uint deadline) public payable {
        //eth to tokens
        uint amountEthIn = EthInAmount;
        uint amountOutMin = TokenOutAmountMin;
        uniswapRouter.swapExactETHForTokens{ value: amountEthIn }(amountOutMin, getPathForEthToToken(tokenAddress), address(this), deadline);
        //tokens to eth 
        IERC20 token = IERC20(tokenAddress);
        token.approve(UNISWAP_ROUTER_ADDRESS, TokenOutAmountMin);
        uniswapRouter.swapExactTokensForETH(TokenOutAmountMin, EthOutAmount, getPathForTokenToEth(tokenAddress), address(this), deadline);
        }

    function EthToTokens(address tokenAddress, uint EthInAmount, uint TokenOutAmountMin, uint deadline) public payable {
        uniswapRouter.swapExactETHForTokens{ value: EthInAmount }(TokenOutAmountMin, getPathForEthToToken(tokenAddress), address(this), deadline);
        }

    function TokensToEth(address tokenAddress, uint EthOutAmount, uint TokenInAmount, uint deadline) public payable {
        IERC20 token = IERC20(tokenAddress);
        token.approve(UNISWAP_ROUTER_ADDRESS, TokenInAmount);
        uniswapRouter.swapExactTokensForETH(TokenInAmount, EthOutAmount, getPathForTokenToEth(tokenAddress), address(this), deadline);
        }

    function getPathForEthToToken(address tokenAddress) private view returns (address[] memory) {
        address[] memory path = new address[](2);
        path[0] = uniswapRouter.WETH();
        path[1] = tokenAddress;
        return path;
        }    

    function getPathForTokenToEth(address tokenAddress) private view returns (address[] memory) {
        address[] memory path = new address[](2);
        path[0] = tokenAddress;
        path[1] = uniswapRouter.WETH();
        return path;
        }

    function sendEther() public payable{
    }

    function withdraw(address payable _address, uint withdrawAmount) public payable isOwner{
        _address.transfer(withdrawAmount);
    }

    }
请先 登录 后评论

最佳答案 2021-03-01 11:21

当前你的合约没办法接收 ETH, 需要实现,receive 函数 或 fallback 函数。

参考文档: https://learnblockchain.cn/docs/solidity/contracts.html#receive

请先 登录 后评论

其它 1 个回答

jfh860813
请先 登录 后评论
  • 3 关注
  • 1 收藏,11795 浏览
  • 0x999 提出于 2021-02-28 19:52