Transports (Transport)
A transport carries JSON-RPC calls to a node or a wallet. Each one is a Layer that provides
the Transport service, and the Subscriptions service when its connection can push.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const Http = Transport.http("http://127.0.0.1:8545").pipe( Layer.provide(FetchHttpClient.layer),);
const chainId = await Eth.chainId().pipe(Effect.provide(Http), Effect.runPromise);Transports
Section titled “Transports”| Constructor | Provides | Needs from the platform | Connects to |
|---|---|---|---|
Transport.http(url, options?) |
Transport |
an HttpClient |
a node over HTTP |
Transport.webSocket(url, options?) |
Transport, Subscriptions |
a WebSocketConstructor |
a node over WebSocket |
Transport.ipc(options?) |
Transport, Subscriptions |
a Socket |
a node on the same machine |
Transport.eip1193(provider) |
Transport, Subscriptions |
nothing | a wallet, through its EIP-1193 provider |
The Transports guide explains how to choose one and where the platform layers come from.
Transport
Section titled “Transport”- Type: a service with one member,
request(method, params)
Sends one call and returns its result as the node sent it, undecoded. Every
method is written against this service, which is why a definition works
over every transport.
import { Effect } from "effect";import { Transport } from "@ethect/core";
const version = Effect.gen(function* () { const transport = yield* Transport.Transport; return yield* transport.request("web3_clientVersion", []); // unknown: "anvil/v1.9.0" as the node sent it});| Member | Type |
|---|---|
request |
(method: string, params: Transport.Params) => Effect<unknown, Transport.Error> |
Call it directly only for a method you have no definition for and do not want to define. A definition is one line and types both directions.
Subscriptions
Section titled “Subscriptions”- Type: a service with two members:
subscribe(params)andopened(params)
Opens an eth_subscribe subscription and streams the items the node pushes, undecoded. HTTP
does not provide it, so a program that subscribes does not compile against HTTP alone.
import { Effect, Stream } from "effect";import { Transport } from "@ethect/core";
const firstHead = Effect.gen(function* () { const subscriptions = yield* Transport.Subscriptions; return yield* subscriptions.subscribe(["newHeads"]).pipe(Stream.runHead);});| Member | Type |
|---|---|
subscribe |
(params: ReadonlyArray<unknown>) => Stream<unknown, Transport.Error> |
opened |
(params: ReadonlyArray<unknown>) => Stream<Transport.Opened, Transport.Error> |
params are the params of eth_subscribe: the kind, then its own params. The subscription is
cancelled with eth_unsubscribe when the stream ends. The typed kinds are under
Eth.subscribe, and
Subscription.make defines another.
Transport.subscriptionsOver(connection) builds
this service for a transport of your own.
opened
Section titled “opened”The same subscription, said each time the node takes it: when it answers eth_subscribe on
the connection that is open, then on each connection that replaces it. Use it when you read
the node besides listening to it.
import { Effect, Stream } from "effect";import { Transport } from "@ethect/core";
/** What a subscription pushes, with `catchUp` read first each time the node takes it. */const withCatchUp = <A, E, R>(params: ReadonlyArray<unknown>, catchUp: Effect.Effect<A, E, R>) => Stream.unwrap( Effect.map(Transport.Subscriptions, ({ opened }) => opened(params).pipe( // the node has the subscription: nothing happens between this read and what it pushes Stream.switchMap(({ items }) => Stream.concat(Stream.fromEffect(catchUp), items)), ), ), );// Stream<unknown, E | Transport.Error, R | Subscriptions>A Transport.Opened is the answer of the node, and what follows it.
| Field | Type | Holds |
|---|---|---|
id |
string |
the id the node answered eth_subscribe with |
items |
Stream<unknown, Transport.Error> |
the items the node pushes under that id |
- The read comes after the answer. A node pushes from the moment it answers. What you read
when an
Openedcomes, with itsitems, misses nothing: neither what happened while there was no connection, nor what happens while the subscription is being made. A read made when the stream starts, or when a connection is established, can be followed by something that is neither read nor pushed. - No item is missed, however late you read them. The items are listened to from before the request is sent. Those pushed while your read is under way wait, and come after it.
- Some of them you have already read. What the node pushes between its answer and your read is in both: tell an item you have by its hash, or by its place in its block.
- The items of one end with it. Read them inside the stream, with
Stream.switchMap: when the nextOpenedcomes, or when the stream is no longer run, the subscription is cancelled. subscribeisopenedfor whoever does not read:opened(params).pipe(Stream.switchMap(({ items }) => items)).
| The transport | opened emits |
|---|---|
| WebSocket, IPC | each time the node answers eth_subscribe on a connection. It fails with the node’s error when the node refuses the subscription, and when the transport gives up reconnecting. |
| EIP-1193, or one of your own with a single connection | once. A wallet that relays to a node may answer before its node has the subscription: the answer is the wallet’s. |
Block.watch and Ev.subscription
are built on it.
Several chains
Section titled “Several chains”An app on one chain provides one transport. An app on several provides one per chain, and each part of the program says which chain it is on.
| Export | Kind | Description |
|---|---|---|
Transport.chains(transports) |
layer | Every chain of an app, each with its node, by chain id. Each transport is built the first time it is used, and kept. |
Transport.on(chainId) |
function | Says which chain an effect or a stream is on: the node of that chain is its Transport. |
Transport.chainId |
effect | The chain id of the transport in context. Under Transport.on it is the id given. For a transport alone, the node is asked once, with eth_chainId, and the answer is kept for as long as the transport lives. |
Transport.Chains |
service | What Transport.chains provides. It is not a Transport: against several chains, a program that does not say its chain does not compile. |
Transport.UnknownChain |
error | A chain the program has no node for, or a node that is not on the chain it was given for. It holds the chainId. |
| Type | Is |
|---|---|
Transport.Error |
what a transport can fail with: RpcError | TransportError. Also a value: the Schema of that type, to declare in the failure of a query or of a command |
Transport.Url |
where a node is: a string, or a Redacted<string> for a URL with a key in it, which is then never written |
Transport.On |
what Transport.on makes of an effect or a stream: it needs Transport.Chains in place of Transport, and can fail with Transport.UnknownChain |
Transport.Params |
the params of a request: an array, or an object for the few methods that take them by name |
Transport.HttpOptions |
the options of Transport.http |
Transport.SocketOptions |
the options of Transport.webSocket and Transport.ipc |
Transport.Opened |
a subscription as the node holds it on one connection: the id it answered eth_subscribe with, and the items it pushes under it. What opened emits. |
Transport.PushConnection |
what Transport.subscriptionsOver takes: request, items, scope, and connections and isLive for a transport that reconnects |
Transport.PushedItem |
an item a node pushed, under the id of its subscription: { subscription, result } |
Behavior
Section titled “Behavior”- Plain JSON-RPC 2.0. A request is
jsonrpc,id,methodandparams, and nothing else. - A layer is built where it is provided. Each
Effect.provide(transport)that reachesEffect.runPromisebuilds a transport of its own: a new HTTP batcher, or a new socket connection. Provide it once, around the whole program. See Transports. - A transport is scoped. A socket transport closes its connection when the scope that built its layer closes, which is when the program it was provided to ends.
- No timeout. No transport gives up on a slow call. Use
Effect.timeouton the call. - No retry. A failed call is not sent again. Use
Effect.retryon the calls that are safe to repeat.
Custom transports shows how to write a layer that provides these services.