Solidity

通過介面從另一個合約呼叫函式時,恢復交易

  • October 13, 2022

我到處搜尋,還仔細查看了我的簡單程式碼。當然我是一個新手,但感覺我的程式碼一切都很好(很簡單):

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface Dexes {
   function allPairsLength() external view returns (uint256);
}

contract callingDexInfo {
   address public constant uniswapV2 = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
   Dexes dexAddress = Dexes(uniswapV2);

   function getAllPairs() public view returns (uint256) {
       uint256 all = dexAddress.allPairsLength();
       return all;
   }
}

部署後看看部署了什麼。我首先看到它出現“餘額:0 ETH”。之後是兩個藍色按鈕:getAllPairs 和 uniswapV2。uniswapV2 按預期工作,但 getAllPairs 沒有給我來自 Uniswap 的契約工廠的 allPairsLength。它應該給我一個 uint256。

我得到的是終端上的一條消息:


revert
   The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

無法理解為什麼它要求為此付費。它只是讀取功能。對不起,但我不明白我做錯了什麼。

您的問題是您正在部署到 Remix 的內部虛擬機。該網路僅在您的 Remix 會話期間存在,並且未連接到任何外部網路。它沒有 Uniswap 合約。

Uniswap 僅部署到此處列出的網路。如果你想在你的 Remix 網路中擁有它,你必須首先自己在那裡部署它(這不是一件容易的事)。或者然後使用已經部署了 Uniswap 的測試網 - 您可以通過瀏覽器錢包使用 Remix 中的“注入提供程序”連接到這些測試網。

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