npm solc:AssertionError [ERR_ASSERTION]:指定了无效的回调

Mou*_*zam 9 node.js npm ethereum solidity

我正在尝试使用npm solc编译可靠性智能合约.我试着遵循不同的例子.链接到示例:https://medium.com/coinmonks/how-to-compile-a-solidity-smart-contract-using-node-js-51ea7c6bf440

我编写了如下代码:

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));
Run Code Online (Sandbox Code Playgroud)

运行上面的代码时出现以下错误.

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)
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Fal*_*lko 22

您使用的是哪个版本的solc?

Solc前几天发布了一个破解版本,这个错误与此有关.

npm uninstall solc
npm install solc@0.4.25
Run Code Online (Sandbox Code Playgroud)


kus*_*ikh 8

如果您使用的是最新版本,即。0.5.9编译代码的方式有所变化。

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))));
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案,简单地降级版本不是解决方案 (2认同)

小智 5

这是因为在安装 solc 包时安装的 Solidity 编译器版本与solidity 文件中提到的编译器版本不匹配。要解决此问题,请尝试

安装:

npm install solc@0.4.25

在solidity文件中使用:

pragma solidity^0.4.25;