6

That's the error that occurs when I try to test my contract.

Can someone tell me what the problem is?

That's the code for the test:

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);
  });
});
9
  • 1
    Can you please console.log(typeof JSON.parse(compiledFactory).interface) and tell us what you get? – goodvibration Mar 18 '20 at 13:56
  • Alright so I tryed and I get "undefined" – Gianna Mar 18 '20 at 15:10
  • 1
    This means that you have no interface field in the JSON object which the string compiledFactory is parsed into. Print it (or simply open the file which you read it from), and find out what fields you have there, and which one of them represents a legal ABI. – goodvibration Mar 18 '20 at 15:14
  • I tryed with JSON.parse(compiledFactory).abi and that seems to be right (you was right, I don't have any Interface property but instead I've got ABI). The problem is that when I test I got another Error:"cannot read property 'send' of null". What does this mean? – Gianna Mar 18 '20 at 15:19
  • 1
    Well, that's a different question than the one asked here (about "Error: you must provide the json interface of the contract when instantiating a contract object"). I suggest that you first debug this problem for a while, and then post it as a separate question, including all relevant information from your debug session (i.e., don't just post every error that you get along the way). – goodvibration Mar 18 '20 at 15:40
3

You could instead try something like this to instantiate a contract :

  • copy paste the abi of your contract in a json file

  • read the file : const contractJson = fs.readFileSync('path_to_abi_file.json');

  • parse the file : const abi = JSON.parse(contractJson);

  • instantiate the contract : contractInstance = new web3.eth.Contract(abi);

2
  • 2
    You just made up a file named path_to_abi_file.json, which you want to contain a proper ABI, but that's exactly the problem that this user is dealing with in her input file. If she knew how to create such file, then she wouldn't have this problem to begin with. Her input file obviously has a different format (some other object, with a field named interface). So I doubt that your answer could contribute much. – goodvibration Mar 18 '20 at 14:52
  • It's clearly a bloke... – Rambatino Jun 4 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.