npm solc: AssertionError [ERR_ASSERTION]: Invalid callback specified
Asked Answered
N

15

24

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.

Navarro answered 17/11, 2018 at 16:29 Comment(2)
Are you sure this error is from solc. Can you debug and see where exactly you get the error from?Ventose
There was a bug I guess. Installing [email protected] worked.Navarro
O
50

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]
Overlooker answered 17/11, 2018 at 17:36 Comment(0)
B
48

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))));
Burgomaster answered 23/6, 2019 at 14:9 Comment(1)
This should be the accepted answer simply downgrading the version is not a solutionEmotional
F
6

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;

Fetch answered 9/1, 2019 at 13:3 Comment(0)
O
4

This is because the version mismatch of Solidity compiler. Please note or verify the solidity compiler version in which you want to work. For example: If you are doing work in

pragma solidity ^0.4.17

then you have to install 0.4.17 solidity compiler version like this:

npm install [email protected]

in the command prompt or terminal.

Occam answered 6/2, 2019 at 23:33 Comment(0)
T
4

with solc 0.7.1:


function compileContract() {
    let voterSOl = fs.readFileSync('./contracts/voter.sol' , 'utf8')
    let complierInput = {
        language: 'Solidity',
        sources:
        {
            'voter.sol': 
            {
                content: voterSOl
            }
        },
        settings:
        {
            optimizer:
            {
                enabled: true
            },
            outputSelection:
            {
                '*':{
                    '*':['*']
                }
            }
        }
    };
    console.log('compiling contract');
    let compiledContract = JSON.parse(solc.compile(JSON.stringify(complierInput)));
    console.log('Contract Compiled');
    for (let contractName in compiledContract.contracts['voter.sol']) {
        console.log(contractName , compiledContract.contracts['voter.sol'][contractName].abi);      
        let abi = compiledContract.contracts['voter.sol'][contractName].abi;
        fs.writeFileSync(`./contracts/bin/${contractName}_abi.json` , JSON.stringify(abi));
        return compiledContract.contracts['voter.sol'][contractName];
    }
}
Triumph answered 8/9, 2020 at 6:59 Comment(0)
A
3

If you have see error like this.You must do following two steps.

  1. Uninstall solc:

    npm uninstall solc

  2. Reinstall one of two versions:

The version used in the course:

npm install --save [email protected]

or

The newest version that will not break:

npm install --save [email protected]

source - Udemy - Ethereum and Solidity The Complete Developer's Guide

Ashliashlie answered 30/6, 2019 at 2:4 Comment(1)
but going fowardBamford
F
1

This is generally the assertion error between the solidity compiler that you have installed and the solidity compiler version that you are using in the solidity contract file. If you are using

npm install --save [email protected]

to install solc in your mac then, please use the same version of pragma in your solidity file like the following

pragma solidity^0.4.25

Fid answered 19/5, 2019 at 13:58 Comment(0)
N
1

I fixed the error with adding "npm install --save [email protected]" in cmd. Error is the solidity version. you need to install old version of solidity to execute the compile script

Natural answered 18/5, 2020 at 12:22 Comment(0)
M
1

sol ver: 0.8.17

Compile:

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

const buildPath = path.resolve(__dirname, 'build');
fs.removeSync(buildPath);

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

var input = {
    language: 'Solidity',
    sources: {
        'Campaign.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
fs.ensureDirSync(buildPath);

for(contractName in output.contracts['Campaign.sol']){
    fs.outputJSONSync(
        path.resolve(buildPath, contractName + '.json'),
        output.contracts['Campaign.sol'][contractName]
    );
}

Deploy:

const HDWalletProvider = require('@truffle/hdwallet-provider');
const Web3 = require('web3');
const compiledFactory = require('./build/CampaignFactory.json');

const provider = new HDWalletProvider(
    process.env.NEXT_PUBLIC_META_MASK,
    process.env.NEXT_PUBLIC_INFURA_API
);

const web3 = new Web3(provider);

const deploy = async () => {
    const accounts = await web3.eth.getAccounts();
    console.log('attempting to deploy from account: ', accounts[0]);

    const result = await new web3.eth.Contract(compiledFactory.abi)
    .deploy({data: compiledFactory.evm.bytecode.object})
    .send( {from:accounts[0], gas:'3000000'});

    console.log('Contract deployed to: ', result.options.address);
    provider.engine.stop();
};

deploy();
Mixologist answered 11/10, 2022 at 11:49 Comment(0)
P
0

Check your code in Remix first, then check compiler version

Perjure answered 1/2, 2019 at 14:45 Comment(0)
T
0
  • unistall solidity compiler

  • using - npm uninstall solc

  • then install required version of solidity compiler

  • using - npm install [email protected]

Trichinosis answered 26/12, 2019 at 6:39 Comment(0)
D
0

Remember to check solidity version and solc version . They should be equal.

Defilade answered 11/10, 2021 at 6:11 Comment(0)
S
0

Basically, it's a version issue. Some code syntax will change on each update. You don't need the 'filesystem' and 'path' libraries. To solve this issue, try this code:

const solc = require('solc');

var input = {
    language: 'Solidity',
    sources: {
        'hello.sol': {
            content: 'contract hello { function f() public { } }'
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output);

From this, you will get bytecode.

Semirigid answered 30/10, 2021 at 13:49 Comment(0)
K
0

The version of Solidity you are using and the solidity compiler(solc) you are using should be same .

If pragma solidity^0.4.17 then solc-->version = 0.4.17

Kirkkirkcaldy answered 7/12, 2021 at 22:38 Comment(0)
S
0

sol version 0.8.6

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

const buildPath = path.resolve(__dirname, 'build');
fs.removeSync(buildPath); // remove build folder

// Read 'hello.sol' file from the 'contracts' folder
const helloPath = path.resolve(__dirname, 'contracts', 'hello.sol');
const source = fs.readFileSync(helloPath, 'utf8');

var input = {
    language: 'Solidity',
    sources: {
        'hello.sol': {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
};


var output = JSON.parse(solc.compile(JSON.stringify(input)));

fs.ensureDirSync(buildPath); // ensure build folder exists

for (let contract in output) {
    fs.outputJSONSync(
        path.resolve(buildPath, contract + '.json'),
        output[contract]
    );
}
Secco answered 12/6, 2022 at 2:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.