17.0.1 • Published 4 days ago

web3-provider-engine v17.0.1

Weekly downloads
66,923
License
MIT
Repository
github
Last release
4 days ago

Web3 ProviderEngine

Web3 ProviderEngine is a tool for composing your own web3 providers.

!CAUTION This package has been deprecated.

This package was originally created for MetaMask, but has been replaced by @metamask/json-rpc-engine, @metamask/eth-json-rpc-middleware, @metamask/eth-json-rpc-provider, and various other packages.

Here is an example of how to create a provider using those packages:

import { providerFromMiddleware } from '@metamask/eth-json-rpc-provider';
import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';
import { valueToBytes, bytesToBase64 } from '@metamask/utils';
import fetch from 'cross-fetch';

const rpcUrl = '[insert RPC URL here]';

const fetchMiddleware = createFetchMiddleware({
  btoa: (stringToEncode) => bytesToBase64(valueToBytes(stringToEncode)),
  fetch,
  rpcUrl,
});
const provider = providerFromMiddleware(fetchMiddleware);

provider.sendAsync(
  { id: 1, jsonrpc: '2.0', method: 'eth_chainId' },
  (error, response) => {
    if (error) {
      console.error(error);
    } else {
      console.log(response.result);
    }
  }
);

This example was written with v12.1.0 of @metamask/eth-json-rpc-middleware, v3.0.1 of @metamask/eth-json-rpc-provider, and v8.4.0 of @metamask/utils.

Composable

Built to be modular - works via a stack of 'sub-providers' which are like normal web3 providers but only handle a subset of rpc methods.

The subproviders can emit new rpc requests in order to handle their own; e.g. eth_call may trigger eth_getAccountBalance, eth_getCode, and others. The provider engine also handles caching of rpc request results.

const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js')
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js')
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js')
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js')
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js')
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker.js')
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')

var engine = new ProviderEngine()
var web3 = new Web3(engine)

// static results
engine.addProvider(new FixtureSubprovider({
  web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
  net_listening: true,
  eth_hashrate: '0x00',
  eth_mining: false,
  eth_syncing: true,
}))

// cache layer
engine.addProvider(new CacheSubprovider())

// filters
engine.addProvider(new FilterSubprovider())

// pending nonce
engine.addProvider(new NonceSubprovider())

// vm
engine.addProvider(new VmSubprovider())

// id mgmt
engine.addProvider(new HookedWalletSubprovider({
  getAccounts: function(cb){ ... },
  approveTransaction: function(cb){ ... },
  signTransaction: function(cb){ ... },
}))

// data source
engine.addProvider(new RpcSubprovider({
  rpcUrl: 'https://testrpc.metamask.io/',
}))

// log new blocks
engine.on('block', function(block){
  console.log('================================')
  console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
  console.log('================================')
})

// network connectivity error
engine.on('error', function(err){
  // report connectivity errors
  console.error(err.stack)
})

// start polling for blocks
engine.start()

When importing in webpack:

import * as Web3ProviderEngine  from 'web3-provider-engine';
import * as RpcSource  from 'web3-provider-engine/subproviders/rpc';
import * as HookedWalletSubprovider from 'web3-provider-engine/subproviders/hooked-wallet';

Built For Zero-Clients

The Ethereum JSON RPC was not designed to have one node service many clients. However a smaller, lighter subset of the JSON RPC can be used to provide the blockchain data that an Ethereum 'zero-client' node would need to function. We handle as many types of requests locally as possible, and just let data lookups fallback to some data source ( hosted rpc, blockchain api, etc ). Categorically, we don’t want / can’t have the following types of RPC calls go to the network:

  • id mgmt + tx signing (requires private data)
  • filters (requires a stateful data api)
  • vm (expensive, hard to scale)

Running tests

