用solc-js编译实体,但得到空结果

时间:2018-04-07 15:36:58

标签: javascript solidity

我尝试使用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 repocompiler input JSON spec的说明操作。它没有任何错误,但结果非常空

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

它没有output description所述的大量内容。我不确定我的代码出了什么问题。

1 个答案:

答案 0 :(得分:1)

您需要告诉solc-js您想要包含在输出响应中的内容。默认情况下,它仅编译和报告错误。 From the Input/Output JSON description(在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)