perf(wallet): port MetaMask hook off web3 to viem - #1182
Open
Danswar wants to merge 3 commits into
Open
Conversation
🤖 PR Review Bot❌ Security: 1 critical vulnerabilitiesThis is an automated review. Please address the issues above. |
Collaborator
Author
|
This PR went through 3 review passes before reaching 0 outstanding issues. Fixed along the way:
|
Danswar
marked this pull request as ready for review
July 21, 2026 02:05
Collaborator
Author
Manual QA checklist before mergeThis PR hasn't been tested against a live wallet yet (see test plan). Checklist for whoever picks that up:
|
web3 is CommonJS-only with no sideEffects declaration, so it can't be tree-shaken and ships in full wherever it's imported. Since the MetaMask hook is wired into the app-wide WalletContextProvider (not lazy-loaded), this pulled the entire web3 dependency tree into the main bundle for every visitor regardless of wallet choice. Cuts main.js by ~1.02 MiB (29.8%) in a local production build.
The web3-based implementation explicitly nulled maxFeePerGas/ maxPriorityFeePerGas on every send, forcing legacy pricing regardless of whether a gasPrice override was given (see #163, DEV-2129: some MetaMask/ chain combinations misbehave with EIP-1559 fields). The viem port dropped this for the no-override path, letting viem's default fee estimation pick EIP-1559 again. Resolve gasPrice via publicClient.getGasPrice() when no override is passed, so every transaction still resolves to a legacy-type send.
…ed logic readBalance()'s native-coin branch returned an un-awaited promise from inside a try block, so a rejection (e.g. RPC failure) never hit the function's own catch and the documented throwExceptions=false fallback never fired. Mirror the already-correct ERC20 branch by awaiting the balance before returning. Also add unit tests for readBalance, createTransaction (including the gasPrice/legacy-pricing resolution), and sign against mocked viem clients -- this logic previously had no coverage at all. Soften the gasPrice comment to not overstate certainty about wallet-side behavior that isn't independently verified in this PR.
Danswar
force-pushed
the
feat/metamask-viem-migration
branch
from
July 23, 2026 20:18
c0da4c9 to
95ecfb3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/hooks/wallets/metamask.hook.ts: replaced allweb3.eth.*/Contract.methods.*calls withviem'spublicClient/walletClient(getBalance,readContract,writeContract,signMessage,sendTransaction,getAddresses), mirroring the pattern already used inwallet-connect.hook.ts.src/hooks/web3.hook.ts: the one remainingweb3.utils.toHexcall replaced with viem'snumberToHex.web3frompackage.json(the only one of the three that was ever a direct dependency —web3-core/web3-eth-contractwere transitive/type-only imports resolved through it) — confirmed no other file in the repo imports any of them.TextEncoder/TextDecoderpolyfill tosrc/setupTests.ts: jsdom's test environment doesn't provide these, and viem needs them at import time. This was latent —wallet-connect.hook.tsalready used viem but had no test coverage, so nothing had hit this before.jest.mock('web3', ...)blocks from the two existing test files, and added real unit test coverage forreadBalance,createTransaction, andsignagainst mocked viem clients (this logic had zero coverage before this PR).Why
web3is CommonJS-only with nosideEffects: falsedeclaration (confirmed via its publishedpackage.json), so it cannot be tree-shaken — importing any part of it always ships the whole package, plus its own legacy crypto-polyfill dependency tree (elliptic,bn.js,eth-lib,secp256k1,asn1.js, IPFS-eramultihashes/multibasefor the long-deadweb3-bzzSwarm support, etc.). SinceuseMetaMaskis wired into the app-wideWalletContextProvider(not behind a lazy route), all of that shipped in the main entry chunk for every visitor, regardless of wallet choice.Measured impact
Same-checkout before/after production build:
main.jsweb3)viem)Behavior preserved intentionally
.send()/.sendTransaction()blocks until the transaction is mined before returning the hash (web3'sPromiEventresolves with the receipt).viem'swriteContract/sendTransactionreturn the hash immediately instead, so an explicitpublicClient.waitForTransactionReceipt({ hash })was added to keep today's wait-for-mined behavior unchanged. Note this means MetaMask and WalletConnect (wallet-connect.hook.ts, which does not wait) already behave differently here — out of scope for this PR, flagging for awareness.maxFeePerGas/maxPriorityFeePerGason every send (added in [DEV-2129] Metamask fees [DEV-2109] WC sign message [DEV-2067] Limit request [DEV-2142] Signature hint #163/DEV-2129, for MetaMask/chain combinations that misbehaved with EIP-1559 fee fields). The first version of this port dropped that whenever nogasPriceoverride was given, silently reverting to viem's default fee estimation. Fixed in a follow-up commit:createTransactionnow resolvesgasPriceviapublicClient.getGasPrice()when no override is passed, sogasPriceis always a concrete value and the EIP-1559 fields are never set. Note this restores the same request shape as before at the code level, but — same as the rest of this PR — hasn't been independently verified against a live wallet.readBalance()'s native-coin branch had a pre-existing bug carried over from the web3 code (return promise.then(...)inside atrydoesn't get caught by thattry's owncatch), which the ERC20 branch didn't have. Fixed to match.Test plan
npm test— all 431 tests pass (36 suites), including new coverage forreadBalance/createTransaction/signtsc --noEmitclean on all non-test application codemain.jsmeasured before/after in the same checkoutpersonal_sign, and confirming the broadcast transaction is actually type0x0/legacy) on testnet before merging — not done as part of this PR