Fix: fetchToken() incorrectly rejects tokens with 0 decimals - #6
Open
draevik wants to merge 1 commit into
Open
Conversation
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.
|
@draevik is attempting to deploy a commit to the Lightchain AI Team on Vercel. A member of the Team first needs to authorize it. |
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.
What this fixes
fetchToken()(hooks/useTokens.ts) checked!name.result || !symbol.result || !decimals.resultto decide whether amulticalllookup succeeded. viem's multicall results carry astatusfield ("success" | "failure") specifically so callers don't have to infer success from the shape of the return value — but this code checked truthiness of.resultinstead. A token whosedecimals()genuinely returns0(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.tsasisValidTokenMetadata(), 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.tscovers: thedecimals = 0regression case, a normal token, and each of the three calls failing individually and all together.Verification
pnpm testfails specifically on thedecimals = 0case (1 of 6 assertions), then restored the fix and confirmed all 6 pass.pnpm build: the old inline check let TS narrow.resultfromT | undefinedtoTvia control-flow analysis, which a function call can't do. Added justified non-null assertions (.result!) at the one call site, sinceisValidTokenMetadatahas already verified all three calls succeeded immediately above.pnpm test: 6/6 passing.pnpm lintandpnpm build: both clean (aside from the same pre-existing, unrelated@metamask/sdkReact Native resolution warning present onmain).