Tx.receipt
Says where one transaction stands now, in one request: its receipt when it is in a block, none when it is not. It waits for nothing, and fails if the transaction reverted.
import { Effect, Option } from "effect";import { type Hash, Tx } from "@ethect/core";
const status = (hash: Hash.Hash) => Effect.gen(function* () { // An Option: none when it is in no block yet. No head is read. const receipt = yield* Tx.receipt(hash); return Option.match(receipt, { onNone: () => "not in a block yet", onSome: ({ blockNumber }) => `in block ${blockNumber}`, }); });It needs a transport, and no signer.
Or Tx.wait
Section titled “Or Tx.wait”Tx.wait asks the same request first. The two differ when the answer
is none: Tx.receipt says so, and Tx.wait starts to watch the blocks.
| The transaction | Tx.receipt(hash) |
Tx.wait(hash) |
|---|---|---|
| is in a block | its receipt, in one request | its receipt, in the same one request |
| is in no block yet | none, at once | waits, with one look per block |
| is unknown to the node | none, at once | waits, then TxDropped after three heads and two minutes |
| was replaced | none, at once | follows a fee bump, or TxDropped with replacedBy |
| reverted | TxReverted |
TxReverted |
Use Tx.receipt where a program must answer now: a list of transactions with where each
stands, a request handler, a check before something is sent again.
What a revert may be
Section titled “What a revert may be”errors gives the custom errors to read the reason of a revert with, as for Tx.wait.
Err.of(calls) gives those of the calls.
import { Effect, Option } from "effect";import { type Address, Err, type Hash, Tx } from "@ethect/core";import { Token } from "./token";
const paid = (usdc: Address.Address, to: Address.Address, hash: Hash.Hash) => { const call = Token.at(usdc).transfer(to, 1_000_000n); // none: not in a block yet. A receipt: it is paid. return Tx.receipt(hash, { errors: Err.of([call]) }).pipe( Effect.map(Option.isSome), Effect.catchReason("TxReverted", "ERC20InsufficientBalance", (reason) => Effect.succeed(`The sender lacks ${reason.needed - reason.balance}`), ), );};import { Abi, Erc20 } from "@ethect/core";
// ERC-20 is built in, and declares no error. This adds the one the token reverts with.export const Token = Erc20.extend( Abi.parse(["error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed)"]),);Without errors, the reason is a plain Revert: a message, a panic, or
the data as it is.
Or Eth.getTransactionReceipt
Section titled “Or Eth.getTransactionReceipt”Eth.getTransactionReceipt is the request alone, and
gives what the node answered. A transaction that reverted has a receipt there, with
status: "reverted". Tx.receipt never gives one: a receipt it gives is always of a
transaction that succeeded, and a revert is a failure, with why.
| The node answers | Tx.receipt |
Eth.getTransactionReceipt |
|---|---|---|
| no receipt | Option.none() |
null |
a receipt, status: "success" |
Option.some(receipt) |
the receipt |
a receipt, status: "reverted" |
fails with TxReverted: its reason, and the receipt in receipts |
the receipt. Nothing says why |
What was submitted
Section titled “What was submitted”Tx.receipt takes one hash. For what Tx.submit gave, ask for each
hash: the answers come in the order of the calls.
import { Effect, Option } from "effect";import { Tx } from "@ethect/core";
/** How many of the transactions are in a block. A handle has no hash: its wallet says. */const mined = (pending: Tx.Transactions) => Effect.forEach(pending.hashes, (hash) => Tx.receipt(hash), { concurrency: "unbounded" }).pipe( Effect.map((receipts) => receipts.filter(Option.isSome).length), );Returns
Section titled “Returns”Effect<Option<Receipt>, Tx.Reverted<E> | Transport.Error, Transport>
The receipt of the transaction when it is in a block and succeeded.
None when the node has no receipt for it. E is the errors given in errors.
Parameters
Section titled “Parameters”transaction
Section titled “transaction”- Type:
Hash.Hash
The hash of the transaction.
options.errors (optional)
Section titled “options.errors (optional)”- Type:
ReadonlyArray<new (value: any) => unknown>
The custom errors a revert may be, as Err.make builds them. They type
the reason of a TxReverted. The type of the options is Tx.ReceiptOptions.
Behavior
Section titled “Behavior”- It is one request.
eth_getTransactionReceipt, and nothing else: no head is read, nothing is subscribed to, nothing is polled. - None is not a failure. A transaction in the pool, one that was dropped, one that was
replaced and a hash that was never sent all have no receipt, and the node says nothing more.
Tx.waittells them apart, by watching. - A transaction that reverted is
TxReverted. Its receipt is inreceipts. It has nocall: a hash is no call of a list. - The reason of a revert is found by running the transaction again, with
eth_callon the block before its own: two more requests, only when it reverted. A run that does not revert again, or a node that no longer has that state, leaves aRevert.Unknownwith no data. - It says nothing of depth. A receipt is of a block, which a reorg can still take. For a
depth,
Tx.wait(hash, { confirmations }): one more request when the transaction is already that deep. - It does not follow a replacement. A transaction that was sped up is mined under another hash, and this one stays none.
- It is asked of the transport in context. A hash says no chain. Under
Transport.on, the node of that chain is asked.
Errors
Section titled “Errors”| Error | When |
|---|---|
Tx.Reverted |
the transaction is in a block and reverted. reason is one of errors, or a plain revert |
RpcError, TransportError |
the node refused, or could not be reached. Nothing is known of the transaction |
Under the hood
Section titled “Under the hood”Only the Transport is asked.
Tx.receipt(hash, { errors })- your node
eth_getTransactionReceipt(hash) nullgives nonestatus: "success"gives the receiptstatus: "reverted"- your node
eth_getTransactionByHash(hash) - your node
eth_call({ from, to, data, value, gas }, block - 1)
fails with
TxReverted- your node
- your node