const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require('../compile');
let accounts;
let inbox;
beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
// Use one of those accounts to deploy the contract
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode, arguments: ['Hi there!'] })
.send({ from: accounts[0], gas: '1000000' });
inbox.setProvider(provider);
});
describe('inbox', () => {
it('deploys a contract', () => {
assert.ok(inbox.options.address);
});
it('has a default message', async () => {
const message = await inbox.methods.message().call();
assert.equal(message, 'Hi there!');
});
});
Error lies with the require('../compile')
for sure as it must be returning a null.
Try referring to the code below for compilation of the solidity code. Check if your compile.js file is working in the similar manner or not.
// Compile the source code
const input = fs.readFileSync('Coin.sol');
const output = solc.compile(input.toString(), 1);
const bytecode = output.contracts[':Coin'].bytecode;
const abi = JSON.parse(output.contracts[':Coin'].interface);
Make sure you got const abi = JSON.parse(output.contracts[':Coin'].bytecode);
the 'C' of 'Coin.sol' in capitals in 'compile.js' cause that's just how it's stored in solidity
-
-
@urjitonrails simply check your compile.js file. Make sure you have got the first letter in caps as shown in the code above. – Apoorv Jain Jul 18 '18 at 22:11
The problem doesn't lie with the code provided. There's something wrong with your "compile" file.
const { interface, bytecode } = require('../compile');
is the issue,
require('../compile')
is returning undefined
or null
.
-
Yes, So how should I fix it ? I Bytecode is working. I am not sure about the inferface. Can you please help me out ? – 0xSHA1001 Jun 5 '18 at 13:55
-
Maybe post another question with your compile file if you can't figure it out. The bytecode isn't working either. The whole
require('../compile')
evaluates toundefined
ornull
. That's all that I can tell from what you've posted. – AnAllergyToAnalogy Jun 5 '18 at 14:30 -
Also note, the error you've posted has nothing to do with Ethereum, it's just a Javascript error. – AnAllergyToAnalogy Jun 5 '18 at 14:31
You need to make sure that you are exporting everything correctly from your compile.js
file to your test file. I have spelling error when I was exporting contract, my code bit here module.exports = solc.compile(source, 1).contracts[':Registration'];
the registration was :registration
instead of :Registration
and hence the {interface, bytecode } were not being exported from the
compile.js`.
I hope this helps. It was a typo, js error.
I also got the same error but make sure spelling of your contract name and the name you are using here: module.exports = solc.compile(source, 1).contracts[':Lottery']; and contract Lottery{ This must be identical.
For googlers:
The issue is with this is likely the versioning of the solc compiler. Bumping the version's dependency to one that is more recent may solve your issue.
Try the following:
rm -rf node_modules
// removenode_modules
rm -rf package-lock.json
// removepackage-lock.json
- Change the .sol file's import statement to:
pragma solidity ^0.4.25
- Change the dependency in package.json to
"solc": "^0.4.25"
npm install
// install new dependencies
Then you can run your tests
beforeEach
be inside thedescribe
? – mirg Jun 5 '18 at 9:24