Search code examples
node.jscompilationsolidity

How to compile a solidity file using solc? (Version 0.8.15, node.js)


I have been trying for some time to compile the simple contract I created using solc but without success.

The contract.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.15;


contract basic {


modifier onlyOwner {
      require(msg.sender == owner);
      _;
}

address public owner;
address public peer;

address public lastSender;
int public lastAmaunt;

constructor(address p2){
    owner=msg.sender;
    peer=p2;
}

//see the balance of the contract
function getTotalBalance () public view returns (uint){
    return address(this).balance;
}

//only owner can sent money from contract 
function sentMoneyto (uint to,uint256 _value) public onlyOwner{
        if(to==1){
            (payable(peer)).transfer(_value);
        }else{
            (payable(owner)).transfer(_value);
        }
}
//can get money from evreyone 
function getMoneyFrom() public payable{
        lastSender = msg.sender;
        lastAmaunt =int(msg.value);
}

function cangePeer(address newPeer) public onlyOwner{
    peer=newPeer;
}

}

Not a complex contract, its purpose is to transfer money between two accounts as a middleman. compile file:

const path = require('path');
const fs = require('fs');
const solc = require('solc');
 
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
 
const input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol': {
            content: source,
        },
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*'],
            },
        },
    },
};
 
 
compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
].Inbox;

console.log(compilesFiles);

When I run compile.js I get the following output ...

my screen

My intention is to save the output in the file after compiling.

Thanks.


Solution

  • Mabe its because of solc version, but the problem was with the below line.

    before:

    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
    ].Inbox;
    

    After:

    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
    ].basic;
    

    Below is a working example!

    const path = require('path');
    const fs = require('fs');
    const solc = require('solc');
    const fsExtra = require('fs-extra')
     
    const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
    const source = fs.readFileSync(inboxPath, 'utf8');
     
    const input = {
        language: 'Solidity',
        sources: {
            'Inbox.sol': {
                content: source,
            },
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': ['*'],
                },
            },
        },
    };
    
    /**
     * Writes the contracts from the compiled sources into JSON files, which you will later be able to
     * use in combination with web3.
     * @param compiled - Object containing the compiled contracts.
     * @param buildPath - Path of the build folder.
     */
    function writeOutput(compiled, buildPath) {
        fsExtra.ensureDirSync(buildPath);
    
        for (let contractFileName in compiled.contracts) {
            const contractName = contractFileName.replace('.sol', '');
            console.log('Writing: ', contractName + '.json');
            fsExtra.outputJsonSync(
                path.resolve(buildPath, contractName + '.json'),
                compiled.contracts[contractFileName].basic
            );
        }
    }
     
     
    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input)));
    const buildPath = path.resolve(__dirname, 'build');
    writeOutput(compilesFiles, buildPath);
    

    Reference: best reference