What is solc?
The solc npm package is a JavaScript binding for the Solidity compiler. It allows developers to compile Solidity code from within a JavaScript environment, making it easier to integrate smart contract compilation into web applications, development tools, and automated scripts.
What are solc's main functionalities?
Compile Solidity Source Code
This feature allows you to compile Solidity source code. The code sample demonstrates how to compile a simple Solidity contract using the solc package.
const solc = require('solc');
const source = 'contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }';
const input = {
language: 'Solidity',
sources: {
'SimpleStorage.sol': {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output);
Get Compiler Version
This feature allows you to retrieve the version of the Solidity compiler being used. The code sample demonstrates how to get the compiler version using the solc package.
const solc = require('solc');
console.log(solc.version());
Compile with Specific Compiler Version
This feature allows you to compile Solidity code using a specific version of the Solidity compiler. The code sample demonstrates how to compile a contract using a specific compiler version.
const solc = require('solc');
const solcVersion = 'v0.8.6+commit.11564f7e';
const solcjs = require(`solc/soljson-${solcVersion}.js`);
const input = {
language: 'Solidity',
sources: {
'SimpleStorage.sol': {
content: 'contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }'
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
};
const output = JSON.parse(solcjs.compile(JSON.stringify(input)));
console.log(output);
Other packages similar to solc
truffle
Truffle is a development environment, testing framework, and asset pipeline for Ethereum. It provides a suite of tools for developing smart contracts, including compilation, linking, deployment, and binary management. Unlike solc, which focuses solely on compilation, Truffle offers a more comprehensive suite of tools for the entire smart contract development lifecycle.
hardhat
Hardhat is a development environment for Ethereum that helps developers manage and automate the recurring tasks inherent to building smart contracts and DApps. It includes a built-in Solidity compiler, but also offers additional features like task runners, testing, and debugging. Hardhat provides a more integrated development experience compared to solc, which is focused on compilation.
embark
Embark is a framework for developing and deploying decentralized applications (DApps) that integrates with Ethereum blockchains. It includes a Solidity compiler, but also offers features for managing smart contract deployment, testing, and communication with decentralized storage and messaging systems. Embark provides a broader range of functionalities compared to solc, which is specialized in compilation.
solc-js
JavaScript bindings for the Solidity compiler.
Node.js Usage
To use the latest stable version of the Solidity compiler via Node.js you can install it via npm:
npm install solc
And then use it like so:
var solc = require('solc');
var input = 'contract x { function g() {} }';
var output = solc.compile(input, 1);
for (var contractName in output.contracts) {
console.log(contractName + ': ' + output.contracts[contractName].bytecode);
console.log(contractName + '; ' + JSON.parse(output.contracts[contractName].interface));
}
Starting from version 0.1.6, multiple files are supported with automatic import resolution by the compiler as follows:
var solc = require('solc');
var input = {
'lib.sol': 'library L { function f() returns (uint) { return 7; } }',
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
};
var output = solc.compile({sources: input}, 1);
for (var contractName in output.contracts)
console.log(contractName + ': ' + output.contracts[contractName].bytecode);
Note that all input files that are imported have to be supplied, the compiler will not load any additional files on its own.
Starting from version 0.2.1, a callback is supported to resolve missing imports as follows:
var solc = require('solc');
var input = {
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
};
function findImports(path) {
if (path === 'lib.sol')
return { contents: 'library L { function f() returns (uint) { return 7; } }' }
else
return { error: 'File not found' }
}
var output = solc.compile({sources: input}, 1, findImports);
for (var contractName in output.contracts)
console.log(contractName + ': ' + output.contracts[contractName].bytecode);
Note:
If you are using Electron, nodeIntegration
is on for BrowserWindow
by default. If it is on, Electron will provide a require
method which will not behave as expected and this may cause calls, such as require('solc')
, to fail.
To turn off nodeIntegration
, use the following:
new BrowserWindow({
webPreferences: {
nodeIntegration: false
}
});
Using a Legacy Version
In order to allow compiling contracts using a specific version of Solidity, the solc.useVersion
method is available. This returns a new solc object using the version provided. Note: version strings must match the version substring of the files availble in /bin/soljson-*.js
. See below for an example.
var solc = require('solc');
var solcV011 = solc.useVersion('v0.1.1-2015-08-04-6ff4cd6');
var output = solcV011.compile('contract t { function g() {} }', 1);
If the version is not available locally, you can use solc.loadRemoteVersion(version, callback)
to load it directly from GitHub.
You can also load the "binary" manually and use setupMethods
to create the familiar wrapper functions described above:
var solc = solc.setupMethods(require("/my/local/soljson.js"))
.
Using the Latest Development Snapshot
By default, the npm version is only created for releases. This prevents people from deploying contracts with non-release versions because they are less stable and harder to verify. If you would like to use the latest development snapshot (at your own risk!), you may use the following example code.
var solc = require('solc');
solc.loadRemoteVersion('latest', function(err, solcSnapshot) {
if (err) {
}
var output = solcSnapshot.compile("contract t { function g() {} }", 1);
});