# Transports

A transport carries the calls of a program to a node or a wallet. There are four, each a
`Layer`, and a program does not change when you swap one for another. The example reads a
contract over HTTP.

```ts title="supply.ts"
import { Address, Erc20 } from "@ethect/core";

const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

/** A read of a contract. It names no node: it runs over any transport. */
export const supply = Erc20.at(USDC).totalSupply();
```
```ts title="main.ts"
import { Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { supply } from "./supply";

const Http = Transport.http("https://ethereum-rpc.publicnode.com").pipe(
  Layer.provide(FetchHttpClient.layer),
);

const total = await supply.pipe(Effect.provide(Http), Effect.runPromise);
```

## Choosing one

| You have | Use | Subscriptions |
| --- | --- | --- |
| an HTTP endpoint | [`Transport.http`](#http) | no |
| a WebSocket endpoint, and you subscribe or make many calls | [`Transport.webSocket`](#websocket) | yes |
| a node on the same machine | [`Transport.ipc`](#ipc) | yes |
| a wallet in the browser | [`Transport.eip1193`](#a-wallet) | yes, where the wallet serves them |

Start with HTTP. Move to a socket when you need events pushed, or a
[subscription](https://ethect.dev/rpc/guides/subscriptions/).

### HTTP

`main.ts` above. The transport POSTs each call with the `HttpClient` you give it, and
`FetchHttpClient.layer` uses the global `fetch`. Headers, retries and
[batching](https://ethect.dev/rpc/guides/batching/) are on the [`Transport.http`](https://ethect.dev/reference/transport/http/)
page.

### WebSocket

```ts
import { Effect, Layer } from "effect";
import { Socket } from "effect/socket";
import { Transport } from "@ethect/core";
import { supply } from "./supply";

const WebSocket = Transport.webSocket("wss://ethereum-rpc.publicnode.com").pipe(
  Layer.provide(Socket.layerWebSocketConstructorGlobal),
);

const total = await supply.pipe(Effect.provide(WebSocket), Effect.runPromise);
```

One connection serves every call and every subscription. It reconnects when it drops: see
[Connection lifecycle](https://ethect.dev/concepts/connection-lifecycle/).

`Socket.layerWebSocketConstructorGlobal` uses the global `WebSocket` of browsers, Node 22 and
later, Bun and Deno. [`Transport.webSocket`](https://ethect.dev/reference/transport/web-socket/) lists the
options.

### IPC

```ts
import { NodeSocket } from "@effect/platform-node";
import { Effect, Layer } from "effect";
import { Transport } from "@ethect/core";
import { supply } from "./supply";

const Ipc = Transport.ipc().pipe(
  Layer.provide(NodeSocket.layerNet({ path: "/tmp/anvil.ipc" })),
);

const total = await supply.pipe(Effect.provide(Ipc), Effect.runPromise);
```

The socket comes from the platform package, here `@effect/platform-node`. See
[`Transport.ipc`](https://ethect.dev/reference/transport/ipc/).

### A wallet

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

// 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);

// The read goes through the wallet, to the node of the chain it is on.
const total = await supply.pipe(Effect.provide(Injected), Effect.runPromise);
```

The provider is the connection, so this transport needs no platform layer.
[Let the user pick a wallet](https://ethect.dev/guides/wallet-selection/) is how an app connects and signs.
[Talk to a wallet directly](https://ethect.dev/guides/wallets/) is the low-level way, over this transport.

## The program does not change

`supply` names a contract and a function, and no transport. It is written once, and where it
runs is decided at the edge: the four examples above run the same value.

The type of a program says what it needs. A read needs a `Transport`. A program that
subscribes needs `Subscriptions` as well, and providing HTTP alone to it is a compile error.

```ts
import { Effect, Layer, Stream } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const Http = Transport.http("https://ethereum-rpc.publicnode.com").pipe(
  Layer.provide(FetchHttpClient.layer),
);
const heads = Stream.runDrain(Eth.subscribe.newHeads());

// @ts-expect-error HTTP cannot push: the program still needs Subscriptions
Effect.runPromise(heads.pipe(Effect.provide(Http)));
```

## Provide it once

A layer is built where it is provided. Two runs that each provide the transport build two
transports: two WebSocket connections, or two HTTP batchers that cannot share a batch.

```ts
import { Layer, ManagedRuntime } from "effect";
import { Socket } from "effect/socket";
import { Call, Transport } from "@ethect/core";
import { supply } from "./supply";

const WebSocket = Transport.webSocket("wss://ethereum-rpc.publicnode.com").pipe(
  Layer.provide(Socket.layerWebSocketConstructorGlobal),
);

// One runtime, one connection, for as long as the app lives.
const runtime = ManagedRuntime.make(WebSocket);

const total = await runtime.runPromise(supply);
const block = await runtime.runPromise(Call.blockNumber);

// when the app shuts down: closes the connection
await runtime.dispose();
```

| Your code is | Provide the transport |
| --- | --- |
| one script | once, around the whole program, before `Effect.runPromise` |
| an app that runs effects from event handlers | in a `ManagedRuntime`, and run every effect through it |
| a test | in the test's layer: see [Testing](https://ethect.dev/guides/testing/) |

## Timeouts and retries

No transport gives up on a slow call or repeats a failed one. Both are one line on the call,
where you know whether repeating is safe.

```ts
import { Effect, Schedule } from "effect";
import { supply } from "./supply";

const patient = supply.pipe(
  // a read: safe to repeat
  Effect.retry({
    while: (error) => error._tag === "TransportError",
    schedule: Schedule.exponential("200 millis"),
    times: 3,
  }),
  Effect.timeoutOption("5 seconds"), // None when the node took too long
);
```

A timeout interrupts the call: over HTTP the request is aborted.

## Next steps

- [Read and write a contract](https://ethect.dev/guides/contracts/): what most programs run over a transport.
- [The JSON-RPC layer](https://ethect.dev/rpc/): one request at a time, starting with
  [Calling methods](https://ethect.dev/rpc/guides/calling-methods/).
- [Batching](https://ethect.dev/rpc/guides/batching/): several calls in one HTTP request.
- [Connection lifecycle](https://ethect.dev/concepts/connection-lifecycle/): what a socket transport does when
  the connection drops.
- [Custom transports](https://ethect.dev/guides/custom-transports/): a fallback, a logger, or a transport of
  your own.
