首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:在实例化契约对象时,必须提供契约的json接口。

错误:在实例化契约对象时,必须提供契约的json接口。
EN

Ethereum用户
提问于 2020-03-18 13:44:22
回答 1查看 17.3K关注 0票数 7

这就是当我尝试测试我的合同时发生的错误。

有人能告诉我问题出在哪里吗?

这是测试的代码:

代码语言:javascript
复制
const assert = require('assert');
const fs = require('fs');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledFactory = fs.readFileSync('./ethereum/build/LotteriaFactory.json', 'utf8');
const compiledLotteria = fs.readFileSync('./ethereum/build/Lotteria.json', 'utf8');

let accounts;
let factory;
let lotteriaAddress;
let lotteria;

beforeEach(async () => {
  accounts = await web3.eth.getAccounts();

  factory = await new web3.eth.Contract(JSON.parse(compiledFactory).interface)
  .deploy({ data: (compiledFactory).bytecode })
  .send({ from: accounts[0], gas: '1000000' });

  await factory.methods.createNew('5').send({
    from: accounts[0], //manager
    gas: '1000000'
  });

  const addresses = await factory.methods.active().call();
  lotteriaAddress = addresses[0]; //[LotteriaFactory]

   lotteria = new web3.eth.Contract(
     JSON.parse(compiledFactory).interface,
     lotteriaAddress
   );
});

describe('Lotteria', () => {
  it('deploy a factory and a lottery', () => {
    assert.ok(factory.options.address);
    assert.ok(lotteria.options.address);
  });
});
EN

回答 1

Ethereum用户

发布于 2020-03-18 14:33:43

您可以尝试这样的方法来实例化一个契约:

  • 将合同的abi复制到json文件中
  • 读取文件:const contractJson = fs.readFileSync('path_to_abi_file.json');
  • 解析文件:const abi = JSON.parse(contractJson);
  • 实例化契约:contractInstance = new web3.eth.Contract(abi);
票数 9
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/80671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档