API Reference

React Hooks

useWeb3Injected

function useWeb3Injected(options?: Web3ContextOptions): Web3Context

Returns Web3Context built from an injected provider like MetaMask. Will re-render on accounts, network, and connection change.

useWeb3Network

function useWeb3Network(connection: string, options?: Web3ContextOptions)
  : Web3Context

Returns Web3Context built from a connection string like Infura or a private node. Will re-render on accounts, network, and connection change.

useWeb3

function useWeb3(fallbackConnection: string, options?: Web3ContextOptions)
  : Web3Context

Tries to retrive an web3 injected provider first if fails falls back on a network connection. Will re-render on accounts, network, and connection change.

useEphemeralKey

function useEphemeralKey(): KeyPair

Generates in memory private/public key pair.

Helpers

fromConnection

async function fromConnection(connection: string, options?: Web3ContextOptions)
  : Promise<Web3Context>

Creates fully initialized Web3Context from a connection string to a node or a service like Infura.

fromInjected

async function fromInjected(options?: Web3ContextOptions): Promise<Web3Context>

Creates fully initialized Web3Context from an injected web3 provider, like MetMask.

WebContextOptions

export interface Web3ContextOptions {
  timeout: number;
  pollInterval: number;
  gsn: boolean | object;
}

timeout specifies wait time for underlying web3 provider calls before considering them failed. pollInteral is how often data will be polled from an underlying web3 provider. For network providers you might want to set this parameter manually to control a network load. gsn if set a GSN provider will be use. Pass an object to set GSN provider options.

Web3Context

Web3Context class is reponsible for polling web3 providers, firing events, enabling GSN, and keeping data fresh. Typically you shouldn’t call any methods on Web3Context yourself. Use React Hooks or helper methods.

Properties

lib

  public readonly lib: Web3;

An initialized instance of web3.js.

connected

  public connected: boolean;

Indicated if underlying provider available or not.

accounts

  public accounts: string[] | null;

Provides an instant access to the accounts of a web3 provider. If an array is empty then accounts either locked or not avaiable on a web3 provider.

networkId

  public networkId: number | null;

Id of a current network.

networkName

  public networkName: string | null;

A current network human-readable name, for example: Main, Ropsten.

providerName

  public readonly providerName: string;

A current web3 provider human-readable name, for example: metamask, infura.

Methods

startPoll

public startPoll(): void

Starts polling data from web3 provider and firing events on change. Typically you shouldn’t call this method directly.

stopPoll

public stopPoll(): void

Stops polling data and firing events. Typically you shouldn’t call this method directly.

poll

public async poll(): Promise<void>

Updates data and fires events only once. Typically you shouldn’t call this method directly.

requestAuth

public async requestAuth(): Promise<string[]>

Request access to the accounts of an underlying web3 provider according to the [EIP-1102](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1102.md). Typically you shouldn’t call this method directly.