Skip to content

EIP-1193 Providers

Overview

EIP-1193 is a standard that defines a JavaScript Ethereum Provider API for interacting from an arbitrary JavaScript environment (e.g. Web Application, Server, etc) to the Ethereum network.

You would typically use an EIP-1193 Provider when communicating to a Wallet. For example, most Browser Extension Wallets inject a window.ethereum object into the webpage that conforms to the EIP-1193 API as a means to interact with Ethereum.

Libraries

There are a number of libraries that distribute EIP-1193 Providers, such as:

LibraryDescription
mipdMulti-injected browser wallet provider discovery
@walletconnect/ethereum-providerConnect and interact with WalletConnect-enabled wallets
@metamask/sdkConnect and interact with MetaMask Wallet
@coinbase/wallet-sdkConnect and interact with Coinbase Wallet
@safe-global/safe-apps-providerConnect and interact with Safe Wallets

Examples

Instantiating with External Providers

External EIP-1193 Providers can be instantiated with Provider.from.

import 'ox/window'
import { Provider } from 'ox'
 
const provider = Provider.from(window.ethereum)
 
const blockNumber = await provider.request({ method: 'eth_blockNumber' })

Instantiating with an RPC Transport

Ox's RpcTransport is also EIP-1193 compliant, and can be used to instantiate an EIP-1193 Provider. This means you can use any HTTP RPC endpoint as an EIP-1193 Provider.

import { Provider, RpcTransport } from 'ox'
 
const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com')
const provider = Provider.from(transport)

Instantiating a Provider with Events

Event emitters for EIP-1193 Providers can be created using Provider.createEmitter.

Useful for Wallets that distribute an EIP-1193 Provider (e.g. webpage injection via window.ethereum).

import { Provider, RpcRequest, RpcResponse } from 'ox'
 
// 1. Instantiate a Provider Emitter.
const emitter = Provider.createEmitter()
 
const store = RpcRequest.createStore()
 
const provider = Provider.from({
  // 2. Pass the Emitter to the Provider.
  ...emitter, 
  async request(args) {
    return await fetch('https://1.rpc.thirdweb.com', {
      body: JSON.stringify(store.prepare(args)),
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
    })
      .then((res) => res.json())
      .then(RpcResponse.parse)
  },
})
 
// 3. Emit Provider Events.
emitter.emit('accountsChanged', ['0x...'])

Related Modules

ModuleDescription
ProviderUtilities & types for working with EIP-1193 Providers