npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

npm1-solc

v0.4.18

Published

Solidity compiler

Downloads

3

Readme

Build Status

solc-js

JavaScript bindings for the Solidity compiler.

Uses the Emscripten compiled Solidity found in the solc-bin repository.

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

Usage on the Command-Line

If this package is installed globally (npm install -g solc), a command-line tool called solcjs will be available.

To see all the supported features, execute:

solcjs --help

Note: this commandline interface is not compatible with solc provided by the Solidity compiler package and thus cannot be used in combination with an Wiseplat client via the wsh.compile.solidity() RPC method. Please refer to the Solidity compiler documentation for instructions to install solc.

Usage in Projects

From early versions

It can also be included and used in other projects:

var solc = require('npm1-solc')
var input = 'contract x { function g() {} }'
// Setting 1 as second paramateractivates the optimiser
var output = solc.compile(input, 1)
for (var contractName in output.contracts) {
	// code and ABI that are needed by web3
	console.log(contractName + ': ' + output.contracts[contractName].bytecode)
	console.log(contractName + '; ' + JSON.parse(output.contracts[contractName].interface))
}

From version 0.1.6

Starting from version 0.1.6, multiple files are supported with automatic import resolution by the compiler as follows:

var solc = require('npm1-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.

From version 0.2.1

Starting from version 0.2.1, a callback is supported to resolve missing imports as follows:

var solc = require('npm1-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)

The compile() method always returns an object, which can contain errors, sources and contracts fields. errors is a list of error mesages.

From version 0.4.11

Starting from version 0.4.11 there is a new entry point named compileStandardWrapper() which supports Solidity's standard JSON input and output. It also maps old compiler output to it.

Using with Electron

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('npm1-solc'), to fail.

To turn off nodeIntegration, use the following:

new BrowserWindow({
	webPreferences: {
		nodeIntegration: false
	}
})

Using a Legacy Version

In order to compile contracts using a specific version of Solidity, the solc.loadRemoteVersion(version, callback) method is available. This returns a new solc object that uses a version of the compiler specified.

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('npm1-solc')

// getting the development snapshot
solc.loadRemoteVersion('latest', function (err, solcSnapshot) {
	if (err) {
		// An error was encountered, display and quit
	}
	var output = solcSnapshot.compile("contract t { function g() {} }", 1)
})

Linking Bytecode

When using libraries, the resulting bytecode will contain placeholders for the real addresses of the referenced libraries. These have to be updated, via a process called linking, before deploying the contract.

The linkBytecode method provides a simple helper for linking:

bytecode = solc.linkBytecode(bytecode, { 'MyLibrary': '0x123456...' })

Note: in future versions of Solidity a more sophisticated linker architecture will be introduced. Once that changes, this method will still be usable for output created by old versions of Solidity.

Updating the ABI

The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.

It can be used as:

var abi = require('solc/abi')

var inputABI = [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}]
var outputABI = abi.update('0.3.6', inputABI)
// Output contains: [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":true,"type":"function"},{"type":"fallback","payable":true}]