From 25e18ec5cbfaa1bd718aeb94c57dbe43dab80377 Mon Sep 17 00:00:00 2001 From: natsukingly Date: Sat, 4 Apr 2026 16:09:33 +0900 Subject: [PATCH] =?UTF-8?q?getDisplayAmount=20=E3=81=A7=E3=83=88=E3=83=BC?= =?UTF-8?q?=E3=82=AF=E3=83=B3=E3=81=AE=20decimals=20=E3=82=92=E5=8B=95?= =?UTF-8?q?=E7=9A=84=E5=8F=96=E5=BE=97=E3=81=97=20USDC=20=E3=83=8F?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/http/x402HTTPResourceServer.ts | 14 +++++++++++--- .../core/src/server/x402ResourceServer.ts | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/typescript/packages/core/src/http/x402HTTPResourceServer.ts b/typescript/packages/core/src/http/x402HTTPResourceServer.ts index 0cd074ea81..184076ecb4 100644 --- a/typescript/packages/core/src/http/x402HTTPResourceServer.ts +++ b/typescript/packages/core/src/http/x402HTTPResourceServer.ts @@ -1031,6 +1031,13 @@ export class x402HTTPResourceServer { // Fallback: Basic HTML paywall const resource = paymentRequired.resource; const displayAmount = this.getDisplayAmount(paymentRequired); + const firstAccept = paymentRequired.accepts?.[0]; + const assetLabel = + typeof firstAccept?.extra?.name === "string" + ? firstAccept.extra.name + : firstAccept?.asset + ? `...${firstAccept.asset.slice(-6)}` + : "Token"; return ` @@ -1045,7 +1052,7 @@ export class x402HTTPResourceServer { ${paywallConfig?.appLogo ? `${paywallConfig.appName || ` : ""}

Payment Required

${resource ? `

Resource: ${resource.description || resource.url}

` : ""} -

Amount: $${displayAmount.toFixed(2)} USDC

+

Amount: ${displayAmount.toFixed(6)} ${assetLabel}

0) { const firstReq = accepts[0]; if ("amount" in firstReq) { - // V2 format - return parseFloat(firstReq.amount) / 1000000; // Assuming USDC with 6 decimals + const decimals = this.ResourceServer.getAssetDecimalsForRequirements(firstReq); + return parseFloat(firstReq.amount) / 10 ** decimals; } } return 0; diff --git a/typescript/packages/core/src/server/x402ResourceServer.ts b/typescript/packages/core/src/server/x402ResourceServer.ts index 14f8aab79e..1c7f0a445d 100644 --- a/typescript/packages/core/src/server/x402ResourceServer.ts +++ b/typescript/packages/core/src/server/x402ResourceServer.ts @@ -232,6 +232,24 @@ export class x402ResourceServer { return !!findByNetworkAndScheme(this.registeredServerSchemes, scheme, network); } + /** + * Returns the decimal precision for the asset specified in the given payment requirements. + * Looks up the registered scheme for the network and delegates to its getAssetDecimals + * method if available. Falls back to 6 (standard for USDC stablecoins) when the scheme + * does not implement getAssetDecimals or is not registered. + * + * @param requirements - The payment requirements containing scheme, network, and asset + * @returns The number of decimal places for the asset + */ + getAssetDecimalsForRequirements(requirements: PaymentRequirements): number { + const scheme = findByNetworkAndScheme( + this.registeredServerSchemes, + requirements.scheme, + requirements.network as Network, + ); + return scheme?.getAssetDecimals?.(requirements.asset ?? "", requirements.network as Network) ?? 6; + } + /** * Registers a resource service extension that can enrich extension declarations. *