Provider.from
Instantiates an EIP-1193 Provider.Provider
from an arbitrary EIP-1193 Provider interface.
Imports
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_fooabe_bareth_accountseth_blobBaseFeeeth_blockNumbereth_calleth_chainIdeth_coinbaseeth_estimateGaseth_feeHistoryeth_gasPriceeth_getBalanceeth_getBlockByHasheth_getBlockByNumbereth_getBlockTransactionCountByHasheth_getBlockTransactionCountByNumbereth_getCodeeth_getFilterChangeseth_getFilterLogseth_getLogseth_getProofeth_getStorageAteth_getTransactionByBlockHashAndIndexeth_getTransactionByBlockNumberAndIndexeth_getTransactionByHasheth_getTransactionCounteth_getTransactionReceipteth_getUncleCountByBlockHasheth_getUncleCountByBlockNumbereth_maxPriorityFeePerGaseth_newBlockFiltereth_newFiltereth_newPendingTransactionFiltereth_protocolVersioneth_requestAccountseth_sendRawTransactioneth_sendTransactioneth_signTransactioneth_signTypedData_v4eth_uninstallFilterpersonal_signwallet_addEthereumChainwallet_getCallsStatuswallet_getCapabilitieswallet_getPermissionswallet_grantPermissionswallet_requestPermissionswallet_revokePermissionswallet_sendCallswallet_showCallsStatuswallet_switchEthereumChainwallet_watchAssete' })
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/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>
Error Type
Provider.from.ErrorType