1

I've got his error in Solidity when compiling my test file: TypeError: Cannot destructure property interface of 'undefined' or 'null'.

I know it is related to my compile.js file, but what is unusual is that my tests compiled just fine until I tried to update the solidity compiler version. I then uninstalled the updated compiler version and restored the version it compiled with before but now have got this error.

This makes me think it is not related to a mistake in my compile.js file but to do with the solidity compiler. However, when I check the solc version (using solcjs --version), I have correct version that is compatible with my code.

My compile.js file is:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8');

module.exports = solc.compile(source, 1).contracts[':Lottery'];

my test file is:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const {interface, bytecode } = require('../compile');

let lottery;
let accounts;

beforeEach(async () => {
    accounts = await web3.eth.getAccounts(); // unlocked accounts 
created automatically for us with ganache

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

describe('Lottery Contract', () => {

    it('deploys a contract', () => {
        assert.ok(lottery.options.address);
    });
});

Please help, this error has paralysed my work!

Some additional information, when I changed the solc compiler version, the pragma solidity ^0.5.0 declaration was underlined with a red squiggle indicating and error. When I hovered on the error it said the file requires a different compiler version (the old version that was used). How can the file be unable to recognised that the solc version has been updated? Could this be to do with the JSON files?

1

1 Answer 1

1

you are using old api on new version of solc.

here is the implementation.

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8');
var input = {
  language: "Solidity",
  sources: { "test.sol": { content: source } },
  settings: {
    outputSelection: {
      "*": {
        "*": ["*"]
      }
    }
  }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log("output",output) //allways check what you are getting

module.exports = output.contracts["test.sol"]["Lottery"];

**NOTE:**solc.compile(source, 1).contracts[':Lottery'] IS NOT VALID

in your test file

const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const compile = require("../compile");

//web3 needs prodvider to connect a network
const OPTIONS = {
  defaultBlock: "latest",
  transactionConfirmationBlocks: 1,
  transactionBlockTimeout: 5
};
const provider = ganache.provider();
const web3 = new Web3(provider, null, OPTIONS);

const interface = compile.abi; //this is abi and it is not in JSON format anymore. so we will pass this new web3.eth.Contract() without parsing
const bytecode = compile.evm.bytecode.object; //bytecode

let accounts;
let myContract;
beforeEach(async () => {
  //get a list of all accounts
  accounts = await web3.eth.getAccounts();

  myContract = await new web3.eth.Contract(interface)
    .deploy({ data: "0x" + bytecode, arguments: ["hi there"] })
    .send({ gas: 5000000, from: accounts[0] });
});
//"0x" tells js compiler that it will compile the hex code
describe("testign the deployment", () => {
  it("deploy the contract", () => {
    console.log(myContract);
    assert.ok(myContract.options.address);
  });
});

NOTE: when you install ganach-cli, ganach-core should be automatically installed. If not npm i --save ganache-core

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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