A Universal Ethereum Provider
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ConnectionManager
asset
connections
parse
presets
provider
resolve
test
.gitignore
LICENSE
README.md
browser.js
node.js
package-lock.json
package.json

README.md


eth-provider


A Universal Ethereum Provider

Seamlessly connect to HTTP, WebSocket, IPC and Injected RPC transports in Node and the Browser!



Notice

  • Alpha software, eth-provider is a work in progress and not ready for production use
  • Evolving with EIP 1193
  • Testing and feedback is very much appreciated!

Goals

  • Support all transport types
  • Attempt connection to an array of RPC endpoints until success
  • Reconnect when connection is lost
  • Emit helpful status updates so apps can handle changes gracefully
  • Implement eth_pollSubscriptions method for HTTP subscriptions (supported by Frame)

Install

npm install eth-provider --save

Use

const provider = require('eth-provider')
const web3 = new Web3(provider())
  • By default, eth-provider will first try to discover providers injected by the environment, usually by a browser or extension
  • If eth-provider fails to find an injected provider it will attempt to connect to local providers running on the user's device like Frame, Geth or Parity
  • You can override these defaults by passing in your own RPC targets
const provider = require('eth-provider')
const web3 = new Web3(provider('wss://rinkeby.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b'))
  • When passing in multiple RPC targets order them by priority
  • When eth-provider fails to connect to a target it will automatically attempt to connect to the next priority target
  • For example ['injected', 'wss://rinkeby.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b'] will first try to discover injected providers and if unsuccessful connect to the Infura endpoint
const provider = require('eth-provider')
const web3 = new Web3(provider(['injected', 'wss://rinkeby.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b']))
  • In Node and Electron you'll have access to IPC endpoints created by Geth or Parity that cannot be accessed by the Browser. You can connect to these by using the 'direct' preset, or by passing custom IPC paths
const provider = require('eth-provider')
const web3 = new Web3(provider('direct'))

Presets

  • injected - Discover providers injected by environment, usually by the browser or a browser extension
    • Browser
      • ['injected']
  • frame - Connect to Frame running on the user's device
    • Browser/Node/Electron
      • ['ws://127.0.0.1:1248', 'http://127.0.0.1:1248']
  • direct - Connect to local Ethereum nodes running on the user's device
    • Browser
      • ['ws://127.0.0.1:8546', 'http://127.0.0.1:8545']
    • Node/Electron
      • [/* Default IPC paths for platform */, 'ws://127.0.0.1:8546', 'http://127.0.0.1:8545']
  • infura - Connect to Mainnet Infura
    • Browser/Node/Electron
      • ['wss://mainnet.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b', 'https://mainnet.infura.io/v3/786ade30f36244469480aa5c2bf0743b']
  • infuraRinkeby - Connect to Rinkeby Infura
    • Browser/Node/Electron
      • ['wss://rinkeby.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b', 'https://rinkeby.infura.io/v3/786ade30f36244469480aa5c2bf0743b']

If you do not pass any targets, eth-provider will use default targets ['injected', 'frame'] in the Browser and ['frame', 'direct'] in Node and Electron.