Solidity

無法使用 web3 呼叫外部視圖函式

  • August 11, 2021

我需要從我的契約中呼叫一個 getter 函式,但我收到以下錯誤:

Error Error: Internal JSON-RPC error.
{
 "message": "VM Exception while processing transaction: revert",
 "code": -32000,
 "data": {
   "0xc3a8b1710c15f6ba59f9fefea5d9da656aabb8495834fb56c9b4277df741d610": {
     "error": "revert",
     "program_counter": 305,
     "return": "0x"
   },
   "stack": "RuntimeError: VM Exception while processing transaction: revert\n    at Function.RuntimeError.fromResults (/tmp/.mount_ganach04KHEZ/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n    at /tmp/.mount_ganach04KHEZ/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:568:26",
   "name": "RuntimeError"
 }
}
   at Object._fireError (index.js:50)
   at sendTxCallback (index.js:540)
   at cb (util.js:689)
   at callbackifyOnRejected (util.js:666)
   at Item.push../node_modules/process/browser.js.Item.run (browser.js:153)
   at drainQueue (browser.js:123)

我的合約版本是^0.8.0,函式相關的程式碼是:

ItemForSale[] public itemsForSale;
function totalItemsForSale() external view returns(uint256) {
   return itemsForSale.length;
 }

我嘗試使用 web3 從前端呼叫該函式是:

const totalItemsForSale = await marketplaceContract.methods.totalItemsForSale().call();

我剛剛發現了問題。我正在將另一個契約中的地址傳遞給 MarketplaceContract。一個愚蠢的錯誤。

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