Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions typescript/packages/core/src/http/x402HTTPResourceServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
<!DOCTYPE html>
Expand All @@ -1045,7 +1052,7 @@ export class x402HTTPResourceServer {
${paywallConfig?.appLogo ? `<img src="${paywallConfig.appLogo}" alt="${paywallConfig.appName || "App"}" style="max-width: 200px; margin-bottom: 20px;">` : ""}
<h1>Payment Required</h1>
${resource ? `<p><strong>Resource:</strong> ${resource.description || resource.url}</p>` : ""}
<p><strong>Amount:</strong> $${displayAmount.toFixed(2)} USDC</p>
<p><strong>Amount:</strong> ${displayAmount.toFixed(6)} ${assetLabel}</p>
<div id="payment-widget"
data-requirements='${JSON.stringify(paymentRequired)}'
data-app-name="${paywallConfig?.appName || ""}"
Expand All @@ -1063,6 +1070,7 @@ export class x402HTTPResourceServer {

/**
* Extract display amount from payment requirements.
* Uses the registered scheme's decimal precision for the asset, falling back to 6.
*
* @param paymentRequired - The payment required object
* @returns The display amount in decimal format
Expand All @@ -1072,8 +1080,8 @@ export class x402HTTPResourceServer {
if (accepts && accepts.length > 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;
Expand Down
18 changes: 18 additions & 0 deletions typescript/packages/core/src/server/x402ResourceServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading