Skip to content

perf: request the block and priority fee concurrently in estimateFeesPerGas - #5527

Closed
jxom wants to merge 2 commits into
mainfrom
perf/estimate-fees-concurrency
Closed

jxom wants to merge 2 commits into
mainfrom
perf/estimate-fees-concurrency

Conversation

@jxom

@jxom jxom commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Without chain fee hooks, estimateFeesPerGas requested the latest block and then the priority fee, although the priority fee does not depend on the block. It also requested the block for legacy fees, which only chain fee hooks read. prepareTransactionRequest did the same for legacy fees.

  • estimateFeesPerGas now requests the block and the priority fee together (EIP-1559), and skips the block for legacy fees.
  • prepareTransactionRequest passes the block to legacy fee estimation only when the chain has fee hooks (it reuses the type-detection block when there is one).

Effect

Real in-process node:http JSON-RPC server adding 50 ms per request, real Viem clients over http(url). Depth is the longest chain of requests that each wait for the previous one.

Case main this PR
estimateFeesPerGas (EIP-1559) 102 ms, 2 requests, depth 2 51 ms, 2 requests, depth 1
estimateFeesPerGas({ type: 'legacy' }) 102 ms, 2 requests, depth 2 51 ms, 1 request, depth 1
prepareTransactionRequest, type: 'legacy' on an EIP-1559 network 153 ms, 4 requests, depth 3 102 ms, 3 requests, depth 2
prepareTransactionRequest, legacy network (type cache warm) 153 ms, 4 requests, depth 3 102 ms, 3 requests, depth 2
prepareTransactionRequest, default on an EIP-1559 network 102 ms, depth 2 unchanged

Behavior

  • Fee hooks: chains with a function under fees keep the sequential order (multiplier, block, then the hook or the priority fee), and legacy fees still request the block for them.
  • Error precedence: the block is awaited before the priority fee, so a block error still surfaces first. BaseFeeScalarError is still thrown before any request. Every early-started promise has .catch(() => {}) attached when it is created.
  • Extra request on non-EIP-1559 chains: estimateFeesPerGas() with the default type: 'eip1559' now sends eth_maxPriorityFeePerGas before throwing the same Eip1559FeesNotSupportedError.

Tests

  • New tests record pending requests through the http transport's onFetchRequest and onFetchResponse hooks against Anvil: block and priority fee overlap, legacy fees skip the block, and both stay sequential with a fee hook (in estimateFeesPerGas and prepareTransactionRequest).
  • Error precedence: a createRpcProxy test delays a failing block and fails the priority fee and its gas price fallback right away. It reports the block error, and reports the gas price error if the priority fee is awaited first.
  • estimateFeesPerGas.test.ts passes. In prepareTransactionRequest.test.ts, only with eth_fillTransaction > args: maxFeePerGas (under default tip) fails, and it fails the same way on main with Anvil 1.9.0.

The nonce manager (#5528) and cold-client priority fee (#5529) changes to prepareTransactionRequest are separate PRs. All three merge cleanly together, and the combined branch passes type checks and the same tests.

…PerGas

Without chain fee hooks, estimateFeesPerGas requested the block and then
the priority fee, although the priority fee does not depend on the block.
They are now requested together, and legacy fees no longer request the
block, which only chain fee hooks read. prepareTransactionRequest no
longer requests the block for legacy fees either.

The block error still takes precedence over the priority fee error, and
chains with fee hooks keep the sequential order.

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: 4094aa0

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:29.726048Z 4094aa0 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: b832fd33ef

ℹ️ 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 +7 to +9
import { createTestClient, defineChain } from '../../index.js'
import type { Block } from '../../types/block.js'
import type { Chain } from '../../types/chain.js'

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 Import newly used public APIs through the package entrypoint

The newly added defineChain and Chain imports use relative source paths, contrary to the explicit test-import requirement in AGENTS.md. Both are public exports of viem, so import them from 'viem'; the exceptions for internal helpers and chain definitions do not apply to these exports. This keeps the new tests exercising the supported package surface rather than bypassing it.

AGENTS.md reference: AGENTS.md:L188-L193

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.

Fixed in 4094aa0: defineChain and Chain now come from 'viem'.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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

pnpm add viem@pr-5527
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 — 4094aa01 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