.json files for contracts are not created

by Zubayr   Last Updated July 31, 2020 22:28 PM - source

I am really new in ethereum and solidity and I am having hard time to figure out how to get the .json equivalent for contracts.

I have the Campaign.sol inside the contractsFolder. The Campaign.sol file has 2 contracts named CampaignMgr and CampaignFactory.

My package.json file looks like this:

{
  "name": "kickstarter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "zake",
  "license": "ISC",
  "dependencies": {
    "fs-extra": "^9.0.0",
    "ganache-cli": "^6.9.1",
    "mocha": "^7.0.1",
    "solc": "^0.4.17",
    "web3": "^1.0.0-beta.26"
  }
}

My compile.js code is:

const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');


const buildPath = path.resolve(__dirname, 'build');
fs.removeSync(buildPath); //removes folder and its contains

const campaignPath = path.resolve(__dirname, 'contractsFolder', 'Campaign.sol');

const source = fs.readFileSync(campaignPath, 'UTF-8');
const output = solc.compile(source, 1).contracts;


fs.ensureDirSync(buildPath); //checks if directory exists. if not creates for us


for(let ctrct in output)
{
  fs.outputJsonSync(
    path.resolve(buildPath, ctrct+ '.json' ),
    output[ctrct]
  );
}

It should output the 2 json files like :CampaignMgr.json and :CampaignFactory.json inside the build folder but I never get the json equivalent of the contract.

I hope someone will point out exactly what I am doing wrong. Please let me know if you need anything else in the code.



Answers 2


The Campaign.sol file has 2 contracts named CampaignMgr and CampaignFactory

The basic input unit for the Solidity compiler is a contract, not a file:

  • If you create one file for each contract, and you use the same name for the file and for the contract, then yes - you get one artifact per file, with the exact same name
  • If you create one file for each contract, but you use different names for the file and for the contract, then you get one artifact per file, but with the name of the contract
  • If you place several contracts in a single file (and obviously use a different name for each one of them), then you get one artifact per each contract, with the name of the contract

As you can see in all cases, the Solidity compiler generates one artifact per contract, with the name of that contract; the way you choose to split them among different files has no impact here.

goodvibration
goodvibration
April 07, 2020 13:08 PM

I myself go the answer (thank God, finally).

I am giving the details in case anyone in future get some helps from it.

The point is atom is not supporting any .json file starting with : .

Thus what we have to do is to replace it with empty string, i.e.

path.resolve(buildPath, contract.replace(':', '') + '.json' ),

Also make sure to use the .sol version same as what version you use in the solidity code (e.g. for me pragma solidity ^0.4.17; )

Zubayr
Zubayr
April 07, 2020 15:12 PM

Related Questions




How to compile solidity AST to binary code?

Updated August 01, 2017 14:28 PM

Truffle Bytecode Different from Etherscan Bytecode

Updated October 14, 2017 04:28 AM

Error while compiling: Stack too deep

Updated August 25, 2018 00:28 AM