Eth.subscribe.newPendingTransactions
A stream of the transactions entering the node’s pending pool: their hashes, or the transactions in full when you ask for them.
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.newPendingTransactions().pipe( Stream.take(2), Stream.runCollect,);
const hashes = await program.pipe( Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")), Effect.provide(Socket.layerWebSocketConstructorGlobal), Effect.runPromise,);// [// "0xb148b2a7bb51041c8f1e44dc35d6efd03395ee07a3515377cf3c281f1ab4ee9c",// "0x5ffa7f098f7f6ef173ac11f15b34230f4d5027b25d5f16bcf90d0ecbc1f0dcf0",// ]The program subscribes, collects the hashes of the next two transactions the node receives,
and unsubscribes when Stream.take(2) ends the stream.
A subscription needs a transport that can push: WebSocket here, or IPC or EIP-1193. The examples below leave the transport out.
Transactions in full
Section titled “Transactions in full”Pass true to get each transaction as an object.
import { Effect, Stream } from "effect";import { Eth } from "@ethect/core/rpc";
const watch = Eth.subscribe.newPendingTransactions(true).pipe( Stream.runForEach((transaction) => Effect.log(`${transaction.from} sends ${transaction.value} wei`), ),);Not every node serves the full form. anvil does.
Only the ones you care about
Section titled “Only the ones you care about”A stream filters like an array. Here, the pending transactions sent to one address.
import { Stream } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const incoming = Eth.subscribe.newPendingTransactions(true).pipe( Stream.filter((transaction) => transaction.to === ADDRESS), Stream.map((transaction) => transaction.hash),);Addresses in results are lowercase, and so is an address you built: === compares them.
Returns
Section titled “Returns”Stream<Hash, Method.Error, Subscriptions>
Each item is the hash of a transaction. With true, the stream
is a Stream<Transaction.Transaction, Method.Error, Subscriptions> and each item is a
Transaction.
Parameters
Section titled “Parameters”full (optional)
Section titled “full (optional)”- Type:
boolean
true for the transactions in full, false or nothing for their hashes. The type of the items
follows the value.
import { Stream } from "effect";import { Eth } from "@ethect/core/rpc";
// Stream<Hash>const hashes = Eth.subscribe.newPendingTransactions(false);
// Stream<Transaction.Transaction>const transactions = Eth.subscribe.newPendingTransactions(true).pipe( Stream.map((transaction) => transaction.nonce),);Behavior
Section titled “Behavior”- Hashes by default. Without a param, the node is asked for nothing more than the kind.
- A pending transaction has no place yet. In the full form,
blockHash,blockNumberandtransactionIndexarenull. - Pending is not mined. A transaction in the pool can be replaced or dropped. Ask for its
receipt with
Eth.getTransactionReceiptto know what became of it. - The node’s own pool. Each node pushes what enters its own pool, so two nodes can push different transactions.
- Unsubscribes when the stream ends. When the stream ends or is interrupted, the transport
sends
eth_unsubscribe. - Carries on over a reconnection. The subscription is issued again on the new connection. The transactions that entered the pool in between are not replayed.
Errors
Section titled “Errors”The three errors every method can fail with. Here they fail the stream:
| Error | Reason | When |
|---|---|---|
RpcError |
the node’s own | the node refuses the subscription |
CodecError |
on: "result" |
an item is not a hash, or not a transaction in the full form |
TransportError |
Unreachable |
the connection is lost and the reconnect schedule runs out |
JSON-RPC method
Section titled “JSON-RPC method”eth_subscribe with the kind newPendingTransactions