Skip to content

Provider.from

Instantiates an EIP-1193 Provider.Provider from an arbitrary EIP-1193 Provider interface.

Imports

Named
import { Provider } from 'ox'

Examples

Instantiating with RPC Transport

Ox's RpcTransport is 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 with External Providers

The example below demonstrates how we can instantiate a typed EIP-1193 Provider from an external EIP-1193 Provider like window.ethereum.

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

Instantiating a Custom Provider

The example below demonstrates how we can instantiate a typed EIP-1193 Provider from a HTTP fetch JSON-RPC request. You can use this pattern to integrate with any asynchronous JSON-RPC transport, including WebSockets and IPC.

import { Provider, RpcRequest, RpcResponse } from 'ox'
 
const store = RpcRequest.createStore()
 
const provider = Provider.from({
  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)
  },
})
 
const blockNumber = await provider.request({ method: 'eth_blockNumber' })

Type-safe Custom Schemas

It is possible to define your own type-safe schema by using the RpcSchema.from type.

import 'ox/window'
import { Provider, RpcSchema } from 'ox'
 
const schema = RpcSchema.from<
  | RpcSchema.Default
  | {
      Request: {
        method: 'abe_foo',
        params: [id: number],
      }
      ReturnType: string
    }
  | {
      Request: {
        method: 'abe_bar',
        params: [id: string],
      }
      ReturnType: string
    }
>()
 
const provider = Provider.from(window.ethereum, { schema })
 
const blockNumber = await provider.request({ method: '
abe_foo
abe_bar
eth_accounts
eth_blobBaseFee
eth_blockNumber
eth_call
eth_chainId
eth_coinbase
eth_estimateGas
eth_feeHistory
eth_gasPrice
eth_getBalance
eth_getBlockByHash
eth_getBlockByNumber
eth_getBlockTransactionCountByHash
eth_getBlockTransactionCountByNumber
eth_getCode
eth_getFilterChanges
eth_getFilterLogs
eth_getLogs
eth_getProof
eth_getStorageAt
eth_getTransactionByBlockHashAndIndex
eth_getTransactionByBlockNumberAndIndex
eth_getTransactionByHash
eth_getTransactionCount
eth_getTransactionReceipt
eth_getUncleCountByBlockHash
eth_getUncleCountByBlockNumber
eth_maxPriorityFeePerGas
eth_newBlockFilter
eth_newFilter
eth_newPendingTransactionFilter
eth_protocolVersion
eth_requestAccounts
eth_sendRawTransaction
eth_sendTransaction
eth_signTransaction
eth_signTypedData_v4
eth_uninstallFilter
personal_sign
wallet_addEthereumChain
wallet_getCallsStatus
wallet_getCapabilities
wallet_getPermissions
wallet_grantPermissions
wallet_requestPermissions
wallet_revokePermissions
wallet_sendCalls
wallet_showCallsStatus
wallet_switchEthereumChain
wallet_watchAsset
e' })

Instantiating a Provider with Events

The example below demonstrates how to instantiate a Provider with your own EIP-1193 flavored event emitter.

This example is 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...'])

Definition

function from<provider, options>(
  provider: provider | Provider<{
    schema: RpcSchema.Generic;
}>,
  options?: options | Options,
): Provider<options>

Source: src/core/Provider.ts

Parameters

provider

  • Type: provider | Provider<{ schema: RpcSchema.Generic; }>

The EIP-1193 provider to convert.

options

  • Type: options | Options
  • Optional

Return Type

An typed EIP-1193 Provider.

Provider<options>