INNER CODE UNIT · JavaScript
PaymentRequiredError
nirholas/XActions · packages/xactions-edge/index.js:54
export class PaymentRequiredError extends XActionsError {
/**
* @param {string} tool
* @param {object} payload The server's x402 body.
*/
constructor(tool, payload) {
const accepts = payload?.accepts || [];
const first = accepts[0] || {};
const decimals = 6; // USDC on both supported chains
const amount = Number(first.maxAmountRequired ?? first.amount ?? 0) / 10 ** decimals;
const price = amount ? `$${amount.toFixed(6).replace(/0+$/, '').replace(/\.$/, '')}` : 'a fee';
const chains = accepts.map((entry) => NETWORK_NAMES[entry.network] || entry.network);
super(
`${tool} costs ${price} in USDC (${chains.join(' or ') || 'x402'}). `
+ 'Pay with an x402 client and retry: https://xactions.app/docs/guides/x402',
{ tool, status: 402 },
);
this.name = 'PaymentRequiredError';