/** * Paying a merchant that quotes only `exact`: through a bridge facilitator * whose relayer signs the merchant's EIP-3009 authorization, and the recourse * for a bridge payment the merchant did not take. * * agent.ts pays both kinds of merchant. It names a bridge facilitator * (`bridge: { facilitator }` in its paidFetch) from PX402_BRIDGE_FACILITATOR, * else the network's own: PRXVT's on Base. Without one, a 402 that quotes * only `exact` comes back unpaid, and the agent says what to set. */ import { PX402BridgeError, type Wallet } from "px402"; import { saveReceipts } from "./receipts-file.js"; /** What to say when a merchant quotes only `exact` and no bridge facilitator is configured. */ export function exactWithoutBridge(url: string): string { return ( `${url} quotes only the x402 "exact" scheme (no px402 entry). This wallet pays it through a ` + `bridge facilitator: set PX402_BRIDGE_FACILITATOR (node_modules/px402/docs/QUICKSTART.md, ` + `section 6, "Paying a merchant that quotes only exact") and run again. Nothing was paid.` ); } /** What a bridge payment that stopped means for the caller. */ export function explainBridgeError(error: PX402BridgeError): string { if (!error.committed) { // Nothing was paid: refused before the coins were committed. return `not paid (${error.reason}); ${error.retryable ? "retry later" : "do not retry"}`; } // The coins are committed and the merchant was not handed a payment. // Never retry: a retry pays again. The recourse brings the coins back. return `paid, not delivered (${error.reason}): wallet.pursueBridgeRecourse() follows it up`; } /** Between calls, like refresh(): follow up bridge payments the merchant did not take. */ export async function followUpBridgePayments(wallet: Wallet, receiptsFile: string): Promise { const report = await wallet.pursueBridgeRecourse(); for (const step of report.pursued) { console.log(`bridge payment ${String(step.seq)}: ${step.stage} (${step.outcome ?? "pending"})`); } // The schedule lives in the receipt file: save it, so a restart carries on. saveReceipts(receiptsFile, wallet); }