Eth.getTransactionReceipt
Returns the receipt of a mined transaction, or null while the transaction is pending or when
the node does not know it.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const receipt = await Eth.getTransactionReceipt( "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",).pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// receipt?.status: "success"// receipt?.gasUsed: 21000n// receipt?.blockNumber: 1nThe call works over every transport. The examples below leave the transport out.
Check that it succeeded
Section titled “Check that it succeeded”A mined transaction can have reverted. The receipt says which with status.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const outcome = Effect.gen(function* () { const receipt = yield* Eth.getTransactionReceipt(hash); if (receipt === null) return "not mined yet"; if (receipt.status === "reverted") return "reverted"; return `succeeded in block ${receipt.blockNumber}`;});Wait for a receipt
Section titled “Wait for a receipt”Tx.wait(hash) waits for a transaction: it looks at once, then once
per block, follows a replacement, and fails when the transaction reverted. With this method alone, ask
again until the result is not null.
import { Effect } from "effect";import { type Hash } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const waitForReceipt = (hash: Hash.Hash) => Eth.getTransactionReceipt(hash).pipe( Effect.delay("1 second"), Effect.repeat({ until: (receipt) => receipt !== null }), Effect.timeout("2 minutes"), );Each attempt waits a second, then asks. The result is a Receipt, without the null, and the
wait fails with a TimeoutError after two minutes: a transaction the node drops never gets a
receipt.
To send and wait in one call, use
Eth.sendTransactionSync or
Eth.sendRawTransactionSync where the node
serves them.
What it cost
Section titled “What it cost”The fee paid is the gas used times the price paid per gas, in wei.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const fee = Effect.gen(function* () { const receipt = yield* Eth.getTransactionReceipt(hash); if (receipt === null || receipt.effectiveGasPrice === undefined) return null; return receipt.gasUsed * receipt.effectiveGasPrice; // 21000n * 2000000000n});effectiveGasPrice is optional: a node may leave it out.
The address of a new contract
Section titled “The address of a new contract”When the transaction created a contract, the receipt holds its address.
import { Effect } from "effect";import { type Hash } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const deployedAt = (hash: Hash.Hash) => Eth.getTransactionReceipt(hash).pipe( // absent when the transaction did not create a contract Effect.map((receipt) => receipt?.contractAddress), );Returns
Section titled “Returns”Effect<Receipt.Receipt | null, Method.Error, Transport>
A Receipt, or null when the transaction is not mined.
Parameters
Section titled “Parameters”- Type:
Hash.Hash
The hash of the transaction: 32 bytes of hex.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const receipt = Eth.getTransactionReceipt( "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",);Behavior
Section titled “Behavior”nulluntil mined. A pending transaction has no receipt, and neither has a hash the node does not know. The two cases look the same here:Eth.getTransactionByHashtells them apart.- A revert is not an error. A transaction that reverted has a receipt with
status: "reverted". The call itself succeeds. - Logs included.
logsholds every log the transaction emitted, each with its position in the block. - A whole block at once.
Eth.getBlockReceiptsreturns the receipts of every transaction of a block in one request.
Errors
Section titled “Errors”The three errors every method can fail with. The case specific to this method:
| Error | Reason | When |
|---|---|---|
CodecError |
on: "params" |
the hash is not 32 bytes of hex: nothing is sent |
JSON-RPC method
Section titled “JSON-RPC method”eth_getTransactionReceipt