【问题标题】:UniswapV2 getAmountsOut execution reverts without a reason stringUniswapV2 getAmountsOut 执行在没有原因字符串的情况下恢复
【发布时间】:2023-01-13 03:43:35
【问题描述】:

我正在 fantom 网络上编写 UniswapV2 的 LP 代币价格聚合器。

我完成了价格获取功能,所以我想在掉期之前和之后测试它们(想象闪贷等)

但是 getAmountsOutIUniswapV2Router02 的执行会在没有原因字符串的情况下恢复,所以我找不到合适的解决方案。

这是我的交换代码。

function convertEthToToken(
        address tokenAddress,
        uint tokenAmount,
        uint deadline
    ) public payable {
        address[] memory path = new address[](2);
        path[0] = IUniswapV2Router02(UNISWAP_V2_ROUTER).WETH();
        path[1] = tokenAddress;

        ERC20(tokenAddress).approve(UNISWAP_V2_ROUTER, tokenAmount);
        ERC20(IUniswapV2Router02(UNISWAP_V2_ROUTER).WETH()).approve(
            UNISWAP_V2_ROUTER,
            tokenAmount
        );
        IUniswapV2Router02(UNISWAP_V2_ROUTER).swapETHForExactTokens{
            value: msg.value
        }(tokenAmount, path, address(this), deadline);

        // refund leftover ETH to user
        msg.sender.call{value: address(this).balance}("");
    }

function swap(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn,
        address _to,
        uint256 _deadline
    ) public payable {
        // transfer the amount in tokens from msg.sender to this contract
        convertEthToToken(_tokenIn, _amountIn, _deadline);

        //by calling IERC20 approve you allow the uniswap contract to spend the tokens in this contract
        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn);

        address[] memory path;
        path = new address[](2);
        path[0] = _tokenIn;
        path[1] = _tokenOut;

        // @here occurs this error -> "Error: Transaction reverted without a reason string"!!!
        uint256[] memory amountsExpected = IUniswapV2Router02(UNISWAP_V2_ROUTER)
            .getAmountsOut(_amountIn, path);

        uint256[] memory amountsReceived = IUniswapV2Router02(UNISWAP_V2_ROUTER)
            .swapExactTokensForTokens(
                amountsExpected[0],
                (amountsExpected[1] * 990) / 1000, // accepting a slippage of 1%
                path,
                _to,
                _deadline
            );
        console.log("swap finished. ", amountsReceived[0]);
    }

这是我的测试代码。

it("swapping", async () => {
    const latestBlock = await ethers.provider.getBlockNumber();
    const timestamp = (await ethers.provider.getBlock(latestBlock)).timestamp;
    await priceOracle
      .connect(owner)
      .swap(FTM, WBTC, 100000, owner.address, timestamp + 1000, {
        value: 1000000,
      });
    await priceOracle.getSafePrice(FTM_BTC_LP);
  });

等待您宝贵的帮助。

【问题讨论】:

    标签: solidity router price revert uniswap


    【解决方案1】:

    使用 remix 调试器总是有助于查看合约实际恢复的断点。从那里尝试理解为什么它会恢复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      相关资源
      最近更新 更多