【问题标题】:Initiating a contract. Uncaught Error: You must provide the json interface of the contract when instantiating a contract object启动合同。未捕获的错误:实例化合约对象时必须提供合约的json接口
【发布时间】:2021-09-01 16:00:03
【问题描述】:

我正在尝试以这种方式启动合同:

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

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

我有一个很大的 contract.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)

希望有人可以帮助我!

【问题讨论】:

  • 请编辑您的问题并发布contract.json 内容。它应该是标准化的 ABI 编码结构和数据。我的猜测是,当 web3 不是预期的格式时,它会抛出此错误...您还可以检查 contractJSON 是否包含字符串内容或 JS 解析的对象?
  • 已编辑。我贴了一部分
  • 你能不能通过console.log(data)看看jquery带来了什么
  • 不,我不能。显然错误出现在...,但它应该不是 jquerys 错误...我也尝试了 json 文件的路径,但我一直收到同样的错误

标签: json solidity smartcontracts abi contract


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 2021-12-30
    • 2022-09-30
    • 2022-07-26
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多