# Provider.from

Instantiates an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) [`Provider.Provider`](/api/Provider/types#provider) from an arbitrary [EIP-1193 Provider](https://eips.ethereum.org/EIPS/eip-1193) interface.

## Imports

:::code-group
```ts [Named]
import { Provider } from 'ox'
```

```ts [Entrypoint]
import * as Provider from 'ox/Provider'
```
:::

## Examples

### Instantiating with RPC Transport

Ox's [`RpcTransport`](/api/) 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.

```ts twoslash
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`.

```ts twoslash
import 'ox/window'
import { Provider } from 'ox'

const provider = Provider.from(window.ethereum)

const blockNumber = await provider.request({
  method: 'eth_blockNumber'
})
```

:::tip
There are also libraries that distribute EIP-1193 Provider objects that you can use with `Provider.from`:

* [`@walletconnect/ethereum-provider`](https://www.npmjs.com/package/@walletconnect/ethereum-provider)

* [`@coinbase/wallet-sdk`](https://www.npmjs.com/package/@coinbase/wallet-sdk)

* [`@metamask/detect-provider`](https://www.npmjs.com/package/@metamask/detect-provider)

* [`@safe-global/safe-apps-provider`](https://github.com/safe-global/safe-apps-sdk/tree/main/packages/safe-apps-provider)

* [`mipd`](https://github.com/wevm/mipd): EIP-6963 Multi Injected Providers
:::

### 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.

```ts twoslash
// @noErrors
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`](/api/RpcSchema/from) type.

```ts twoslash
// @noErrors
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: '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`).

```ts twoslash
// @noErrors
import { Provider, RpcRequest, RpcResponse } from 'ox'

// 1. Instantiate a Provider Emitter.
const emitter = Provider.createEmitter() // [!code ++]

const store = RpcRequest.createStore()

const provider = Provider.from({
  // 2. Pass the Emitter to the Provider.
  ...emitter, // [!code ++]
  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...']) // [!code ++]
```

## Definition

```ts
function from<options, provider>(
  provider: provider | from.Value<options> | undefined,
  options?: options | Options,
): from.ReturnType<options, provider>
```

**Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L681)

## Parameters

### provider

* **Type:** `provider | from.Value<options> | undefined`

The EIP-1193 provider to convert.

### options

* **Type:** `options | Options`
* **Optional**

## Return Type

An typed EIP-1193 Provider.

`from.ReturnType<options, provider>`
