node.js - Error: You must provide the json interface of the contract when instantiating a contract object
问题描述
const Web3 = require('web3');
const web3 = new Web3('http://foodchain-node1.etherhost.org:22001');
const CONTRACT_ADDRESS = "0xA4fafbE0ea4823e262b4916EF93CC5A6306A5DBc";
async function eventQuery(){
'use strict';
var fs = require('fs');
var data = fs.readFileSync('food3.abi', 'utf-8');
//console.log(data);
//console.log(typeof JSON.parse(data).interface)
const CONTRACT_ABI = JSON.parse(data);
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS);
const START_BLOCK = 7700000;
const END_BLOCK = 7701000;
contract.getPastEvents("FoodSection",
{
fromBlock: START_BLOCK,
toBlock: END_BLOCK
})
.then(events => console.log(events))
.catch((err) => console.error(err));
}
eventQuery();
I created this code and everytime I ran it, it returns the error of "Error: You must provide the json interface of the contract when instantiating a contract object." I am guessing it have something to do with the json parse of the 'food3.abi' file but I do not know how to correct it. Could somebody please let me know what is my mistake and how to fix it?
解决方案
我尝试运行您的示例,但没有收到错误,因此您拥有的文件可能存在问题food3.abi
(例如,如果您从某处复制粘贴 ABI,您可能错过了 [])。
这是我遵循的步骤。也许它也对你有用。
- 安装 solc
npm install -g solc
- 编译合约
solcjs --abi contract.sol
。这将contract_sol_contract.abi
在同一文件夹中创建一个文件 fs.readFileSync()
在语句中使用生成的abi文件
推荐阅读
- javascript - Nextjs 样板开发服务器报错
- python - Python Telethon 检查是频道还是组
- windows - 由于 postgres.exe 没有输出,PostgreSQL initdb 在 windows docker 容器中失败
- excel - 了解十分位数计算以及 bin 的形成方式
- python - 绘制事件:plt.plot vs ax.plot,为什么后者不起作用?
- docker - 将 docker 容器托管到本地网络
- wordpress - 无法在 Wordpress 中更改网站语言
- javascript - 如何阻止我的“轮盘赌风格”号码选择器滞后?
- reactjs - 当我转到“联系人路径”时,我希望 App 中的组件不出现
- python - Django 表单提交重定向到错误的 url