使用 solc-js 编译 solidity 但得到空结果
Compile solidity with solc-js but get empty result
我尝试编译以下合约(在 remix) with 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 repo and the compiler input JSON spec 的指示。它没有任何错误,但结果非常空
{
"contracts": {
"example": {
"Calculator": {
"evm": {}
}
}
},
"sources": {
"example": {
"id": 0
}
}
}
它没有 output description 中描述的很多内容。我不确定我的代码出了什么问题。
您需要告诉 solc-js 您希望在输出响应中包含什么。默认情况下,它只编译并报告错误。 From the Input/Output JSON description(在 settings
部分):
// The following can be used to select desired outputs.
// If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors.
例如,要输出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)
我尝试编译以下合约(在 remix) with 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 repo and the compiler input JSON spec 的指示。它没有任何错误,但结果非常空
{
"contracts": {
"example": {
"Calculator": {
"evm": {}
}
}
},
"sources": {
"example": {
"id": 0
}
}
}
它没有 output description 中描述的很多内容。我不确定我的代码出了什么问题。
您需要告诉 solc-js 您希望在输出响应中包含什么。默认情况下,它只编译并报告错误。 From the Input/Output JSON description(在 settings
部分):
// The following can be used to select desired outputs.
// If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors.
例如,要输出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)