Skip to content

Add a decoded Ethers.Receipt struct for transaction receipts - #310

Merged
alisinabh merged 6 commits into
mainfrom
feat/receipt-struct
Nov 16, 2026
Merged

alisinabh merged 6 commits into
mainfrom
feat/receipt-struct

Conversation

@alisinabh

@alisinabh alisinabh commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Ethers.get_transaction_receipt/2 used to return the node's JSON untouched: a string-keyed map with hex quantities and undecoded logs. It now returns an Ethers.Receipt struct, and Ethers.Receipt.decode_logs/2 decodes the receipt's logs with the existing event machinery.

Ethers.Receipt

Fields follow the ReceiptInfo schema of execution-apis src/schemas/receipt.yaml (last changed in d0ea69f3, "add blockTimestamp to logs"). Each field is the snake case of its RPC member name:

Field Decoded as
transaction_hash, block_hash, root 0x hex strings, as elsewhere in the library
transaction_index, block_number, gas_used, cumulative_gas_used, effective_gas_price, blob_gas_used, blob_gas_price integers
type integer EIP-2718 type id (e.g. 2, or 126 for OP Stack deposits)
status :success, :failure, or nil for pre-Byzantium receipts, which carry root instead
from, to, contract_address checksummed addresses, nil when absent
logs_bloom binary
logs kept as returned by the node
extra the members the node returned that this version does not decode, as %{"rpcMemberName" => undecoded_value}
  • A member the node omits decodes to nil.
  • extra is the node's receipt map minus every member the decoder knows, whether or not that member was present. Keys stay the node's strings and values stay undecoded. It holds chain-specific members (e.g. OP Stack l1Fee, l1GasPrice, …) and node extras (e.g. anvil's non-standard blockTimestamp), and is %{} when the node sent nothing unknown, never nil.
  • Forward compatibility: extra only holds members this version does not decode, so a member can move from extra to a struct field in a later release.
  • A member that doesn't match the schema (e.g. status: "0x2", a non-hex quantity or a bad address) returns {:error, {:invalid_receipt_member, "status"}} instead of raising or being silently dropped. A result that isn't an object returns {:error, :invalid_receipt}.

The :get_transaction_receipt action of Ethers.batch/2 now works and returns the same struct. It was never mapped to eth_getTransactionReceipt, so the node answered Method not found.

Logs and decode_logs/2

A receipt holds logs from every contract the transaction touched, and they can't be decoded without the ABIs of their events. So logs stays as the node's maps, which is the form Ethers.Event.decode/2 and Ethers.Event.find_and_decode/2 already accept.

Ethers.Receipt.decode_logs(receipt, filter) takes an event filter, a combined event filter or a contract's EventFilters module. It returns %Ethers.Event{} structs in log order. A log matches when:

  • its topic_0 is the event's signature hash,
  • its indexed values match the filter (nil is a wildcard),
  • it comes from the filter's default address, if it has one (compared case-insensitively),
  • its topic count equals 1 + the number of indexed arguments, so ERC-20 and ERC-721 Transfer logs are not decoded as each other,
  • its data ABI-decodes as the event's non-indexed arguments. Any contract can emit a log with an event's signature and malformed data; such a log is skipped instead of raising. Only the MatchError and ArgumentError the decoder raises are rescued.

The first three rules are the ones Ethers.get_logs/2 applies. Logs matching none of the events are skipped.

Breaking change

Ethers.get_transaction_receipt/2, Ethers.get_transaction_receipt!/2 and the :get_transaction_receipt batch action return %Ethers.Receipt{} instead of the raw map. {:error, :transaction_receipt_not_found} is unchanged. Malformed receipts now return {:error, {:invalid_receipt_member, member}}. This is described in guides/upgrading.md under "Upgrading to 1.0.0".

# Before (0.8.x)
%{"status" => "0x1", "gasUsed" => gas_used_hex} = receipt

# After (1.0.0)
%Ethers.Receipt{status: :success, gas_used: gas_used} = receipt
l1_fee = receipt.extra["l1Fee"]

Ethers.deployed_address/2 is unchanged by this PR: it still reads the RPC map directly and, since #312, returns the checksummed address, the same value as receipt.contract_address. The test helper wait_for_transaction! only matches {:ok, _}, so it works as before.

Design decisions

  • type as an integer rather than a transaction module. It covers every type, including L2 types the library has no module for. It doesn't depend on the :transaction_types config. A receipt has no transaction payload, so a payload module wouldn't add anything. To compare, use Ethers.Transaction.Eip1559.type_id().
  • Strict decoding with error tuples. A malformed member is a node bug. Naming the member makes it easy to track down, and a silently nil status would be worse than an error.
  • Raw logs plus decode_logs/2 rather than decoding logs up front, which is impossible without the ABIs. A future wait_for_transaction_receipt can return this struct as is.
  • Strict address decoding. Receipt addresses are validated and then checksummed. A malformed one returns {:error, {:invalid_receipt_member, member}}, matching the rest of the decoder. That is why it doesn't use Return EIP-55 checksummed addresses everywhere #312's tolerant Ethers.Utils.maybe_checksum_address/1, which returns malformed values unchanged. Since Return EIP-55 checksummed addresses everywhere #312, the receipt's addresses compare equal to Ethers.Event.address and Ethers.deployed_address/2.

Open points (not addressed here)

  • Ethers.get_logs/2 can misdecode ERC-20 vs ERC-721 Transfer logs. It goes through CombinedEventFilter.decode_logs/2, which only checks topic_0.
  • Ethers.Event.decode/2 raises KeyError on nodes that omit log members such as removed or logIndex. decode_logs/2 deliberately doesn't rescue this.
  • decode_logs/2 has no :address override. One could be added later.

Not verified

  • The unit-test receipts are hand-built in the schema's shape with made-up hashes, not captured from mainnet.
  • Not run against geth, reth or an L2 node. The live tests ran against anvil 1.6.0 locally (CI uses 1.9.0) and don't assert node-specific extras: anvil adds a numeric blockTimestamp and returns blobGasPrice for non-blob transactions.

Closes #294

🤖 Generated with Claude Code

https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6

alisinabh and others added 5 commits November 15, 2026 18:14
`Ethers.get_transaction_receipt/2` returned the node's JSON untouched: a
string-keyed map with hex quantities. It now returns an `Ethers.Receipt`
struct decoded after the execution-apis `ReceiptInfo` schema:

- quantities (including `type`, the EIP-2718 type id, also for types
  the library has no module for) are integers
- `status` is `:success | :failure`, `nil` for pre-Byzantium receipts,
  which carry `root` instead
- addresses are checksummed, hashes stay `0x` hex strings, `logs_bloom`
  is a binary and `logs` are kept as returned by the node
- omitted members are `nil`, unknown members are ignored and the node's
  map is kept in `raw` for chain specific members (OP Stack L1 fees...)
- a member not matching the schema returns
  `{:error, {:invalid_receipt_member, member}}` instead of raising

The `:get_transaction_receipt` batch action now works and returns the
same struct: it was not mapped to `eth_getTransactionReceipt`, so the
node answered "Method not found".

This is a breaking change for 1.0.0.

Issue: #294

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
A receipt holds the logs of every contract the transaction called, so
its logs are kept as returned by the node. `decode_logs/2` decodes the
ones matching an event filter, a combined event filter or a contract's
EventFilters module into `%Ethers.Event{}` structs, in emission order.

A log matches like with `eth_getLogs` (topic_0, indexed argument values
with `nil` as a wildcard, the filter's default address compared case
insensitively) plus two checks receipts need: the topic count must equal
1 + the number of indexed arguments, so ERC-20 and ERC-721 `Transfer`
logs are not decoded as each other, and logs whose data does not ABI
decode as the event are skipped instead of raising. Logs matching none
of the events are skipped.

Also note in the typedoc that some nodes (anvil) return `blobGasPrice`
for non-blob transactions.

Issue: #294

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
`raw` duplicated the whole node map, including every member the struct
already decodes. It is replaced by `extra`, which holds only the members
this version of the library does not decode (the receipt map minus every
member of the decoder table, present or not): chain specific members such
as the OP Stack L1 fee fields, and node extras such as anvil's
`blockTimestamp`. Keys and values stay as returned by the node, and it is
`%{}` when there are none. A member can move from `extra` to a struct
field in a later release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Since #312, `Ethers.Event.address` and `Ethers.deployed_address/2` are
checksummed, so the receipt tests compare them with the receipt's
addresses directly. The default address case uses a lowercase address to
keep covering case-insensitive matching in `decode_logs/2`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
@alisinabh
alisinabh merged commit 19bb78c into main Nov 16, 2026
5 checks passed
@alisinabh
alisinabh deleted the feat/receipt-struct branch November 16, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a decoded Ethers.Receipt struct for transaction receipts

1 participant