Solidity - Solidity code to Input JSON Description
Asked Answered
C

3

10

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?

Cantone answered 15/12, 2018 at 18:18 Comment(0)
E
12

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;
    }
}
Ebonee answered 15/12, 2018 at 21:31 Comment(8)
Thanks. This is basically what i wanted to do, but is there some way, how to create the content of the input automatically? Something like solc.CreateJSON(CONTRACT_FILE)?Cantone
hey maybe you can advise? const appPath = path.resolve(__dirname, 'contracts', 'inbox.sol'); const source = fs.readFileSync(appPath, 'utf8').toString(); getting the file in a very similar manner, but got Invalid input source specified errorNelsonnema
Do you modify your content file anyhow before passing it here?Nelsonnema
Try const appPath = path.resolve(__dirname, './contracts/inbox.sol'); and maybe remove ''utf8'' in fs.readFileSync(). I didn't modify contentEbonee
yeah i tried hardcoded paths and removing utf-8 - sameNelsonnema
Ok i got it, you forget to include json key content in your answer and i overlook it in doc. it must be: [CONTRACT_FILE]: { content: content }Nelsonnema
Sorry about that, if key and value are same, you can write just one word, you can know more here ariya.io/2013/02/…. Thanks for your feedback, will do my examples more readable next time =)Ebonee
I re-cheked, code above with single content works on Node.js v8.12.0Ebonee
C
4

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

Cantwell answered 16/12, 2018 at 1:40 Comment(0)
V
0
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
Variegation answered 23/4, 2022 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.