【问题标题】:Swap Exact Tokens for Tokens Errors为代币错误交换确切代币
【发布时间】:2021-06-06 02:10:19
【问题描述】:

一段时间以来,我一直在尝试通过 uniswap 进行 Swap ExactTokenforToken 交换。不断提出问题。

我确实有一个成功部署的合约,但它不断收到传输错误。通过查看文档、帖子、此论坛等,我的理解是我没有转移/批准事件。

这是已部署但不能交换的代码,供参考:

pragma solidity 0.7.1;

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

contract UniswapExample {
  address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;

  IUniswapV2Router02 public uniswapRouter;
  address private multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
  address private USDC = 0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5;
  
  constructor() {
    uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
  }
  
  function convertUSDCtoEthtoDai(uint daiAmount, uint USDCAmount) public payable {
    uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend!
    uniswapRouter.swapExactTokensForTokens(USDCAmount, daiAmount, getPathForETHtoDAI(), address(this), deadline);
    // refund leftover ETH to user
    (bool success,) = msg.sender.call{ value: address(this).balance }("");
    require(success, "refund failed");

  }
  
  
  function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) {
    return uniswapRouter.getAmountsIn(daiAmount, getPathForETHtoDAI());
  }

  function getPathForETHtoDAI() private view returns (address[] memory) {
    address[] memory path = new address[](3);
    path[0] = USDC;
    path[1] = uniswapRouter.WETH();
    path[2] = multiDaiKovan;
    
    return path;
  }
 
  
  // important to receive ETH
  receive() payable external {}
}

所以我按照自己的方式编写了以下代码,但是,我在“.transferFrom”上收到一个错误,提示“TypeError: Member “transferFrom” not found or not visible after argument-dependent lookup in address。”

.approve 也是如此。我已经为 Router02 和 IERC20 导入了 IERC20 接口。所以我无法部署合同来测试我是否接近。

这是新代码:

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

contract UniswapExample2 {

    address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;

    IUniswapV2Router02 public uniswapRouter;
    address private multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
    address private USDC = 0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5;
  
    constructor() {
     uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
  }
    function swapTokenForETH(uint amountIn, uint amountOutMin, uint deadline) public payable returns(uint[] memory) {
        // transfer
        require(USDC.transferFrom(msg.sender, address(this), amountIn), 'transferFrom failed.');
        // approve
        require(USDC.approve(UNISWAP_ROUTER_ADDRESS, amountIn), 'approve failed.');
        // swap
        return uniswapRouter.swapExactTokensForTokens(amountIn, amountOutMin, getPathForETHtoDAI(), msg.sender, deadline);
    }
    
    
    function getPathForETHtoDAI() private view returns (address[] memory) {
    address[] memory path = new address[](3);
    path[0] = USDC;
    path[1] = uniswapRouter.WETH();
    path[2] = multiDaiKovan;
    
    return path;
  }
}

我也尝试在实际合约中复制函数,但随后我收到一条错误消息,提示必须将整个合约标记为抽象。

任何建议/指导将不胜感激!

【问题讨论】:

    标签: token solidity swap


    【解决方案1】:

    您正在尝试对 address 类型(未定义)执行 transferFrom() 函数。

    <address>.transferFrom()
    

    它需要在定义transferFrom()函数的接口上。​​

    interface IERC20 {
        function transferFrom(address _from, address _to, uint256 _amount) external returns (bool);
    }
    
    contract UniswapExample2 {
        IERC20 private USDC = IERC20(0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5);
    
        function swapTokenForETH() public {
            USDC.transferFrom();
        }
    }
    

    注意:您已经(正确地)使用IUniswapV2Router02 接口、uniswapRouter 属性和方法swapExactTokensForTokens() 做同样的事情。

    【讨论】:

    • 感谢您的回答。这很有意义!
    猜你喜欢
    • 2020-01-23
    • 2021-07-17
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2022-07-29
    相关资源
    最近更新 更多