# Transport.eip1193

A transport over an EIP-1193 provider, such as the object a browser wallet injects. Calls go
to the provider's `request` function, and subscription items come from its `message` events.

## Usage

```ts
import { Effect } from "effect";
import { type Eip1193, Transport } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";

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

const Injected = Transport.eip1193(provider);

const accounts = await Wallet.requestAccounts().pipe(
  Effect.provide(Injected),
  Effect.runPromise,
);
```

The layer needs nothing from the platform: the provider is the connection.

### Every method, not only the wallet's

A wallet forwards the calls it does not answer itself to its node, so the
[`Eth`](https://ethect.dev/rpc/eth/) methods work over the same transport.

```ts
import { Effect } from "effect";
import { type Eip1193, Transport } from "@ethect/core";
import { Eth, Wallet } from "@ethect/core/rpc";

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

const program = Effect.gen(function* () {
  const [account] = yield* Wallet.requestAccounts();
  const chainId = yield* Eth.chainId();
  return { account, chainId };
}).pipe(Effect.provide(Transport.eip1193(provider)));
```

### In a test

[`MockProvider`](https://ethect.dev/reference/testing/mock-provider/) is an EIP-1193 provider that answers from
handlers, so the same transport runs a program without a wallet.

```ts
import { Effect } from "effect";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
import { MockProvider } from "@ethect/core/testing";

const provider = MockProvider.make([
  MockProvider.handler(Eth.chainId, () => Effect.succeed(1)),
]);

const chainId = Eth.chainId().pipe(Effect.provide(Transport.eip1193(provider)));
```

### Seeing what the wallet answers

A wallet's requests never show in the Network tab of a browser. Each one is logged at the
Debug level once it is answered: set the minimum level to `"Debug"` to see them.

```ts
import { Effect, References } from "effect";
import { type Eip1193, Transport } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";

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

const status = Wallet.getCallsStatus(id).pipe(
  Effect.provide(Transport.eip1193(provider)),
  // timestamp=… level=DEBUG fiber=#1 message=wallet_getCallsStatus ms=12 params=["0x…"] answer={…}
  Effect.provideService(References.MinimumLogLevel, "Debug"),
);
```

| Annotation | Holds |
| --- | --- |
| `ms` | how long the provider took to answer |
| `params` | the params, as they were sent |
| `answer` | the answer, as the provider gave it, before anything reads it |
| `error` | what the provider rejected with, instead of `answer` |
| `interrupted` | `true` when the request was given up before an answer, such as by a timeout |

The method is the message. Signers and connections built on a wallet log the same way.

## Returns

`Layer<Transport | Subscriptions>`

A layer that provides [`Transport`](https://ethect.dev/reference/transport/#transport) and
[`Subscriptions`](https://ethect.dev/reference/transport/#subscriptions), with no requirement.

## Parameters

### provider

- **Type:** [`Eip1193.Provider`](https://ethect.dev/reference/eip1193/#provider)

An object with `request`, `on` and `removeListener`.

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

// Any object of this shape is a provider.
const provider: Eip1193.Provider = {
  request: ({ method }) => Promise.resolve(method === "eth_chainId" ? "0x1" : null),
  on: () => {},
  removeListener: () => {},
};

const Injected = Transport.eip1193(provider);
```

## Behavior

- **The method and params are passed as they are on the wire**: `provider.request` receives
  `method` and the encoded `params`. The provider assigns ids and correlates answers itself.
- **A rejection with a JSON-RPC error object is an `RpcError`.** The object needs a numeric
  `code` and a string `message`. A user who refuses a prompt is the reason
  `UserRejectedRequest`.
- **Any other rejection is a `TransportError`** with the reason `MalformedResponse`: the
  provider did not answer in JSON-RPC terms.
- **Subscriptions are the `message` events** of type `eth_subscription`. The transport listens
  for as long as its layer lives. A provider that does not serve `eth_subscribe` fails the
  stream with its own error.
- **A provider without events still answers calls.** When `provider.on` is missing or throws,
  calls work and every subscription fails with a `TransportError`, reason `Unreachable`.
- **Each request is logged at the Debug level**, once answered or given up. At the default
  level nothing is logged. A provider has no URL and its requests carry no key, so the log
  holds nothing secret.
- **No reconnection.** The provider owns its connection. Watch
  [`Eip1193.disconnect`](https://ethect.dev/reference/eip1193/#disconnect) and
  [`Eip1193.connect`](https://ethect.dev/reference/eip1193/#connect) to know its state.
- **Events are separate.** Account and chain changes are not part of the transport: they are
  the streams of the [`Eip1193`](https://ethect.dev/reference/eip1193/) module.

See [Wallets](https://ethect.dev/guides/wallets/) for a connect flow built on this transport.

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | the reason of the code | the provider rejected with a JSON-RPC error object |
| `TransportError` | `MalformedResponse` | the provider rejected with anything else |
| `TransportError` | `Unreachable` | a subscription, on a provider whose events cannot be listened to |
