Solidity

Solidity 函式執行已恢復。我的程式碼有什麼問題?

  • December 27, 2021

我正在嘗試從 QuickSwap 獲得對儲備金,但 EVM 恢復了我的通話。我的程式碼有什麼問題?

interface IUniswapV2Factory {
   function getPair(address tokenA, address tokenB) external view returns(address pair);
}

interface IUniswapV2Pair {
   function token0() external view returns(address);

   function token1() external view returns(address);

   function getReserves() external view returns(uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

contract myContract {
   // router address
   address private _router = 0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff;

   // Configure Router and get WETH address
   IUniswapV2Router01 Router = IUniswapV2Router01(_router);


   // Setup factory
   address private _factory = Router.factory();
   IUniswapV2Factory Factory = IUniswapV2Factory(_factory);


   function getReserve(address _pairToken, address _token) public view onlyUsers returns(uint256[] memory reserves) {


       reserves[0] = 0; // default values
       reserves[1] = 0;

       // Get pair address and if it doesn't exists then return 0
       address pair = Factory.getPair(_pairToken, _token);
       if (pair == address(0)) {
           return reserves;
       }
       // Setup interface for pair
       IUniswapV2Pair PAIR = IUniswapV2Pair(pair);
       (uint256 res0, uint256 res1, ) = PAIR.getReserves();

       // Sort reserves
       (uint256 pairRes, uint256 tokenRes) = _pairToken < _token ? (res0, res1) : (res1, res0);
       reserves[0] = pairRes;
       reserves[1] = tokenRes;
   }
}

感謝您的關注

我發現我忘記在“getReserve”函式中設置數組大小並將這段程式碼添加到函式的開頭修復了錯誤

reserves = new uint256[](2);

引用自:https://ethereum.stackexchange.com/questions/117420