patatafly
patatafly

Reputation: 164

Initiating a contract. Uncaught Error: You must provide the json interface of the contract when instantiating a contract object

I'm trying to initiate a contract in this way:

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

I've also tried with

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

But I don't think that second option is necesary.

I have a large contract.json file, so I only post a part of it:

{
  "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"
    },


     ...

I don't know if $getJSON needs the path of the json file as an argument, or just the name as I have it. I've seen it writen both ways in different pages. Either way I get this error:

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

Hope someone can help me!

Upvotes: 2

Views: 825

Answers (1)

JMP
JMP

Reputation: 1934

I had the same error The fix was to provide the abi property of the interface object to web3.eth.Contract instead of the whole object. E.g:

//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)

Upvotes: 3

Related Questions