Subscription.make
Defines a kind of eth_subscribe from the names of its params, its name, the schemas of its
params and the schema of the items the node pushes.
import { Effect, Schema, Stream } from "effect";import { Socket } from "effect/socket";import { Hash, Quantity, Transport } from "@ethect/core";import { Subscription } from "@ethect/core/rpc";
/** Only the number and the hash of each new block. */const newHeadIds = Subscription.make()( "newHeads", [], Schema.Struct({ number: Quantity.schema, hash: Hash.schema }),);
const nextTwo = await newHeadIds().pipe( Stream.take(2), Stream.runCollect, Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")), Effect.provide(Socket.layerWebSocketConstructorGlobal), Effect.runPromise,);// [{ number: 1n, hash: "0x…" }, { number: 2n, hash: "0x…" }]The definition is a function that returns a Stream. A node pushes a full header for
newHeads: the schema keeps the two fields it names and drops the rest.
Subscription.make is called twice: first with the names of the params, as a type,
then with the definition. This kind has no params, so there is nothing to name.
A kind of another chain
Section titled “A kind of another chain”A node that serves a kind of its own is one definition away.
import { Schema } from "effect";import { Address, Hash } from "@ethect/core";import { Subscription } from "@ethect/core/rpc";
/** Each time a rollup's sequencer confirms a transaction, before it is in a block. */const confirmations = Subscription.make<[filter?: unknown]>()( "acme_confirmations", [Schema.optionalKey(Schema.Struct({ from: Address.wireSchema }))], Schema.Struct({ hash: Hash.schema, index: Schema.Number }),);
confirmations(); // every confirmationconfirmations({ from: Address.zero }); // those of one senderReturns
Section titled “Returns”Subscription.Subscription<Kind, Schema.Tuple<Params>, Item, Names>
A subscription: call it with the params of the kind to get a
Stream<Item, Method.Error, Subscriptions>.
Type parameters
Section titled “Type parameters”- Type: a tuple type with a label per param (optional)
The names of the params after the kind, in order: what an editor shows for the arguments of
the function. They follow the rules of Method.make: the types in
the tuple are not read, an optional param has an optional name, and names that are not those
of the params do not compile.
import { Schema } from "effect";import { Filter, Log, Subscription } from "@ethect/core/rpc";
const logs = Subscription.make<[filter?: unknown]>()( "logs", [Schema.optionalKey(Filter.schema)], Log.schema,);// logs(filter?: Filter)Parameters
Section titled “Parameters”- Type:
string
The name of the kind, sent as the first param of eth_subscribe.
import { Schema } from "effect";import { Hash } from "@ethect/core";import { Subscription } from "@ethect/core/rpc";
const pendingHashes = Subscription.make()( "newPendingTransactions", [], Hash.schema,);params
Section titled “params”- Type:
ReadonlyArray<Schema>
One schema per param after the kind, in order. Wrap a trailing one in Schema.optionalKey to
let the caller leave it out. A required param after an optional one does not compile, as in
Method.make.
import { Schema } from "effect";import { Filter, Log, Subscription } from "@ethect/core/rpc";
const logs = Subscription.make<[filter?: unknown]>()( "logs", [Schema.optionalKey(Filter.schema)], Log.schema,);- Type:
Schema
The schema of each item pushed. Its decoded type is the item type of the stream.
import { Schema } from "effect";import { Block, Subscription } from "@ethect/core/rpc";
const newHeads = Subscription.make()( "newHeads", [], Block.headerSchema,);
newHeads(); // Stream<Block.Header, Method.Error, Subscriptions>Behavior
Section titled “Behavior”- Always
eth_subscribe. The request iseth_subscribewith the kind and the encoded params, and items are the notifications that carry the id the node answered with. - Pure. Defining a subscription sends nothing.
- Same lifecycle as the built-in kinds: unsubscribed when the stream ends, issued again after a reconnection. See Opening a subscription.