yarn test
@muhammadwajidshahid/java-tron-providermultichain-walletkit-sdk@aquiladev/truffle-wallet-connect-providerarisenidjs-trial-version2-web3@amra-finance/ding@amra-finance/ding-plugin-ledger-web@amra-finance/ding-plugin-trezor-webweb3-yeetdrawer-farmer-wealth-ideabnc-onboard-metamaskargent-connect-coreopensea-wallet-providerarthcoin.js@nervehammer/springwalletstorage.core.minespider.com@infinitebrahmanuniverse/nolb-web3@everything-registry/sub-chunk-3124ng-ethoccaecatinihiloccaecatieaqueofficiisipsumomnislaboriosamomniscommodipanthalassanonfugamoacjs-walletmetamask-zeroclientmintswap-rfqminusinciduntminusomnismew-coremew-maker-pluginngx-request-networkpariatursaepeopen-registry-sdkpoolz-backpoolz-benefitpoolz-envelop-tokenpoolz-helperpoolz-helper-v2poolz-hodlers-whitelistpoolz-multi-withdrawpoolz-whitelistpraesentiumblanditiismolestiasullamnatusquinatusesse@cardstack/cardpay-sdkatodit@catalyst-net-js/truffle-provider@celo/0x-subproviders@digix/etc-redemption@digix/react-trezor-container@digix/sigmate@digix/truffle-lightwallet-provider@chakra-swap/core@dmihal/hdwallet-providerblacktea024bitski-truffle-provider@doneup/web3-provider@circlecurve/eth-signer-kms@chronobank/loginbnc-onboardbnc-onboard-custombnc-onboard-silent@cardstack/wc-provider@cardstack/web-client@coolwallets/web3-subproviderburner-provider@dragonereum/contracts@compound-finance/sol-coveragebrowser-testrpc@colligence/aws-kms-provider@crowdform/flex-connect-sdk@davidqhr/ganache-core@dcentralab/wc-hw-wallet-utils@dcentralab/wc-ledger@dcentralab/wc-trezor@dcentralab/web3-wallet-connector@daonomic/trezor-wallet-providercc-zoscapsule-core-js@fuseio/walletconnect-web3-provider@fuseio/bnc-onboardchirpjs@fwen/eth-signer-kms@francismurillodigix/react-trezor-containerweb3-ebakusweb3-hdwallet-provider@xswap/v2-core@xea12com/test-provider@ujoteam/card@two-point-five/web3-sdkweb3-zeus-providerweb3-vanillaweb3-walletweb3readyweb3-nodejs-providerweb3-ledger-webusb-connectorweb3-polyfill
17.0.1

4 days ago

16.0.8

4 days ago

17.0.0

8 days ago

16.0.7

6 months ago

16.0.6

7 months ago

16.0.5

1 year ago

16.0.4

2 years ago

16.0.3

3 years ago

16.0.2

3 years ago

16.0.1

4 years ago

16.0.0

4 years ago

15.0.12

4 years ago

15.0.11

4 years ago

15.0.10

4 years ago

15.0.8

4 years ago

15.0.9

4 years ago

15.0.7

4 years ago

15.0.6

4 years ago

15.0.5

4 years ago

15.0.4

4 years ago

15.0.3

5 years ago

15.0.2

5 years ago

14.2.1

5 years ago

15.0.1

5 years ago

15.0.0

5 years ago

14.2.0

5 years ago

14.1.0

6 years ago

14.0.6

6 years ago

14.0.5

6 years ago

14.0.4

6 years ago

14.0.3

6 years ago

14.0.2

6 years ago

14.0.1

6 years ago

14.0.0

6 years ago

13.8.0

6 years ago

13.7.1

6 years ago

13.7.0

6 years ago

13.6.6

6 years ago

13.6.5

6 years ago

13.6.4

6 years ago

13.6.3

6 years ago

13.6.2

6 years ago

13.6.1

6 years ago

13.6.0

6 years ago

13.5.6

6 years ago

13.5.5

6 years ago

13.5.4

6 years ago

13.5.3

6 years ago

13.5.2

6 years ago

13.5.1

6 years ago

13.5.0

6 years ago

13.4.0

