问题
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.
回答1:
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 solc@0.4.25
回答2:
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))));
回答3:
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 solc@0.4.25
in solidity file use :
pragma solidity^0.4.25;
回答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 solc@0.4.17
in the command prompt or terminal.
回答5:
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 solc@0.4.25
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
回答6:
Check your code in Remix first, then check compiler version
回答7:
If you have see error like this.You must do following two steps.
Uninstall solc:
npm uninstall solc
Reinstall one of two versions:
The version used in the course:
npm install --save solc@0.4.17
or
The newest version that will not break:
npm install --save solc@0.4.25
source - Udemy - Ethereum and Solidity The Complete Developer's Guide
回答8:
unistall solidity compiler
using - npm uninstall solc
then install required version of solidity compiler
using - npm install solc@0.4.17
回答9:
I fixed the error with adding "npm install --save solc@0.4.25" in cmd. Error is the solidity version. you need to install old version of solidity to execute the compile script
回答10:
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];
}
}
来源:https://stackoverflow.com/questions/53353167/npm-solc-assertionerror-err-assertion-invalid-callback-specified