UniswapV2 getAmountsOut 执行在没有原因字符串的情况下恢复

问题描述 投票:0回答:1

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

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

但是

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

这是我的交换代码。

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 uniswap
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.