6 years ago

13.3.4

6 years ago

13.3.3

7 years ago

13.3.2

7 years ago

13.3.1

7 years ago

13.3.0

7 years ago

13.2.12

7 years ago

13.2.11

7 years ago

13.2.10

7 years ago

13.2.9

7 years ago

13.2.8

7 years ago

13.2.7

7 years ago

13.2.6

7 years ago

13.2.5

7 years ago

13.2.4

7 years ago

13.2.3

7 years ago

13.2.2

7 years ago

13.2.1

7 years ago

13.2.0

7 years ago

13.1.1

7 years ago

13.1.0

7 years ago

13.0.3

7 years ago

13.0.2

7 years ago

13.0.1

7 years ago

13.0.0

7 years ago

12.2.4

7 years ago

12.2.3

7 years ago

12.2.2

7 years ago

12.2.1

7 years ago

12.2.0

7 years ago

12.1.0

7 years ago

12.0.6

7 years ago

12.0.5

7 years ago

12.0.4

7 years ago

12.0.3

7 years ago

12.0.2

7 years ago

12.0.1

7 years ago

12.0.0

7 years ago

11.0.2

7 years ago

11.0.1

7 years ago

11.0.0

7 years ago

10.0.1

7 years ago

10.0.0

7 years ago

9.2.1

7 years ago

9.1.0

7 years ago

9.0.0

7 years ago

8.6.1

7 years ago

8.6.0

7 years ago

8.5.0

7 years ago

8.4.0

7 years ago

8.3.0

7 years ago

8.2.0

7 years ago

8.1.19

7 years ago

8.1.18

7 years ago

8.1.17

7 years ago

8.1.16

7 years ago

8.1.15

7 years ago

8.1.14

7 years ago

8.1.13

7 years ago

8.1.12

7 years ago

8.1.11

7 years ago

8.1.10

7 years ago

8.1.9

7 years ago

8.1.8

7 years ago

8.1.7

7 years ago

8.1.6

8 years ago

8.1.5

8 years ago

8.1.4

8 years ago

8.1.3

8 years ago

8.1.1

8 years ago

8.1.0

8 years ago

8.0.8

8 years ago

8.0.7

8 years ago

8.0.6

8 years ago

8.0.5

8 years ago

8.0.4

8 years ago

8.0.3

8 years ago

8.0.2

8 years ago

8.0.1

8 years ago

8.0.0

8 years ago

7.8.8

8 years ago

7.8.7

8 years ago

7.8.6

8 years ago

7.8.5

8 years ago

7.8.4

8 years ago

7.8.3

8 years ago

7.8.2

8 years ago

7.8.1

8 years ago

7.8.0

8 years ago

7.7.3

8 years ago

7.7.2

8 years ago

7.7.1

8 years ago

7.7.0

8 years ago

7.6.7

8 years ago

7.6.6

8 years ago

7.6.5

8 years ago

7.6.4

8 years ago

7.6.3

8 years ago

7.6.2

8 years ago

7.6.1

8 years ago

7.6.0

8 years ago

7.5.0

8 years ago

7.4.0

8 years ago

7.3.0

8 years ago

7.2.1

8 years ago

7.2.2

8 years ago

7.1.1

8 years ago

7.1.0

8 years ago

7.0.1

8 years ago

7.0.0

8 years ago

6.1.0

8 years ago

6.0.4

8 years ago

6.0.3

8 years ago

6.0.2

8 years ago

6.0.1

8 years ago

6.0.0

8 years ago

5.1.1

8 years ago

5.1.0

8 years ago

5.0.2

8 years ago

5.0.1

8 years ago

5.0.0

8 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.4.1

8 years ago

3.4.0

8 years ago

3.3.1

8 years ago

3.3.0

8 years ago

3.2.3

8 years ago

3.2.2

8 years ago

3.2.1

8 years ago

3.2.0

8 years ago

3.1.2

8 years ago

3.1.1

8 years ago

3.1.0

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.0

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago