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

solc-browserify

v1.3.0

Published

Solidity compiler for the browser

Downloads

24

Readme

Solc Browserify

Solidity Compiler for the browser. Powered by WebWorker.

About

This package is heavily inspired by rexdavinci/browser-solidity-compiler.

Why another package?

this package uses a different method to initialize the compiler, it uses the bundled solc/wrapper module provided by solc.

How it works

this is accomplished by using a dedicated web worker and browserifying the solc/wrapper module, and then uploading the bundled module to npm. the worker then fetch the bundled wrapper module directly using importScripts from unpkg(open-source cdn for npm). check the bundled module here.

Why use the bundled wrapper ?

the rexdavinci/browser-solidity-compiler uses the built-in wasm cwrap function to initialize the compiler. although this works, the initialized compiler does not support import callbacks, which is important when you are building complex smart contracts.

by using the bundled native solc/wrapper, it enables custom import callbacks function. which then you can pass arbitrary but pure functions when initalizing the compiler.

but this came at a cost, because we use the bundled native solc/wrapper, we effectively can only wrap the same solc binary version as the bundled solc/wrapper. there is currently no effecient and easy way to bundle solc/wrapper on the fly. solc version will be displayed using a badge in the readme.

Usage

Installation

npm i solc-browserify

using yarn

yarn add solc-browserify

Import it in your app

import {
  Solc,
  ImportCallbackFn,
  ImportCallbackReturnType,
} from "solc-browserify";

Create a new Compiler Instance

const compiler = new Solc(callback? : (Solc: Solc) => any);

Note that when creating a new compiler instance, the newly created worker will fetch solc/wrapper bundle(~500 KB) and the solc binary(~8 MB).

Compile

const output = await compiler.compile(contract);

Support for Import Callback

this package have support for passing import callback function to the compiler. note that the import callback cannot be a closure, MUST be pure function, synchronous, and takes in exactly 1 parameter for the import path.

Basic example using import callback

import {
  Solc,
  ImportCallbackFn,
  ImportCallbackReturnType,
} from "solc-browserify";

const contract = `import "lib.sol";

contract C {

    function f() public {
         L.f();
    }
}`;

const callback: ImportCallbackFn = function (
  path: string
): ImportCallbackReturnType {
  const libSol = `library L { function f() internal returns (uint) { return 7; }`;

  let contract: ImportCallbackReturnType = null;

  if (path === "lib.sol") {
    contract = { contents: libSol };

    return contract;
  }

  contract = {
    error: `could not find source contract for ${path}`,
  };

  return contract;
};

async function main() {
  const solc = new Solc();
  const contract = await solc.compile(contract, callback);
}

main();

Example

Here