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.
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();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
Section titled “Choosing one”| You have | Use | Subscriptions |
|---|---|---|
| an HTTP endpoint | Transport.http |
no |
| a WebSocket endpoint, and you subscribe or make many calls | Transport.webSocket |
yes |
| a node on the same machine | Transport.ipc |
yes |
| a wallet in the browser | Transport.eip1193 |
yes, where the wallet serves them |
Start with HTTP. Move to a socket when you need events pushed, or a subscription.
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 are on the Transport.http
page.
WebSocket
Section titled “WebSocket”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.
Socket.layerWebSocketConstructorGlobal uses the global WebSocket of browsers, Node 22 and
later, Bun and Deno. Transport.webSocket lists the
options.
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.
A wallet
Section titled “A wallet”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 is how an app connects and signs. Talk to a wallet directly is the low-level way, over this transport.
The program does not change
Section titled “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.
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 SubscriptionsEffect.runPromise(heads.pipe(Effect.provide(Http)));Provide it once
Section titled “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.
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 connectionawait 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 |
Timeouts and retries
Section titled “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.
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
Section titled “Next steps”- Read and write a contract: what most programs run over a transport.
- The JSON-RPC layer: one request at a time, starting with Calling methods.
- Batching: several calls in one HTTP request.
- Connection lifecycle: what a socket transport does when the connection drops.
- Custom transports: a fallback, a logger, or a transport of your own.