EIPs Explained

EIP-2700 · Final · Interface · 2020-06-05

EIP-2700: JavaScript Provider Event Emitter

Defines the minimal on/removeListener event-emitter shape that injected JavaScript Ethereum providers expose, so dApps can subscribe to wallet and chain events the same way everywhere.

What problem does it solve?

Browser wallets, desktop apps and Node.js plugins inject an Ethereum provider object into the JavaScript runtime so apps can talk to the chain. Apps also need to be told when things change: the user switches account, the chain changes, a subscription delivers new data.

Before this EIP there was no agreed shape for how an app registers for those notifications. ERC-2700 codifies the existing practice: the provider is a small event emitter with on and removeListener. It is a retrospective standard, written to match what wallets already did.

How it works

The whole interface is two methods:

interface EthereumProvider {
  on(eventName: string, listener: (...params: unknown[]) => void): void;
  removeListener(eventName: string, listener: (...params: unknown[]) => void): void;
}

Rules:

  • If on is called with an event name the provider knows, it must call the listener whenever that event occurs.
  • Adding the same listener twice to the same event may result in one call or two calls per event; the provider chooses.
  • removeListener with a previously added listener must reduce by one the number of times that listener is called per event.

What the spec deliberately leaves out:

  • Which events exist and what their payloads look like. Those are defined elsewhere, most notably in EIP-1193 (connect, disconnect, chainChanged, accountsChanged, message).
  • How the provider is discovered (for example window.ethereum, or the multi-wallet announcement flow of EIP-6963).

Sample code

Subscribing and cleaning up in a browser dApp:

type Listener = (...params: unknown[]) => void;
interface EthereumProvider {
  on(event: string, listener: Listener): void;
  removeListener(event: string, listener: Listener): void;
}

const provider = (window as any).ethereum as EthereumProvider | undefined;
if (!provider) throw new Error("No injected provider");

const onAccounts: Listener = (accounts) => {
  console.log("accounts changed:", accounts);
};
const onChain: Listener = (chainId) => {
  console.log("chain changed:", chainId); // hex string per EIP-1193
};

provider.on("accountsChanged", onAccounts);
provider.on("chainChanged", onChain);

// Later, e.g. when a React component unmounts:
export function cleanup() {
  provider!.removeListener("accountsChanged", onAccounts);
  provider!.removeListener("chainChanged", onChain);
}

A minimal provider-side implementation on top of Node's EventEmitter already satisfies the interface:

import { EventEmitter } from "node:events";
class MyProvider extends EventEmitter {} // inherits on() and removeListener()

Things to know

  • Status is Final, but the standard is intentionally tiny. In practice you will read it together with EIP-1193, which defines the request method and the actual event names.
  • Keep a reference to the exact listener function you pass to on; removeListener needs the same function object, so inline arrow functions cannot be removed later.
  • Because duplicate registrations may or may not fire twice, avoid adding the same listener more than once.
  • The spec assumes a trusted relationship: the app trusts whatever provider was injected into its runtime. Apps that support several wallets should discover them explicitly rather than trusting a single global object.

#wallet #provider #javascript #events #interface

Details

Authors
Micah Zoltu (@MicahZoltu), Erik Marks (@rekmarks)
Status
Final
Type
Standards Track / Interface
Created
2020-06-05
Required by

Machine-readable: Markdown · JSON