Solidity - 输入的 Solidity 代码 JSON 说明

Solidity - Solidity code to Input JSON Description

我想编译我的以太坊 HelloWorld.sol 智能合约。在所有教程中,您都是这样做的:

var solc = require('solc');
var compiledContract = solc.compile(fs.readFileSync('HelloWorld.sol').toString();

其中 HelloWorld.sol 是:

pragma solidity ^0.5.1;

contract HelloWorld {
    bytes32 message;
    constructor(bytes32 myMessage) public {
        message = myMessage;
    }

    function getMessage() public view returns(bytes32){
        return message;
    }
}

换句话说,我将我的原始 Solidity 合约代码放入 solc.compile() 方法中。但是这个过程在 compiledContract:

中给了我这个错误
'{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\n  Syntax error: value, object or array expected.\n* Line 1, Column 2\n  Extra non-whitespace after JSON value.\n","message":"* Line 1, Column 1\n  Syntax error: value, object or array expected.\n* Line 1, Column 2\n  Extra non-whitespace after JSON value.\n","severity":"error","type":"JSONError"}]}'

我找了很长时间的解决方案,但我唯一找到的是

"The high-level API consists of a single method, compile, which expects the Compiler Standard Input and Output JSON."

(link)。标准输入 JSON 看起来像是 JSON 和这个 solidity 代码的某种组合。所以我的问题是 -
如何将 solidity 合约代码传输到编译器标准输入 JSON?
我是否正确认为这是编译合约的唯一方法?

这段代码对我有用,index.js

const solc = require('solc')
const fs = require('fs')

const CONTRACT_FILE = 'HelloWorld.sol'

const content = fs.readFileSync(CONTRACT_FILE).toString()

const input = {
  language: 'Solidity',
  sources: {
    [CONTRACT_FILE]: {
      content: content
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
}

const output = JSON.parse(solc.compile(JSON.stringify(input)))

for (const contractName in output.contracts[CONTRACT_FILE]) {
  console.log(output.contracts[CONTRACT_FILE][contractName].evm.bytecode.object)
}

HelloWorld.sol

contract HelloWorld {
    bytes32 message;
    constructor(bytes32 myMessage) public {
        message = myMessage;
    }

    function getMessage() public view returns(bytes32){
        return message;
    }
}

或者,您可以 运行 使用以下命令和输入数据的 solc(命令行工具)

solc --standard-json   -o outputDirectory --bin --ast --asm HelloWorld.sol

在上面的命令中,当 --standard-json 需要您可以提供的输入 json 文件时。

您可以在下面的 link 中找到输入文件的示例。

来源:https://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html

const solc = require("solc");

// file system - read and write files to your computer
const fs = require("fs");

// reading the file contents of the smart  contract
const fileContent = fs.readFileSync("HelloWorld.sol").toString();

// create an input structure for my solidity compiler
var input = {
  language: "Solidity",
  sources: {
    "HelloWorld.sol": {
      content: fileContent,
    },
  },

  settings: {
    outputSelection: {
      "*": {
        "*": ["*"],
      },
    },
  },
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
// console.log("Output: ", output);

const ABI = output.contracts["HelloWorld.sol"]["Demo"].abi;
const byteCode = output.contracts["HelloWorld.sol"]["Demo"].evm.bytecode.object;

// console.log("abi: ",ABI)
// console.log("byte code: ",byteCode)

npm run yorfilename.js