Skip to content

perf: estimate the priority fee with the type detection block - #5529

Closed
jxom wants to merge 2 commits into
mainfrom
perf/speculative-priority-fee
Closed

jxom wants to merge 2 commits into
mainfrom
perf/speculative-priority-fee

Conversation

@jxom

@jxom jxom commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

On a client that has not detected the network's transaction type yet, prepareTransactionRequest requested the latest block to detect it, and only then the priority fee. Without chain fee hooks, the priority fee is now estimated concurrently with that block.

Effect

Real in-process node:http JSON-RPC server adding 50 ms per request, real Viem clients over http(url), eth_fillTransaction unsupported.

Case main this PR
New client, EIP-1559 network 204 ms, 5 requests, depth 4 154 ms, 5 requests, depth 3
New client, legacy network 204 ms, 5 requests, depth 4 204 ms, 6 requests, depth 4
Client that has detected the type unchanged unchanged

Trade-off

On a legacy network, the speculative priority fee goes unused: one wasted eth_maxPriorityFeePerGas request per client, or two when the node does not support it (the fallback also sends eth_gasPrice). This happens once per client, because the detected type is cached. In exchange, every new client on an EIP-1559 network saves a round trip on its first transaction, which matters most for serverless apps that create a client per request. Legacy networks are rare now, so I think it's worth it, but it's separate so it can be dropped on its own.

Behavior

  • Only when fees is being filled and the chain has no function under fees; chains with fee hooks keep the sequential order.
  • Error precedence: the speculative promise has .catch(() => {}) attached when it is created, and is awaited in the same place as before, so a type-detection block error still surfaces first.

Tests

  • Recording test (Anvil, real http transport): on a new client, eth_maxPriorityFeePerGas is sent while the type-detection block is pending. It fails without the change.
  • Fee hook test: the priority fee waits for the block. It fails if the hook check is removed.
  • Legacy network test (createRpcProxy serves blocks without baseFeePerGas): the request comes back as legacy with a gas price and no EIP-1559 fields.
  • Only with eth_fillTransaction > args: maxFeePerGas (under default tip) fails, and it fails the same way on main with Anvil 1.9.0.

Independent of #5527 and #5528, which also touch prepareTransactionRequest. All three merge cleanly together.

On a client that has not detected the network's transaction type yet,
prepareTransactionRequest requested the latest block and only then the
priority fee. Without chain fee hooks, the priority fee is now estimated
concurrently with that block. On legacy networks the estimate goes
unused, once per client.

Co-authored-by: jxom <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-ba60-755b-bfd0-1a31a4a5bf1e
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 16, 2026 1:14am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ab64489

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T01:12:42.206822Z ab64489 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30d96ee600

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1492 to +1497
expect(requests).toMatchInlineSnapshot(`
{
"eth_estimateGas": [],
"eth_getBlockByNumber": [],
"eth_getTransactionCount": [],
"eth_maxPriorityFeePerGas": [],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Assert fee-hook outputs instead of recorded RPC calls

The new fee-hook test discards the prepared transaction and asserts only the recorded RPC call overlap; the preceding new test also snapshots this call tracking. This violates AGENTS.md's explicit requirement to test observable outputs rather than call tracking. In particular, the fee-hook test can pass even if the hook's result is ignored, provided the RPC ordering remains unchanged. Make the hook produce distinguishable fee values and assert those values in the prepared transaction.

AGENTS.md reference: AGENTS.md:L204-L208

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly addressed in ab64489. The fee hook test now uses a distinguishable multiplier (1.5) and snapshots the prepared request, so maxFeePerGas shows the hook's result (16 gwei vs 13 gwei by default). Keeping this. Recording which requests overlap through a real http transport is how the concurrency rule for these changes is enforced (same approach as the landed #5505 tests). The result alone can't tell a concurrent implementation from a sequential one, since both return the same value. The snapshot records rounds of requests, not hook invocations, so it fails exactly when a waterfall comes back.

method,
params: ['latest', false],
})
return { result: { ...block, baseFeePerGas: undefined } }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exercise legacy behavior without stubbing block responses

The new legacy-network test fabricates a legacy block by removing baseFeePerGas from an actual EIP-1559 block response. Although the proxy uses real HTTP, this response override is still a stub, which AGENTS.md explicitly prohibits. It also leaves the remaining RPC methods backed by an EIP-1559 node rather than exercising a real legacy network. Configure a real test chain without EIP-1559 support and assert the resulting prepared transaction instead.

AGENTS.md reference: AGENTS.md:L155-L158

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this. The shared Anvil instances are all EIP-1559 forks, and adding a pre-London Anvil to the global setup for one test is out of scope here. The proxy only removes baseFeePerGas from real block responses, and every other request goes to Anvil. The test asserts the prepared transaction (legacy type, gas price, no EIP-1559 fields).

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5529 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5529
pnpm add [email protected]

jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle.

@jxom jxom closed this Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — ab644895 Deployed Nov 16, 2026 by vercel[bot]
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.

2 participants