fix(paywall): use dynamic token decimals instead of hardcoding 6#1980
Open
ryanRfox wants to merge 2 commits intox402-foundation:mainfrom
Open
fix(paywall): use dynamic token decimals instead of hardcoding 6#1980ryanRfox wants to merge 2 commits intox402-foundation:mainfrom
ryanRfox wants to merge 2 commits intox402-foundation:mainfrom
Conversation
f08dc05 to
9a67807
Compare
|
@ryanRfox is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Author
|
Note: #1926 fixes the same hardcoded-decimals bug in the server-side fallback paywall ( |
The EVM paywall displays incorrect amounts for any token that does not use 6 decimal places. An 18-decimal token priced at 0.001 raw units renders as $1,000,000,000 because the code divides by 10^6 everywhere. Root cause: the paywall was built assuming USDC (6 decimals) is the only payment token. This assumption appears in three places: 1. Server-side HTML generation (index.ts) divides the raw amount by a hardcoded 10^6 to produce the display value injected into the template. 2. Client-side balance display (EvmPaywall.tsx) calls formatUnits with a hardcoded 6 and reads the token address from a 3-chain USDC lookup table that returns 0 for any other chain. 3. The balance utility (utils.ts) only knows USDC addresses on Ethereum, Base, and Base Sepolia — every other chain silently shows a zero balance. Fix: Server-side (index.ts): look up the token's decimal precision from a known-decimals map aligned with DEFAULT_STABLECOINS. Only non-6-decimal chains need entries; everything else falls back to 6. This keeps the server-side path simple with no RPC calls. Client-side (EvmPaywall.tsx): read the token contract address from the payment requirement's `asset` field (which the server already populates) and query the standard ERC-20 decimals() function on-chain. This works for any compliant token without maintaining a lookup table. utils.ts: replace the USDC-specific getUSDCBalance (internal helper, not part of the public API) with generic getTokenBalance and getTokenDecimals functions that accept a token address parameter. The function signature changes from 2 args to 3, but no external consumer can import it — it is only used by the bundled React component inside the paywall HTML template. **Breaking change note:** the internal helper getUSDCBalance in utils.ts is removed and replaced by getTokenBalance (different signature: takes an explicit token address instead of looking it up by chain ID). This function is not exported from the package's public API — it is only consumed by the bundled EvmPaywall React component inside the paywall HTML template. No server operator code calls it directly. However, anyone who has forked the paywall and imports from the file path (bypassing package exports) would need to update their import and call site. Regenerated Go, Python, and TS paywall templates. **AI disclosure:** This PR was prepared with the assistance of a coding agent and reviewed by Ryan R. Fox (an actual human) before submission.
b3d6d97 to
6dad9dd
Compare
Read token name from firstRequirement.extra.name with a neutral "Token" fallback instead of hardcoding "USDC". Aligns with x402-foundation#1926 which takes the same approach for the server-side fallback paywall. Affected display strings: payment header, testnet prompt, balance display, amount row, insufficient balance error message. This PR was prepared with the assistance of a coding agent and reviewed by Ryan R. Fox (an actual human) before submission.
6dad9dd to
178b613
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.
Description
The EVM paywall displays incorrect amounts for any token that does not use 6 decimal places. An 18-decimal token priced at 0.001 raw units renders as $1,000,000,000 because the code divides by 10^6 everywhere.
Fixes #1979
Related: #1976
Root Cause
The paywall was built assuming USDC (6 decimals) is the only payment token. This assumption appears in three places:
index.ts) divides the raw amount by a hardcoded 10^6 to produce the display value injected into the template.EvmPaywall.tsx) callsformatUnitswith a hardcoded6and reads the token address from a 3-chain USDC lookup table that returns0for any other chain.utils.ts) only knows USDC addresses on Ethereum, Base, and Base Sepolia — every other chain silently shows a zero balance.Fix
Server-side (
index.ts): Look up the token's decimal precision from a known-decimals map aligned withDEFAULT_STABLECOINS. Only non-6-decimal chains need entries; everything else falls back to 6. This keeps the server-side path simple with no RPC calls.Client-side (
EvmPaywall.tsx): Read the token contract address from the payment requirement'sassetfield (which the server already populates) and query the standard ERC-20decimals()function on-chain. This works for any compliant token without maintaining a lookup table.utils.ts: Replace the USDC-specificgetUSDCBalance(internal helper, not part of the public API) with genericgetTokenBalanceandgetTokenDecimalsfunctions that accept a token address parameter. The function signature changes from 2 args to 3, but no external consumer can import it — it is only used by the bundled React component inside the paywall HTML template.Breaking Change Note
The internal helper
getUSDCBalanceinutils.tsis removed and replaced bygetTokenBalance(different signature: takes an explicit token address instead of looking it up by chain ID). This function is not exported from the package's public API — it is only consumed by the bundledEvmPaywallReact component inside the paywall HTML template. No server operator code calls it directly. However, anyone who has forked the paywall and imports from the file path (bypassing package exports) would need to update their import and call site.Regenerated Go, Python, and TS paywall templates.
AI Disclosure
This PR was prepared with the assistance of a coding agent and reviewed by Ryan R. Fox (an actual human) before submission.
Testing
pnpm test— 41/41 tasks)pnpm lint:check)pnpm format)Checklist
pnpm format && pnpm lint:check)pnpm test)pnpm --filter @x402/paywall build:paywall)