0

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.

2 Answers 2

0

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.

4
  • Then please tell me how to proceed in this case
    – Zubayr
    Commented Apr 7, 2020 at 13:17
  • @Zubayr: What is your printout of running this script? Commented Apr 7, 2020 at 13:21
  • The build folder is created but it is empty (though it should have :CampaignMgr.json and :CampaignFactory.json) inside it
    – Zubayr
    Commented Apr 7, 2020 at 13:40
  • if I use a console log with output inside it I get Error: EPERM: operation not permitted, lstat error
    – Zubayr
    Commented Apr 7, 2020 at 13:47
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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