How to compile a Solidity Smart Contract with solc-js

You can easily install solc-js from npm official package repository:

# npm install -g solc

It will install solc-js under /usr/local/bin/solcjs -> ../lib/node_modules/solc/solc.js

Basic syntax to compile a smart contract written in Solidity:

$ solcjs --bin contract.sol

If you use external contracts, imports or OpenZeppelin:

$ solcjs --bin --include-path node_modules/ --base-path . MainContract.sol

If you want to have a look to other parameters, here they are:

$ solcjs --help 
Usage: solcjs [options]

  -V, --version                        output the version number
  --version                            Show version and exit.
  --optimize                           Enable bytecode optimizer. (default: false)
  --optimize-runs <optimize-runs>      The number of runs specifies roughly how often each opcode of the deployed code will be executed across the lifetime of the contract. Lower values
                                       will optimize more for initial deployment cost, higher values will optimize more for high-frequency usage.
  --bin                                Binary of the contracts in hex.
  --abi                                ABI of the contracts.
  --standard-json                      Turn on Standard JSON Input / Output mode.
  --base-path <path>                   Root of the project source tree. The import callback will attempt to interpret all import paths as relative to this directory.
  --include-path <path...>             Extra source directories available to the import callback. When using a package manager to install libraries, use this option to specify directories
                                       where packages are installed. Can be used multiple times to provide multiple locations.
  -o, --output-dir <output-directory>  Output directory for the contracts.
  -p, --pretty-json                    Pretty-print all JSON output. (default: false)
  -v, --verbose                        More detailed console output. (default: false)
  -h, --help                           display help for command

You might be also interested in:

Learn Pentesting like a Pro
🧮 How to install solc in Linux and compile smart contracts
To install the latest version, the best option is to compile directly from the official Github repository: There are two requirements prior to build solc, we need to install cmake and the development libraries of Boost…
Read more
Learn Pentesting like a Pro
🧮 How to install and use Paradigm Foundry to test and deploy smart contracts
As they say, Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. Apart of the speed another difference with Truffle or Hardhat is that the scripts and tests in Foundry are written in Solidity, not Javascript…
Read more






Leave a Reply

Your email address will not be published. Required fields are marked *