EIP-2786 · Withdrawn · Interface · 2020-07-15
EIP-2786: Ethereum Provider Connect/Disconnect Events
A withdrawn proposal for injected Ethereum providers to emit `connect` and `disconnect` events when they gain or lose the ability to serve RPC requests; the behavior now lives in EIP-1193.
What problem does it solve?
A dapp talking to an injected provider (a browser wallet, for example) needs to know when that provider can no longer reach any chain, so it can warn the user instead of failing silently, and when it comes back. Providers already emitted such events in practice, but the behavior was not written down consistently.
EIP-2786 was a mostly retrospective proposal that tried to codify those connect / disconnect events as a small standalone standard.
How it works
The proposal defines two states:
- Connected: the provider can service RPC requests to at least one chain.
- Disconnected: it cannot service RPC requests to any chain.
When the provider goes from disconnected to connected, it must emit connect to every listener registered through the EIP-2700 event emitter interface. Listeners receive { chainId }, where chainId is the chain's integer ID as a hex string and must match eth_chainId if the provider supports it. Listeners may be called in any order.
A matching disconnect event is emitted on the opposite transition. Applications decide for themselves whether and how to react.
Sample code
Listening for both events on an injected provider:
const provider = (window as any).ethereum;
provider.on("connect", ({ chainId }: { chainId: string }) => {
console.log("Provider connected to chain", parseInt(chainId, 16));
});
provider.on("disconnect", (error: { code: number; message: string }) => {
// In EIP-1193 the payload is a ProviderRpcError, typically code 4900
console.warn("Provider disconnected:", error.code, error.message);
});
Things to know
- Status is Withdrawn. Connect and disconnect events are specified in EIP-1193 (Final), which is what wallets and libraries implement. Use that spec, not this one.
- The withdrawn text is internally inconsistent: the abstract swaps the two transitions (it says
connectfires on going disconnected), and the specification section defines only theconnectevent. Thedisconnectpayload in the sample above follows EIP-1193. - It depends on the EIP-2700 event emitter interface; the request transport is in EIP-2696.
- The rationale notes that per-chain connect/disconnect events were left out to match existing behavior.