perf: Phase 2 - Complete lightweight encoding implementation #228
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
This PR completes Phase 2 of the bundle size optimization roadmap by replacing viem utility imports with lightweight custom implementations across the entire codebase.
Changes
Files Modified (17)
src/util/encoding.ts,src/util/assertSubAccount.tsprepareCharge.ts,subscribe.ts,getPaymentStatus.ts,getSubscriptionStatus.ts,translatePayment.ts,validation.tsspend-permission/utils.ts,spend-permission/methods/requestRevoke.tsSigner.ts,utils.ts,findOwnerIndex.ts,handleAddSubAccountOwner.tscreateBaseAccountSDK.tsrollup.payment.config.jsLightweight Encoding Improvements
getAddress()using keccak256trim()supporting both Hex strings and Uint8ArraynumberToHex()to return "0x0" for zero values (matching viem behavior)Functions Replaced
The following viem utilities now use ~90% smaller lightweight implementations:
parseUnits,formatUnits- Token amount conversionsgetAddress,isAddress,isAddressEqual- Address validationbytesToHex,hexToBytes- Binary conversionsnumberToHex,toHex- Hex conversionsisHex,trim- Utility functionsNote: Complex functions like
encodeFunctionData,decodeEventLog, andkeccak256remain using viem as they require significant implementation complexity.Bundle Size Results
Main Browser Bundle
Payment Minimal Bundle (NEW)
Addressing Reviewer Feedback
Question: "How does replacing Viem imports save size exactly - the code you have here is very similar to Viem so unsure where the size diff comes from?"
Answer: While viem has tree-shakable exports, each viem utility import pulls in:
Our lightweight implementations:
The bundler can better eliminate dead code when there are fewer interconnected modules to analyze.
Test Plan
Next Steps
This completes Phase 2. Phase 3 optimizations (lazy loading, code splitting) can be pursued if further reductions are desired.
🤖 Generated with Claude Code