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

isomorphic-rpc

v1.2.2

Published

A Javascript Object That Maps All Methods to RPC Calls as Promises (Isomorphic)

Downloads

479

Readme

Isomorphic-Rpc

A (promise based) Javascript wrapper for clean JSON-RPC method calls

What is JSON-RPC

A standard format for HTTP requests (much like REST), that is used by most cryptocurrency servers.

For Example

For instance a curl request like this:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}' -H "Content-type:application/json" https://www.ethercluster.com/etc

Can be executed in javascript like this:

rpc.eth_getBlockByNumber("latest", false).then(console.log) see initialization steps below

In general this should support any JSON-RPC application, but I made it to simplify my ethereum applications and I've only used/tested it on the same.

Use in place of Web3

  • because web3 is huge
  • web3 doesn't follow the spec (i.e. spec has eth_getBlockByHash, web3 has only getBlock)
  • web3 randomly prints some values as hex string ("0x1234") while others come back as number and for some odd reason block.difficulty is returned as a string of decimal digits (lol WAHT?)
  • New RPC methods like getProof are not supported (in web3 or other libraries) until each piece of software in the chain publishes a feature update to support it.

Solution

Using Ecmascript's new Proxy functionality you can create an object such that when you call a missing method on it, it instead calls a "handler" function that has access to the method name called.

The library uses this to automatically create an RPC request from any method name given. So any brand new or experimental RPC method your client software supports will be exposed.

Extremely Lightweight

The module is 37 lines of code. There is only 1 dependency (which branches into 3 small packages). It's only there to enables the library to be isomorphic.

Isomorphism

The use of this library should work identically in both Node and all desktop/mobile Browsers (except Internet Explorer because bill gates is busy curing malaria)

Use

npm install isomorphic-rpc
const Rpc = require('isomorphic-rpc')
let rpc = new Rpc('https://www.ethercluster.com/etc') // or default to "http://localhost:8545"
// or  "https://mainnet.infura.io" or even "https://web3.gastracker.io" etc...
rpc.web3_sha3("0x").then(console.log)

rpc.eth_getBlockByNumber("latest", false).then(console.log)

or within an async function:

let hash = await rpc.web3_sha3("0x")

Docs

The JSON-RPC Page is the most up to date list of supported Ethereum RPC calls.

Also the RPC 2.0 Specification could be useful in updating this package. If a PR will help it better supports the RPC spec, I'll merge it in.