Skip to main content

Provider API

Core Extension injects an EIP-1193 provider into every page. All interaction goes through a single method, request, plus a small set of events.

request

const accounts = await provider.request({
method: 'eth_requestAccounts',
});

request takes a method name and optional params, and returns a promise. The promise rejects with a provider error when the user declines, the dapp lacks permission, or the method fails.

Which object is provider? Any of the entry points Core exposes:

  • the provider announced through EIP-6963 (recommended — see Detect Core),
  • window.avalanche, Core's own conflict-free handle,
  • window.ethereum, shared with other installed wallets.

Events

The provider is an event emitter. Subscribe with on, unsubscribe with removeListener:

provider.on('accountsChanged', (accounts) => {
if (accounts.length === 0) {
// The user disconnected the dapp.
}
});

provider.on('chainChanged', (chainId) => {
// chainId is a hex string, e.g. '0xa86a' for Avalanche C-Chain.
window.location.reload();
});
EventPayloadFired when
accountsChangedstring[]The active account changes, or the user disconnects
chainChangedstring (hex chain id)The active network changes
connect{ chainId }The provider becomes able to serve requests
disconnectProviderRpcErrorThe provider can no longer serve requests
message{ type, data }Subscription updates, for example eth_subscribe

Method groups