2

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!

patatafly
  • 144
  • 10
  • Please edit your question and post the `contract.json` contents. It should be a standardized ABI-encoded structure and data. My guess is that web3 throws this error when it's not in the expected format... Also can you check if the `contractJSON` contains the string content, or the JS parsed object? – Petr Hejda Sep 01 '21 at 17:48
  • Edited. I posted part of it – patatafly Sep 01 '21 at 18:20
  • can u console.log(data) to see what jquery brings – Yilmaz Sep 01 '21 at 22:02
  • No, I can't. Aparently the error comes before..., but it shound't be jquerys error...I also tried with the path of the json file, and I keep getting the same error – patatafly Sep 02 '21 at 11:47

1 Answers1

3

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)
JMP
  • 1,864
  • 2
  • 8
  • 16