Skip to content

Fix: fetchToken() incorrectly rejects tokens with 0 decimals - #6

Open
draevik wants to merge 1 commit into
lightchain-protocol:mainfrom
draevik:fix/fetchtoken-zero-decimals
Open

Fix: fetchToken() incorrectly rejects tokens with 0 decimals#6
draevik wants to merge 1 commit into
lightchain-protocol:mainfrom
draevik:fix/fetchtoken-zero-decimals

Conversation

@draevik

@draevik draevik commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

What this fixes

fetchToken() (hooks/useTokens.ts) checked !name.result || !symbol.result || !decimals.result to decide whether a multicall lookup succeeded. viem's multicall results carry a status field ("success" | "failure") specifically so callers don't have to infer success from the shape of the return value — but this code checked truthiness of .result instead. A token whose decimals() genuinely returns 0 (a valid value for some real ERC-20s) reads as falsy in JS, so it was misclassified as "call failed" and rejected as "Token not found," even though the call succeeded.

What this adds

Extracted the check into lib/token-metadata.ts as isValidTokenMetadata(), which checks .status === "success" on all three calls instead of the truthiness of .result. This is unit-testable without a wallet or RPC connection.

lib/token-metadata.test.ts covers: the decimals = 0 regression case, a normal token, and each of the three calls failing individually and all together.

Verification

  • Reverted the helper to the original falsy-check logic locally and confirmed pnpm test fails specifically on the decimals = 0 case (1 of 6 assertions), then restored the fix and confirmed all 6 pass.
  • Fixing the runtime check surfaced a real TypeScript error on pnpm build: the old inline check let TS narrow .result from T | undefined to T via control-flow analysis, which a function call can't do. Added justified non-null assertions (.result!) at the one call site, since isValidTokenMetadata has already verified all three calls succeeded immediately above.
  • Ran pnpm test: 6/6 passing.
  • Ran pnpm lint and pnpm build: both clean (aside from the same pre-existing, unrelated @metamask/sdk React Native resolution warning present on main).

isValidTokenMetadata now checks multicall .status instead of the
truthiness of .result, since a valid decimals() return of 0 was being
misread as a failed call.

Extracted the check into lib/token-metadata.ts so it's unit-testable,
and added a regression test covering the decimals=0 case.
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

@draevik is attempting to deploy a commit to the Lightchain AI Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant