Solidity - Solidity code to Input JSON Description

谁都会走 提交于 2019-12-06 04:09:40

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;
    }
}

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

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