Skip to content

Eth.getTransactionByHash

Returns a transaction by its hash, pending or mined, or null 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 transaction = await Eth.getTransactionByHash(
"0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
).pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// transaction?.type: "eip1559"
// transaction?.value: 1n
// transaction?.blockNumber: 1n

The call works over every transport. The examples below leave the transport out.

A pending transaction has no place in the chain yet: its blockNumber is null.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const state = Effect.gen(function* () {
const transaction = yield* Eth.getTransactionByHash(hash);
if (transaction === null) return "unknown";
if (transaction.blockNumber === null) return "pending";
return `mined in block ${transaction.blockNumber}`;
});

The result is a union of transaction types. Compare type to reach the fields of one.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const feeCap = Effect.gen(function* () {
const transaction = yield* Eth.getTransactionByHash(hash);
if (transaction === null) return null;
return transaction.type === "eip1559"
? transaction.maxFeePerGas // 2000000000n
: null;
});

The Transaction page lists the fields of every type.

A transaction that creates a contract has no recipient: to is null, and input is the code that ran to create it.

import { Effect } from "effect";
import { type Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const isCreation = (hash: Hash.Hash) =>
Eth.getTransactionByHash(hash).pipe(
Effect.map((transaction) =>
// null when the node does not know the transaction
transaction === null ? null : transaction.to === null,
),
);

Effect<Transaction.Transaction | null, Method.Error, Transport>

A Transaction, or null when the node does not know the hash.

The hash of the transaction: 32 bytes of hex.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const transaction = Eth.getTransactionByHash(
"0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
);
  • null for a hash the node does not know. That covers a hash that never existed and a transaction the node dropped from its pool.
  • null fields while pending. blockHash, blockNumber and transactionIndex are null until the transaction is mined.
  • Found does not mean succeeded. A transaction that reverted is returned like any other. Its outcome is the status of its receipt.
  • No receipt data. Gas used, logs and the address of a created contract are on the receipt.

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

eth_getTransactionByHash