使用 solc-js 编译 solidity,但得到空结果



我尝试使用 solc-js 编译以下合约(在混音上测试)

pragma solidity ^0.4.21;
contract Calculator {
uint8 public result = 0;
function add(uint8 value) public {
result = result + value;
}
}

这是我正在使用的代码

const { readFileSync } = require('fs');
const solc = require('solc');
const Web3 = require('web3');
// connect to the local instance
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
const params = {
language: "Solidity",
sources: {
'example': {
content: readFileSync('./contracts/example.sol', 'utf-8')
}
}
};
const compiled = JSON.parse(solc.compileStandardWrapper(JSON.stringify(params)));
// check the result
console.log(compiled);

我遵循 solc 存储库和编译器输入 JSON 规范中的说明。它可以正常工作,但结果非常空

{
"contracts": {
"example": {
"Calculator": {
"evm": {}
}
}
},
"sources": {
"example": {
"id": 0
}
}
}

它没有输出说明中所述的大量内容。我不确定我的代码出了什么问题。

你需要告诉 solc-js 你想要在输出响应中包含什么。默认情况下,它仅编译和报告错误。从输入/输出 JSON 说明(在settings部分中):

以下内容可用于选择所需的输出。

如果省略此字段,则编译器加载并执行类型检查,但除了错误之外不会生成任何输出。

例如,要输出 abi 和字节码,您的输入应该是

const params = {
language: "Solidity",
sources: {
"example": {
content: readFileSync('./contracts/example.sol', 'utf-8')
}
},
settings: {
outputSelection: {
"*": {
"*": [ "abi", "evm.bytecode" ]
}
}
}
};

outputSelection的可用选项包括:

abi - ABI
ast - AST of all source files
legacyAST - legacy AST of all source files
devdoc - Developer documentation (natspec)
userdoc - User documentation (natspec)
metadata - Metadata
ir - New assembly format before desugaring
evm.assembly - New assembly format after desugaring
evm.legacyAssembly - Old-style assembly format in JSON
evm.bytecode.object - Bytecode object
evm.bytecode.opcodes - Opcodes list
evm.bytecode.sourceMap - Source mapping (useful for debugging)
evm.bytecode.linkReferences - Link references (if unlinked object)
evm.deployedBytecode* - Deployed bytecode (has the same options as evm.bytecode)
evm.methodIdentifiers - The list of function hashes
evm.gasEstimates - Function gas estimates
ewasm.wast - eWASM S-expressions format (not supported atm)
ewasm.wasm - eWASM binary format (not supported atm)

最新更新