Eth.subscribe
The five kinds of eth_subscribe, each as a function that returns a Stream of the items the
node pushes.
import { Stream } from "effect";import { Eth } from "@ethect/core/rpc";
const nextThree = Eth.subscribe.newHeads().pipe( Stream.map((head) => head.number), Stream.take(3), Stream.runCollect,);nextThree is an Effect that subscribes, collects the numbers of the next three blocks, and
unsubscribes. The Subscriptions guide shows how to work with the
streams.
| Kind | eth_subscribe param |
Item | Description |
|---|---|---|---|
subscribe.newHeads |
newHeads |
Block.Header |
The header of each block added to the chain. |
subscribe.logs |
logs |
Log |
The logs matching a filter, as blocks are added. |
subscribe.newPendingTransactions |
newPendingTransactions |
Hash or Transaction |
The transactions entering the pending pool. |
subscribe.transactionReceipts |
transactionReceipts |
ReadonlyArray<Receipt> |
The receipts of each block added to the chain. |
subscribe.syncing |
syncing |
sync status | The sync status, each time it changes. |
What they share
Section titled “What they share”- A
Stream. Each kind returns aStream<Item, Method.Error, Subscriptions>. Nothing is sent until the stream runs. - A transport that can push. The stream needs the
Subscriptionsservice, which the WebSocket, IPC and EIP-1193 transports provide. With the HTTP transport the program does not compile. - Unsubscribes when the stream ends. When the stream ends or is interrupted, the transport
sends
eth_unsubscribe.Stream.take(3)ends it after three items. - Issued again after a reconnection. When the connection drops and comes back, the subscription is sent again and the stream carries on. What the node emitted in between is not replayed. See Connection lifecycle.
- New items only. A subscription pushes what happens after it is opened. For what is
already on chain, call a method:
Eth.getLogs,Eth.getBlockByNumber. - Apart from each other. Several subscriptions share one connection with the calls, and each stream gets its own items.
Providing a transport
Section titled “Providing a transport”A subscription runs like a method, with a transport that can push in place of HTTP.
import { Effect, Stream } from "effect";import { Socket } from "effect/socket";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const program = Eth.subscribe.newHeads().pipe( Stream.take(3), Stream.runForEach((head) => Effect.log(`block ${head.number}`)),);
await program.pipe( Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")), Effect.provide(Socket.layerWebSocketConstructorGlobal), Effect.runPromise,);Errors
Section titled “Errors”A subscription fails like a method does, and the failure ends the stream.
| Error | When |
|---|---|
RpcError |
the node refuses the subscription, with the node’s own code and reason |
CodecError |
the params cannot be encoded, or an item does not have the shape of its kind |
TransportError |
the connection is lost and the transport stops trying to reconnect |
JSON-RPC method
Section titled “JSON-RPC method”eth_subscribe to open a subscription and eth_unsubscribe to close it. Neither is a function
of its own: the stream sends both.