启动合同.未捕获的错误:在实例化合同对象时,必须提供合同的json接口



我正试图用这种方式启动一项合同:

function initContract() {
var contractJSON = $.getJSON("contract.json", function (data) {
return data;
});
return new web3.eth.Contract(contractJSON);
}

我也试过

return new web3.eth.Contract(JSON.parse(contractJSON));

但是我不认为第二个选择是必要的。

我有一个大合同。json文件,所以我只发布它的一部分:

{
"contractName": "contract",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"indexed": false,
"internalType": "string",
"name": "hash",
"type": "string"
},
{
"indexed": false,
"internalType": "string",
"name": "nombre",
"type": "string"
},
{
"indexed": false,
"internalType": "string",
"name": "organizacion",
"type": "string"
},
{
"indexed": false,
"internalType": "string",
"name": "descripcion",
"type": "string"
},
{
"indexed": false,
"internalType": "address payable",
"name": "autor",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "donacionRecibida",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "donacionRequerida",
"type": "uint256"
}
],
"name": "proyectoDonado",
"type": "event"
},

...

我不知道$getJSON是否需要json文件的路径作为参数,或者只是我所拥有的名称。我在不同的书页上看到过两种写法。无论哪种方式,我得到这个错误:

Uncaught Error: You must provide the json interface of the contract when instantiating a contract object.
at Object.ContractMissingABIError (web3.min.js:30304)

希望有人能帮助我!

我有同样的错误,修复是提供接口对象的abi属性web3.eth。契约而不是整个对象。例句:

//Get the JSON abi interface definition in whichever way you prefer into an object.
let myInterface = require('../my_contracts/my_contract_interface.json')
//Pass in the abi property of the object
let contract = new this.web3.eth.Contract(myInterface.abi)

最新更新