Web3 provider that implements EIP-1193.

The provider automatically connects to Fordefi when a new instance is constructed, and emits a connect event once both chainId and address were verified: chain is supported and address is managed by the given API user.

  • To subscribe to events pass a callback to on('eventName', callbackFn).
  • To get instead a promise that resolves once the event is emitted use the promisified helper waitForEmittedEvent('eventName').

NOTICE: Make sure to subscribe to 'connect' immediately after creating a new instance, and before initiating any other async operations, to avoid a race condition where the event is emitted before the listener is attached.

For example:

const provider = new FordefiWeb3Provider(config)

const onConnect = ({ chainId }: ProviderConnectInfo) => {
console.log(`Connected to chain ${chainId}`)
}

// option 1: subscribe with a callback
provider.on('connect', onConnect)

// option 2: act once a promise resolves
const result = await provider.waitForEmittedEvent('connect')
onConnect(result)
// or
provider
.waitForEmittedEvent('connect')
.then(onConnect)

Emitted events:

  • connect - provider becomes connected.
  • disconnect - provider becomes disconnected.
  • chainChanged - emitted once during connection with the chainId you provided.
  • accountsChanged - emitted once during connection with the address you provided.

Interfaces of each event callback are described in EIP1193EventCallbackParams and EIP1193EventMap.

Implements

  • FordefiEIP1193Provider

Constructors

Properties

on: (<event>(event, listener) => void)

Type declaration

removeListener: (<event>(event, listener) => void)

Type declaration

waitForEmittedEvent: (<Event>(event, timeoutMs?) => Promise<Parameters<EIP1193EventMap[Event]>[number]>)

Type declaration

Methods

  • Handles a JSON-RPC request

    Type Parameters

    • S extends RpcSchema = FordefiRpcSchema | [{
          Method: "web3_clientVersion";
          Parameters: undefined;
          ReturnType: string;
      }, {
          Method: "web3_sha3";
          Parameters: [data: `0x${string}`];
          ReturnType: string;
      }, {
          Method: "net_listening";
          Parameters: undefined;
          ReturnType: boolean;
      }, {
          Method: "net_peerCount";
          Parameters: undefined;
          ReturnType: `0x${string}`;
      }, {
          Method: "net_version";
          Parameters: undefined;
          ReturnType: `0x${string}`;
      }]
    • M extends string = S[number]["Method"]

    Parameters

    Returns Promise<MethodReturnType<S, M>>

    A promise with a response

    Throws

    RpcError with error codes defined in RpcErrorCode and ProviderRpcErrorCode

  • Connects the provider to Fordefi and emits a 'connect' event.

    • If already connected, it does nothing.
    • If connecting, waits for the connection to be established.

    Returns Promise<void>

    A promise that resolves once connected.

  • Disconnects the provider and emits a 'disconnect' event.

    Returns void

  • Returns the current status of the provider.

    Returns ConnectivityStatus

    'connected' if the provider is connected, 'connecting' if trying to connect, 'disconnected' otherwise.