Skip to content

perf(sdk): drop viem's chain list from the mobile bundle - #14711

Merged
dylanjeffers merged 1 commit into
mainfrom
perf/viem-chains-mainnet
Oct 28, 2026
Merged

dylanjeffers merged 1 commit into
mainfrom
perf/viem-chains-mainnet

Conversation

@dylanjeffers

Copy link
Copy Markdown
Contributor

Summary

Metro resolves viem's CommonJS build (viem/_cjs/chains/index.js) and doesn't tree-shake, so import { mainnet } from 'viem/chains' in createSdkWithServices.ts pulled all 383 chain definitions into the mobile bundle, plus the zksync / celo / op-stack / linea formatters those chains reference.

viem 2.21.21's exports map has no per-chain subpath (only ./chains and ./chains/utils), so this defines mainnet in the sdk (packages/sdk/src/sdk/utils/mainnet.ts) with defineChain, copying viem's fields. A new test asserts it toEquals viem's mainnet, so the copy can't drift silently on a viem upgrade.

That was the only viem/chains import reachable from mobile (sdk, common, mobile, spl, eth). The other imports are in web, identity-service and an sdk script, and none of them ship to mobile.

Bundle size (iOS, react-native bundle --dev false --minify true)

before (main 999e753) after Δ
main.jsbundle (min) 13,272,559 B 13,003,080 B −269,479 B (−263 KiB)
gzip 3,152,937 B 3,111,375 B −41,562 B
viem/_cjs/chains/** 231,540 B (383 files) 0 −231,540 B
viem/_cjs/{zksync,celo,op-stack,linea} 19,417 B 0 −19,417 B
sdk/utils/mainnet.ts — 545 B +545 B

The rest of the difference is unmapped bytes and module wrappers (measured with source-map-explorer --json).

Web: Vite already tree-shakes viem's ESM build, and web/src/services/audius-sdk/audiusSdk.ts still imports viem's mainnet for its relay wallet client. The sdk's inline copy therefore adds about 0.5 KB minified to web (esbuild estimate; I didn't run a full web build).

Behavior

The chain passed to the default ethPublicClient / ethWalletClient is deep-equal to viem's mainnet: same id, RPC defaults, ENS and multicall3 contracts. The eth RPC endpoint still comes from servicesConfig.ethereum.rpcEndpoint.

Test plan

  • packages/sdk: vitest run: 31 files, 249 passed / 2 skipped. Includes the new mainnet.test.ts and createSdkWithServices.lazy.test.ts.
  • packages/sdk: eslint and tsc --noEmit pass
  • iOS release bundle builds; before/after numbers above

🤖 Generated with Claude Code

Metro resolves viem's CommonJS build and doesn't tree-shake, so
`import { mainnet } from 'viem/chains'` pulled all 383 chain definitions
(plus the zksync/celo/op-stack/linea formatters they use) into the mobile
bundle. viem has no per-chain export, so define mainnet in the sdk with
defineChain, and add a test that it matches viem's definition.

iOS bundle: 13,272,559 -> 13,003,080 bytes min (-263 KiB),
3,152,937 -> 3,111,375 bytes gzip (-41 KiB).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b1c24f3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@audius/sdk Patch
@audius/sdk-legacy Patch
@audius/protocol-dashboard Patch
@audius/sp-actions 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

@dylanjeffers
dylanjeffers merged commit de6f379 into main Oct 28, 2026
24 of 25 checks passed
@dylanjeffers
dylanjeffers deleted the perf/viem-chains-mainnet branch October 28, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant