簡體   English   中英

UniswapV2 getAmountsOut 執行在沒有原因字符串的情況下恢復

[英]UniswapV2 getAmountsOut execution reverts without a reason string

我正在 fantom.network 上編寫 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);
  });

等待您寶貴的幫助。

使用 remix 調試器總是有助於查看合約實際恢復的斷點。 從那里嘗試理解為什么它會恢復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM