【发布时间】: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