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

ethereum-rpc

v0.1.1

Published

In order for a DApp to function properly, it needs to communicate with the Ethereum network. That is often done by initializing Web3 (from [AVSA's post](https://blog.ethereum.org/2016/07/12/build-server-less-applications-mist/)):

Downloads

2

Readme

Standardizing how DApps talk to Ethereum and avoiding the IE7 hell

In order for a DApp to function properly, it needs to communicate with the Ethereum network. That is often done by initializing Web3 (from AVSA's post):

// Checks Web3 support
if(typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {
    // If there's a web3 library loaded, then make your own web3
    web3 = new Web3(web3.currentProvider);
} else if (typeof Web3 !== 'undefined') {
    // If there isn't then set a provider
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
} else if(typeof web3 == 'undefined') {
    // Alert the user he is not in a web3 compatible browser
    return;  
} 

Problem is, this only works on browsers which support Web3, which isn't the only way to communicate with an Ethereum node. Parity, for example, does things very differently with its own library, parity.js. Even Web3 itself will be updated soon, rendering the old initialization obsolte. That creates a scenario where either a DApp is locked to a specific platform, or the code must have a very complex initialization logic. This reminds the old, dreaded times of writing vendor-specific code to handle, for example, IE7. Hopefully, we can avoid that nightmare.

Obviously, we can't expect programmers to agree on any particular high-level library. The only possible solution is, thus, a simple, low-level, unopinionated library that communicates with Ethereum and nothing else. That library must come in the form of a single, standard object available on all Ethereum browsers (such as Mist), in the same way that XMLHttpRequest is available on classic browsers, giving sites a way to perform HTTP requests. Developers are, then, free to build high-level libraries on top of this object.

Solution: a standard global function that just calls the JSON-RPC

My particular suggestion is, thus, the simplest possible: a standard eth function, which, when called with a method name and a list of parameters, performs a RPC call to the Ethereum network and returns the result in a callback following the Node.js convention. And that's it. This, for example, would be a "hello world" DApp using eth:

// Checks if this browser supports Ethereum
if (typeof eth !== "undefined") {

  // Display the last block number on the page
  eth("eth_blockNumber", [], (err, blockNum) => {
    main.innerHTML = "The last Ethereum block number is " + blockNum + ".";
  });

// If it doesn't, alert the user
} else {
  main.innerHTML = "Your browser doesn't support Ethereum!";
}

Some things to notice:

  1. The check for Ethereum support is as simple as typeof eth !== "undefined".

  2. Methods such as .onconnect, .onopen are absent, because the connection is expected to be handled by the browser - i.e., the user choses what network to connect, not the DApp.

  3. The eth object is just a direct pipe to the RPC as it is. It doesn't do any kind of high-level manipulation or conversions. Results, for example, are sent in hex.

  4. Higher-level libraries build themselves on top of eth.

    
    // Builds Web3 on top of the native `eth`
    const web3 = new Web3(eth);
    
    // Uses its high-level features such as Promises and unit conversors
    web3.eth
      .getBalance("0x7da82c7ab4771ff031b66538d2fb9b0b047f6cf9")
      .then(balance => console.log("Balance on Golem's multisig:", Web3.fromWei(balance, "ether")))
  5. DApp developers are able to use whatever high-level libraries they want.

  6. If a high-level library updates, it won't break your DApp, because the only browser dependency is eth, which won't change.

  7. Middleware interventions (such as Mist's pop-up asking the user permission to submit a transaction) can be done by simply decorating the eth function to be injected on the DApp's webview.

This repository

This repository contains a demo implementation of the proposal above. Check example.js to see how you'd use it on Node.js. This is just a sketch, there is no real work performed on this yet, but it shouldn't be complicate since the proposal is, by definition, as simple as it gets.