Skip to content

Add Ethers.get_code/2 and EIP-7702 delegation helpers - #305

Merged
alisinabh merged 4 commits into
mainfrom
feat/get-code-and-delegation
Nov 16, 2026
Merged

alisinabh merged 4 commits into
mainfrom
feat/get-code-and-delegation

Conversation

@alisinabh

Copy link
Copy Markdown
Member

Adds a wrapper for eth_getCode and helpers to inspect EIP-7702 delegations. Authorizations with a wrong nonce are skipped silently on chain, so reading the authority's code afterwards is the only way to know whether a delegation took effect.

API

  • Ethers.get_code(address, overrides \\ []) / get_code!/2: wraps eth_getCode. Address handling and the :block override (integer or block tag), :rpc_client and :rpc_opts work as in Ethers.get_balance/2. Returns the code as raw bytes: {:ok, ""} for an account without code, and the 23-byte delegation designator for a delegated EOA.
    • Batchable as {:get_code, address} / {:get_code, address, block: block} in Ethers.batch/2.
    • New optional eth_get_code/3 callback on the RPC client adapter, checked with function_exported?/3. Existing custom adapters keep compiling; without the callback get_code/2 returns {:error, :not_supported}, like eth_fee_history/4, eth_create_access_list/3 and eth_config/1. Ethereumex.HttpClient already provides eth_get_code/3, so the default adapter delegates to it unchanged.
  • Ethers.Authorization.parse_delegation(code): pure. Returns {:ok, delegate_address} (checksummed) when code is exactly 0xef0100 ++ address (23 bytes), :error otherwise (empty code, contract code, any other length). Takes raw bytes only, no hex strings. Same rule as go-ethereum's types.ParseDelegation.
  • Ethers.get_delegation(address, overrides \\ []) / get_delegation!/2: get_code/2 + parse_delegation/1. Returns {:ok, delegate_address} when delegated, {:ok, nil} when the account has no delegation (both an account without code and a regular contract), {:error, reason} on RPC failure.

The Ethers.Authorization moduledoc now points to these where it explains that a mismatched nonce silently skips the authorization.

EIP-7702 text relied on

From the authorization processing steps (EIP status: Final):

  1. Verify the code of authority is empty or already delegated.
  2. Verify the nonce of authority is equal to nonce.
    ...
  3. Set the code of authority to be 0xef0100 || address. This is a delegation indicator.
    • If address is 0x0000000000000000000000000000000000000000, do not write the delegation indicator. Clear the account's code by resetting the account's code hash to the empty code hash 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470.
  4. Increase the nonce of authority by one.

If any step above fails, immediately stop processing the tuple and continue to the next tuple in the list.

and the length: "EXTCODESIZE returns 23 (the size of 0xef0100 || address)".

So a cleared account has empty code (get_code/2 returns "", get_delegation/2 returns nil). parse_delegation/1 only checks the format, so a designator for the zero address (which conforming chains never write) parses to the zero address.

Tests

  • Unit tests for parse_delegation/1: valid designator, empty code, contract bytecode, 22 and 24 bytes, bare prefix, 23 bytes with a wrong prefix, zero-address designator, hex string input.
  • Live tests against anvil for get_code/2:
    • a deployed contract (non-empty, found verbatim in its creation code, stable);
    • an EOA ("");
    • a binary address;
    • a :block override before and after deployment;
    • batch requests;
    • bang variants, invalid account and :not_supported.
  • Live tests for get_delegation/2: nil for an EOA and a contract, error propagation.
  • EIP-7702 end to end with a freshly generated, unfunded authority key. Every type-4 transaction is sponsored by a funded dev account and awaited, so no account shared with other tests is ever delegated:
    1. an authorization with a mismatched nonce: the transaction is included, the code stays "", get_delegation/2 returns nil;
    2. a valid authorization: get_code/2 returns 0xef0100 ++ delegate, get_delegation/2 returns the delegate, and querying the block before returns nil;
    3. a clearing authorization (zero address): the code is "" again and get_delegation/2 returns nil.

Included fix

:get_balance was missing from the batch action map, so {:get_balance, address} in Ethers.batch/2 was sent as the get_balance method and failed with "Method not found". It is now mapped to eth_getBalance, with a batch test.

Notes

Local tests ran against anvil 1.6.0-nightly with cached test contract ABIs (the local solc is broken). CI runs anvil 1.9.0.

Closes #293

🤖 Generated with Claude Code

https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6

alisinabh and others added 3 commits November 15, 2026 18:13
- `Ethers.get_code/2` (+ bang) wraps `eth_getCode` with the same address
  handling and `:block` override as `get_balance/2` and returns the code
  as a binary (`""` for accounts without code). Also batchable as
  `{:get_code, address}` / `{:get_code, address, block: block}`.
- New optional `eth_get_code/3` adapter callback; custom adapters without
  it get `{:error, :not_supported}`.
- `Ethers.Authorization.parse_delegation/1` parses the delegate out of a
  23-byte `0xef0100 ++ address` delegation designator.
- `Ethers.get_delegation/2` (+ bang) combines both, returning the
  delegate or `nil` for accounts without a delegation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
`:get_balance` was missing from `@rpc_actions_map`, so batched balance
requests were sent as the `get_balance` method and failed with
"Method not found".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
`Ethers.deployed_address/2` now returns EIP-55 checksummed addresses, so
the delegate returned by `get_delegation/2` must equal it as is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
@alisinabh
alisinabh force-pushed the feat/get-code-and-delegation branch from 119ee7f to ae6609f Compare November 15, 2026 23:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
@alisinabh
alisinabh merged commit e4c6b2e into main Nov 16, 2026
5 checks passed
@alisinabh
alisinabh deleted the feat/get-code-and-delegation branch November 16, 2026 01:24
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 Ethers.get_code/2 and an EIP-7702 delegation designator helper

1 participant