Solidity

呼叫已部署合約的方法失敗

  • July 17, 2021

部署到Rinkeby的合約


合約程式碼

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.3;

contract Cont {

 address[] players;
 uint test = 123;

function enter() public payable { // send() from web3 sucsseded
   require(msg.value > .01 ether, "Sender don't have enogth ether");
   players.push(msg.sender);
 }

function getTest() public view returns (uint) { // call() from Web3 fails
   return test;
 }
}

合約執行腳本程式碼

從執行node

web3 JS- v1.4.0

const Web3 = require('web3')

const ADDRESS = '0x97CBBa2EBC8Be957c0a78e93230021E4d4b7Ab90'
const ABI = [
{
 inputs: [],
 name: 'enter',
 outputs: [],
 stateMutability: 'payable',
 type: 'function',
 constant: undefined,
 payable: true,
 signature: '0xe97dcb62'
},
{
 inputs: [],
 name: 'getTest',
 outputs: [Array],
 stateMutability: 'view',
 type: 'function',
 constant: true,
 payable: undefined,
 signature: '0xa8cd0a80'
}
]


const web3 = new Web3("https://rinkeby.infura.io/v3/640cae69b9ac44c4bb7090bd71dc2ec3")
const contract = new web3.eth.Contract(ABI, ADDRESS) 

contract.methods.getTest().call().then(
  (res) => {console.log(res); // (node:9358) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'match' of undefined
});

注意: send() 方法有效。在另一份契約上進行了測試。

你在你的契約上返回 uint 但你的 abi 說它是數組我不確定但你應該檢查你的 abi。由於聲譽問題,我無法添加評論。

問題出在自定義編譯腳本/ABI 中。使用truffle lib編譯和部署合約。問題消失了。

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