0

In pragma solidity ^0.6.4;

I wrote a contract and can get the byte code as follow:

const solc = require("solc");
const path = require("path");
const fs = require("fs");

const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol");
const source = fs.readFileSync(inboxPath, "utf8");

var input = {
  language: "Solidity",
  sources: { "test.sol": { content: source } },
  settings: {
    outputSelection: {
      "*": {
        "*": ["*"]
      }
    }
  }
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));

for (var contractName in output.contracts["test.sol"]) {
  console.log(
    contractName +
      ": " +
      output.contracts["test.sol"][contractName].evm.bytecode.object
  );
}

this code returns bytecode however I do not know how I can access to ABI code.

  • In solc 0.5 there was output.contracts["test.sol"][contractName].abi. – Ismael Mar 31 at 17:16
  • @Yilmaz Set the answer as the correct answer if you find it useful. – SAHIL SIKARWAR Jun 22 at 17:08
1

What are you doing here is fetching the 'bytecode' only from the JSON map.

output.contracts["test.sol"][contractName].evm.bytecode.object

The code snippet above is going in: contracts -> .sol file -> file_name object -> evm -> bytecode

if you look closely in this console.log:

console.log(output.contracts["test.sol"]["test"]);

you will notice that it contains 1)abi 2)devdoc 3)evm and inside evm there is assembly code and other stuff.

if you want to fetch abi, all you need to do is:

console.log(output.contracts["test.sol"]["test"].abi);

This will give you a result like:

[
  {
    inputs: [ [Object], [Object], [Object], [Object] ],
    name: 'addAdminData',
    outputs: [],
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    inputs: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
    name: 'addUserData',
    outputs: [],
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    inputs: [ [Object], [Object], [Object], [Object] ],
    name: 'addVendorData',
    outputs: [],
    stateMutability: 'nonpayable',
    type: 'function'
  }
]

and that's your ABI.

and if you want bytecode, you can use:

console.log(output.contracts["test.sol"]["test"]["evm"].bytecode);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.