gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:55:07 28 4
gpt4 key购买 nike

我尝试用 remix 编译以下合约(在 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 中的说明进行操作和 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)

关于javascript - 使用 solc-js 编译 solidity 但得到空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com