Skip to content

fix: reject tips above a zero maxFeePerGas - #5313

Closed
mortezashojaei wants to merge 9 commits into
wevm:mainfrom
mortezashojaei:fix/tip-above-zero-fee-cap
Closed

mortezashojaei wants to merge 9 commits into
wevm:mainfrom
mortezashojaei:fix/tip-above-zero-fee-cap

Conversation

@mortezashojaei

@mortezashojaei mortezashojaei commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

EIP-1559 makes a transaction invalid when maxPriorityFeePerGas is greater than maxFeePerGas, and nodes reject it (Geth: max priority fee per gas higher than max fee per gas). Viem checks for this in assertTransactionEIP1559, but the check tests truthiness, so a maxFeePerGas of 0n skips it and the transaction is serialized and signed:

await account.signTransaction({
  chainId: 1,
  maxFeePerGas: 0n,
  maxPriorityFeePerGas: 76984525153045n,
})
// signs without error; the node rejects it at broadcast

The same input with maxFeePerGas: 1n throws TipAboveFeeCapError as expected. The late failure costs a wasted signature (relevant for remote signers such as a KMS) and a confusing error instead of the usual fail-fast one.

The same check runs when validating requests and preparing transactions, so this fixes it in signing, request validation and the related error messages:

  • assertTransactionEIP1559: checks that both fees are bigints instead of truthy. EIP-7702 and EIP-4844 transactions call this function too.
  • assertRequest: same check. This matters for JSON-RPC accounts, where Viem never serializes locally and the request went straight to the wallet.
  • prepareTransactionRequest: when maxFeePerGas: 0n is passed without a tip, the tip is estimated but the MaxFeePerGasTooLowError guard was skipped. It now throws before signing.
  • TipAboveFeeCapError: the message dropped a fee of 0n. Both the tip and the fee cap now print it (= 0 gwei), since getNodeError passes whatever the caller supplied.

The checks use typeof x === 'bigint' rather than !== undefined, so a null fee from an untyped caller or raw RPC JSON is still skipped like before. With !== undefined, 1n > null is true and building the error message crashed in formatGwei. That includes getNodeError, which passes the caller's request fields into TipAboveFeeCapError. When a field is left out, the checks are still skipped, so partial requests keep working, and 0n with 0n still passes.

assertRequest also runs in read-only actions (call, estimateGas, createAccessList, simulateBlocks, Linea's estimateGas, zkSync's assertEip712Request and Tempo's simulate). A maxFeePerGas of 0n with a non-zero tip now throws TipAboveFeeCapError there before reaching the node. Geth already rejects that combination in eth_call and eth_estimateGas (maxFeePerGas (0) < maxPriorityFeePerGas (...)), so only the error source changes.

FeeCapTooLowError had the same truthiness check in its message. It is most likely to be hit with a 0n fee cap, and dropped the value while keeping the unit, printing (`maxFeePerGas` gwei). It now prints = 0 gwei, and a missing fee prints (`maxFeePerGas`).

maxFeePerGas: 0n with no tip fails before signing on both prepareTransactionRequest paths, but with different errors. When Viem estimates fees itself, it throws MaxFeePerGasTooLowError. When the node supports eth_fillTransaction, the node fills the tip, Viem's fee estimation is skipped, and the final assertRequest throws TipAboveFeeCapError.

Added the reproduction above as a signTransaction test, plus zero fee cap tests for EIP-1559, EIP-7702, EIP-4844, assertRequest, both prepareTransactionRequest paths and the getNodeError message. Each fails on main and passes with this change. The eth_fillTransaction test was run against Anvil 1.7.1, the version CI pins; older Anvil lacks the method and falls back to Viem's own fee step. Added 0n message tests for FeeCapTooLowError and for a zero tip in TipAboveFeeCapError, and tests that 0n with 0n still passes assertTransactionEIP1559 and assertRequest, which zero-gas private chains depend on. Added null fee cap tests for assertRequest and TipAboveFeeCapError (which getNodeError builds from the caller's request); both crash with !== undefined and pass with this change. The existing FeeCapTooLowError snapshot without a fee was updated to drop the stray gwei. The rest of the transaction utils, errors, Celo, zkSync and OP Stack serializer tests pass unchanged, and pnpm check:types and Biome are clean.

Not included: fillTransaction only applies the caller's fee preferences when the node returned a non-zero fee. That changes the behavior of a different action, so it is better as its own PR.

@vercel

vercel Bot commented Oct 26, 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 Oct 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 59e8230

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/viem@5313

commit: 59e8230

jxom commented Oct 27, 2026

Copy link
Copy Markdown
Member

Thanks @mortezashojaei, this is a solid fix with a great write-up. We're landing it through #5319, which keeps your commit as-is with your authorship and adds two small follow-ups: FeeCapTooHighError now uses the same typeof === 'bigint' check, and there's a call test for the zero fee cap case. Closing this one in favor of that PR.


Generated by Claude Code

@jxom jxom closed this Oct 27, 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