Solidity - Solidity code to Input JSON Description

我是研究僧i 提交于 2020-01-02 09:52:41

问题


I want to compile my ehtereum HelloWorld.sol smart contract. In all the tutorials is that you do it like this:

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

where HelloWorld.sol is:

pragma solidity ^0.5.1;

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

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

In other words I put my raw Solidity contract code into the solc.compile() method. But this process gives me this error in 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"}]}'

I was looking for a solution quite long, but only thing I found is that

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

(link). The standard input JSON looks like some combination of JSON and this solidity code. So my question is - How to transfer the solidity contract code into a compiler standard input JSON? Am I correct that this is the only way how to compile the contract? Thank you.


回答1:


This code works for me, 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;
    }
}



回答2:


Alternatively, you can run the solc (command line tool) with the below command and with input data

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

Where in the above command when --standard-json expects a input json file that you can give.

You can find an example of how an input file should be in the below link.

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



来源:https://stackoverflow.com/questions/53795971/solidity-solidity-code-to-input-json-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!