# Wallet providers (Eip1193)

The EIP-1193 provider type, and the provider's events as streams: account changes, chain
changes, connect and disconnect. To send calls through a provider, use
[`Transport.eip1193`](https://ethect.dev/reference/transport/eip1193/).

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const watchChain = Eip1193.chainChanged(provider).pipe(
  Stream.runForEach((chainId) => Effect.log(`now on chain ${chainId}`)),
);
```

| Export | Returns | Description |
| --- | --- | --- |
| [`Provider`](#provider) | a type | An EIP-1193 provider. |
| [`accountsChanged(provider)`](#accountschanged) | `Stream<ReadonlyArray<Address>, Eip1193.Error>` | The accounts the wallet exposes, each time they change. |
| [`chainChanged(provider)`](#chainchanged) | `Stream<number, Eip1193.Error>` | The id of the chain the wallet is on, each time it changes. |
| [`connect(provider)`](#connect) | `Stream<number, Eip1193.Error>` | The chain id, each time the provider becomes able to reach a chain. |
| [`disconnect(provider)`](#disconnect) | `Stream<RpcError, Eip1193.Error>` | The reason, each time the provider loses every chain. |
| [`listen(provider, event)`](#listen) | `Stream<unknown, TransportError>` | The raw payloads of any event. |
| [`Error`](#error) | a type | What an event stream can fail with. |

The streams need no transport and no layer: they listen on the provider you pass.

## Provider

The object a wallet injects or a wallet SDK hands out.

```ts
import type { Eip1193 } from "@ethect/core";

const provider: Eip1193.Provider = {
  request: ({ method, params }) => Promise.resolve(null),
  on: (event, listener) => {},
  removeListener: (event, listener) => {},
};
```

| Member | Type |
| --- | --- |
| `request` | `(args: { method: string; params?: unknown }) => Promise<unknown>` |
| `on` | `(event: string, listener: (payload: any) => void) => unknown` |
| `removeListener` | `(event: string, listener: (payload: any) => void) => unknown` |

TypeScript's DOM types do not declare `window.ethereum`. The
[Wallets guide](https://ethect.dev/guides/wallets/#getting-the-provider) shows how to type it.

## accountsChanged

Emits the accounts the wallet exposes to the app, each time they change. The list is empty
when the wallet locks or the user disconnects the app.

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const watchAccount = Eip1193.accountsChanged(provider).pipe(
  Stream.runForEach(([account]) =>
    Effect.log(account === undefined ? "disconnected" : `account ${account}`),
  ),
);
```

- **Event:** `accountsChanged`
- **Parameters:** `provider` ([`Provider`](#provider)).
- **Emits:** `ReadonlyArray<Address>`. The addresses are validated and lowercase.

## chainChanged

Emits the id of the chain the wallet is on, each time it changes.

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const nextChain = Eip1193.chainChanged(provider).pipe(
  Stream.runHead, // the next change as an Option, then stop listening
);
```

- **Event:** `chainChanged`
- **Parameters:** `provider` ([`Provider`](#provider)).
- **Emits:** `number`. The provider sends a hex quantity (`"0xa"`) and the stream emits `10`.

## connect

Emits the chain id each time the provider becomes able to reach a chain.

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const online = Eip1193.connect(provider).pipe(
  Stream.runForEach((chainId) => Effect.log(`connected to chain ${chainId}`)),
);
```

- **Event:** `connect`
- **Parameters:** `provider` ([`Provider`](#provider)).
- **Emits:** `number`, the `chainId` of the event's payload.

## disconnect

Emits the reason each time the provider loses every chain.

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const offline = Eip1193.disconnect(provider).pipe(
  Stream.runForEach((error) => Effect.log(`disconnected: ${error.reason._tag}`)),
);
```

- **Event:** `disconnect`
- **Parameters:** `provider` ([`Provider`](#provider)).
- **Emits:** [`RpcError`](https://ethect.dev/reference/errors/rpc-error/). The payload is the provider's error
  object, classified like the error of a call: code `4900` is the reason `Disconnected`.

## listen

Emits the raw payloads of any event of the provider, undecoded. The four streams above are
built on it.

```ts
import { Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const messages = Eip1193.listen(provider, "message").pipe(Stream.take(1));
```

- **Parameters:** `provider` ([`Provider`](#provider)), then `event` (`string`).
- **Emits:** `unknown`. It does not decode, so it only fails when the provider cannot be
  listened to.

## Error

`Eip1193.Error` is what an event stream can fail with: `TransportError | CodecError`.

| Error | When |
| --- | --- |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/), reason `Unreachable` | the provider cannot be listened to: its `on` is missing or throws |
| [`CodecError`](https://ethect.dev/reference/errors/codec-error/) | the provider emitted a payload of another shape |

```ts
import { Effect, Stream } from "effect";
import { Eip1193 } from "@ethect/core";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

const chains = Eip1193.chainChanged(provider).pipe(
  Stream.catchTag("TransportError", () =>
    // a minimal provider without events: carry on with no changes to follow
    Stream.empty,
  ),
  Stream.runForEach((chainId) => Effect.log(`now on chain ${chainId}`)),
);
```

## Behavior

- **Listening starts when the stream runs** and stops when it ends: the listener is removed
  from the provider. Nothing is registered by building the stream.
- **Only changes.** A stream emits what happens after it started. Read the current state with
  [`Eth.accounts`](https://ethect.dev/rpc/eth/accounts/) and [`Eth.chainId`](https://ethect.dev/rpc/eth/chain-id/).
- **A payload of the wrong shape fails the stream** with a
  [`CodecError`](https://ethect.dev/reference/errors/codec-error/) whose `method` is the event name. `listen`
  does not decode, so it never fails that way.
- **A provider without events fails the stream** with a `TransportError`. `on` is the part of
  EIP-1193 a minimal provider leaves out.
- **Independent of the transport.** The streams work without `Transport.eip1193`, and the
  transport does not consume these events.
