EIP-2696 · Final · Interface · 2020-06-04
EIP-2696: JavaScript `request` method RPC transport
Defines the single `request({ method, params })` function that injected Ethereum providers expose to JavaScript apps, plus a standard error shape and codes such as 4001 for user rejection.
What problem does it solve?
Browser wallets, Electron apps and Node runtimes can inject an Ethereum provider object into the JavaScript environment. Early providers each had their own call styles (send, sendAsync, callbacks, different argument shapes), so a dapp had to special-case every wallet.
EIP-2696 pins down the transport: one async method with a fixed argument shape and a fixed error shape. It deliberately does not define which RPC methods exist or how the object is discovered; those are left to other standards.
How it works
The provider must expose request, called with one object:
interface RequestArguments {
readonly method: string;
readonly params?: readonly unknown[] | object;
}
interface EthereumProvider {
request(args: RequestArguments): Promise<unknown>;
}
- If the provider supports a JSON-RPC method,
requestmust accept it withmethodequal to the JSON-RPC method name andparamsequal to the JSON-RPC params, passed as a JavaScript value, not a JSON string. - The promise resolves to what would have been the JSON-RPC
result, again as a plain JavaScript value. There is nojsonrpc/idenvelope. - On failure the promise rejects, whenever possible with a
ProviderRpcError(message, numericcode, optionaldata).
Standard provider error codes:
| Code | Message | Meaning |
|---|---|---|
| 4001 | User Rejected Request | The user rejected the request |
| 4100 | Unauthorized | Method or account not authorized by the user |
| 4200 | Unsupported Method | Provider does not support the method |
| 4900 | Disconnected | Not connected to any chain |
| 4901 | Chain Disconnected | Not connected to the requested chain |
Codes from the JSON-RPC spec (such as -32602) keep their established meanings.
Sample code
const provider = (window as any).ethereum;
try {
const accounts = (await provider.request({ method: "eth_requestAccounts" })) as string[];
const balance = (await provider.request({
method: "eth_getBalance",
params: [accounts[0], "latest"],
})) as string;
console.log(accounts[0], BigInt(balance));
} catch (err: any) {
if (err.code === 4001) {
console.log("User rejected the request");
} else {
console.error(err.code, err.message, err.data);
}
}
With ethers v6 you rarely call request yourself; BrowserProvider wraps any object that implements it:
import { BrowserProvider } from "ethers";
const signer = await new BrowserProvider((window as any).ethereum).getSigner();
Things to know
- This EIP is the transport layer only. The full wallet-facing API, including events such as
connect,chainChangedandaccountsChanged, is specified in EIP-1193, which uses the samerequestmethod and error codes. - The event emitter interface is in EIP-2700.
- The spec assumes a trusted relationship: the app trusts the injected provider. It does not cover provider authenticity or discovery.
- The spec's own JSON-RPC example shows
method: 'method'where it means the JSON-RPC method name (do_work); treat that as a typo.