Solidity

得到錯誤`錯誤:無法估計氣體;將方法發送到合約時,交易可能會失敗或可能需要手動限制氣體

  • November 3, 2022

我將智能合約部署到 Ganache,如下程式碼:

// SPDX-License-Identifier: MIT
// Tells the Solidity compiler to compile only from v0.8.13 to v0.9.0
pragma solidity ^0.8.13;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./ConvertLib.sol";

// This is just a simple example of a coin-like contract.
// It is not ERC20 compatible and cannot be expected to talk to other
// coin/token contracts.

contract MetaCoin {
       AggregatorV3Interface internal priceFeed;

   constructor() {
       
               priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
   }

       function getLatestPrice() public view returns (int) {
       (
           /*uint80 roundID*/,
           int price,
           /*uint startedAt*/,
           /*uint timeStamp*/,
           /*uint80 answeredInRound*/
       ) = priceFeed.latestRoundData();
       return price;
   }
}

當我getLatestPrice在 chrome 中從 web3 呼叫該方法時,

coinContract.methods.getLatestPrice().send({ from: xxxx });

MetaMask 錢包彈出但顯示錯誤We were not able to estimate gas. There might be an error in the contract and this transaction may fail.

在此處輸入圖像描述

但是如果我將程式碼更改為使用call而不是send

coinContract.methods.getLatestPrice().call();

我收到了這個錯誤:

{
 "message": "VM Exception while processing transaction: revert",
 "code": -32000,
 "data": {
   "0xc431aacd735f5e37f257d9ce3ad00d0ce66c41447ac8d8b16760a6dbc76eb8e2": {
     "error": "revert",
     "program_counter": 1020,
     "return": "0x"
   },
   "stack": "RuntimeError: VM Exception while processing transaction: revert\n    at Function.RuntimeError.fromResults (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n    at /Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:568:26",
   "name": "RuntimeError"
 }
}

我不知道我的程式碼有什麼問題

實際上,您的本地分叉上的 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e 是否部署了 v3 聚合器?

您想檢查您部署到 Ganache 的合約地址和 ABI 是否正確。如果是,請檢查您呼叫的方法是否正確。最後,檢查函式呼叫中的參數是否正確。

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