Skip to content

fix: pass the latest block to chain fee hooks in estimateFeesPerGas - #5611

Closed
mortezashojaei wants to merge 1 commit into
wevm:mainfrom
mortezashojaei:fix/estimate-fees-hook-block
Closed

mortezashojaei wants to merge 1 commit into
wevm:mainfrom
mortezashojaei:fix/estimate-fees-hook-block

Conversation

@mortezashojaei

Copy link
Copy Markdown
Contributor

ChainFeesFnParameters.block is documented as "The latest block" (src/types/chain.ts, and site/pages/docs/chains/fees.md for baseFeeMultiplier, defaultPriorityFee and estimateFeesPerGas). But when estimateFeesPerGas(client) is called directly, without a block, two chain fee hooks receive block: undefined:

  • fees.estimateFeesPerGas is called with block: block_ as Block, the caller-supplied block, even though the action has just fetched the latest block (the fetch happens precisely because a fee hook is defined).
  • fees.baseFeeMultiplier, when it is a function, runs before the block is fetched and also receives block_.

A hook that reads block as documented throws TypeError: Cannot read properties of undefined. prepareTransactionRequest always passes a block, so it is unaffected. fillTransaction already fetches the block before calling baseFeeMultiplier, and estimateMaxPriorityFeePerGas does the same for its own hook.

Repro

const client = createPublicClient({
  chain: defineChain({
    ...mainnet,
    fees: {
      async estimateFeesPerGas({ block }) {
        return { maxFeePerGas: block.number!, maxPriorityFeePerGas: 0n }
      },
    },
  }),
  transport: http(),
})

await estimateFeesPerGas(client)
// TypeError: Cannot read properties of undefined (reading 'number')

Fix

  • Pass the fetched block to fees.estimateFeesPerGas.
  • Compute the base fee multiplier after the block is fetched, and pass that block to fees.baseFeeMultiplier.

No requests are added. The only ordering change is that a static baseFeeMultiplier below 1 now throws BaseFeeScalarError after the block request instead of before it.

Tests

Added behavior: chain fee hooks receive the latest block to estimateFeesPerGas.test.ts, with one test per hook. They use createRpcProxy to return a fixed block, and each hook's result depends on block.number. Both tests fail on main with the TypeError above and pass with this change. The file's other tests still pass.

When estimateFeesPerGas was called without a block, the chain
fees.estimateFeesPerGas hook received the caller's undefined block
instead of the block the action had fetched, and fees.baseFeeMultiplier
ran before any block was fetched. Both hooks now receive the latest
block, as ChainFeesFnParameters and the chain fees docs describe.
@vercel

vercel Bot commented Nov 19, 2026

Copy link
Copy Markdown

@mortezashojaei is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3bc1852

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

jxom commented Nov 19, 2026

Copy link
Copy Markdown
Member

Thanks @mortezashojaei, nice catch and a clear write-up! We continued this in #5613, which keeps your commit and reworks the tests to extend the existing hook cases. Closing in favor of it.

@jxom jxom closed this Nov 19, 2026
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