'Solidity - Solidity code to Input JSON Description

I want to compile my ethereum 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 for quite a long time, but the 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?



Solution 1:[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;
    }
}

Solution 2:[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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Khaja Mohammed
Solution 3 Spandan Joshi