Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm solc: AssertionError [ERR_ASSERTION]: Invalid callback specified

I am trying to compile solidity smart contract using npm solc. I tried to follow different examples. Link to example: https://medium.com/coinmonks/how-to-compile-a-solidity-smart-contract-using-node-js-51ea7c6bf440

I wrote my code like following:

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



const helloPath = path.resolve(__dirname, 'contracts', 'hello.sol');
console.log("First" + helloPath);
const source = fs.readFileSync(helloPath, 'UTF-8');
console.log("Second" + source);
console.log(solc.compile(source, 1));

I am getting following error when running the above code.

AssertionError [ERR_ASSERTION]: Invalid callback specified.
    at wrapCallback (C:\Users\mouazzamj058\solc_example\node_modules\solc\wrapper.js:16:5)
    at runWithReadCallback (C:\Users\mouazzamj058\solc_example\node_modules\solc\wrapper.js:37:42)
    at compileStandard (C:\Users\mouazzamj058\solc_example\node_modules\solc\wrapper.js:78:14)
    at Object.compileStandardWrapper (C:\Users\mouazzamj058\solc_example\node_modules\solc\wrapper.js:85:14)
    at Object.<anonymous> (C:\Users\mouazzamj058\solc_example\example.js:4:19)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

Please help.

like image 331
Mouazzam Avatar asked Nov 17 '18 16:11

Mouazzam


People also ask

What is Node JS SOLC?

solcjs is a Node. js library and command-line tool that is used to compile solidity files. It doesn't use the solc command-line compiler; instead, it compiles purely using JavaScript, so it's much easier to install than solc. Solc is the actual Solidity compiler.

What is SOLC solidity?

solc converts from a high-level solidity language into Ethereum Virtual Machine (EVM) bytecode so that it can be executed on the blockchain by EVM. Get Advanced Blockchain Development now with the O'Reilly learning platform.

How do I check my solidity version?

You can look at the file ./node_modules/solc/package. json and see something like "solc@^x.y.z" where x.y.z is the version number that is being used. Save this answer.


3 Answers

Which version of solc are you using?

Solc released a breaking version the other day, this error is related to that.

npm uninstall solc
npm install [email protected]
like image 107
Falko Avatar answered Nov 02 '22 00:11

Falko


If you are using latest version ie. 0.5.9 there is change in how you compile the code.

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



const helloPath = path.resolve(__dirname, 'contracts', 'hello.sol');
const source = fs.readFileSync(helloPath, 'UTF-8');

var input = {
    language: 'Solidity',
    sources: {
        'hello.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}; 
console.log(JSON.parse(solc.compile(JSON.stringify(input))));
like image 45
kushal parikh Avatar answered Nov 02 '22 00:11

kushal parikh


This is because of version mismatch of solidity compiler installed during solc package installation and the compiler mentioned in the solidity file.To solve this issue try

install:

npm install [email protected]

in solidity file use :

pragma solidity^0.4.25;

like image 6
Vader Avatar answered Nov 01 '22 23:11

Vader