/** * What the wallet holds, spending nothing: recover, then print the runway, * whether it can pay a call now, and the key epochs it holds. No withdrawal, * no payment, no melt. * * node --env-file=.env inspect.js [url] * * With a URL it reads that merchant's 402 (one unpaid request) and asks about * its price; without one, about TYPICAL_PRICE_BASE_UNITS. */ import type { Readiness, Wallet } from "px402"; import { bootWallet, quoteFor, worstCaseCost } from "./agent.js"; import { loadConfig } from "./config.js"; import { TYPICAL_PRICE_BASE_UNITS } from "./wallet.js"; /** One line for each readiness status, with what to do about it. */ export function describeReadiness(state: Readiness): string { switch (state.status) { case "ready": return "ready: spendable coins cover it now"; case "holding": return ( `holding: covered in ${String(state.secondsRemaining)} s, once ` + (state.refreshingBaseUnits > 0n ? "change is melted and its coins' hold passes" : "coins in their hold unlock") ); case "insufficient": return state.fragmented ? `insufficient: the value is there in coins too small for one payment; prewarm buys larger ones` : `insufficient: short by ${state.shortfallBaseUnits.toString()} base units; prewarm, and deposit first if the reserve is empty`; } } /** The runway, readiness for one price, and the epochs: everything read, nothing spent. */ export function printState(wallet: Wallet, price: bigint): void { const runway = wallet.runway(); console.log( `balance ${runway.balanceBaseUnits.toString()} base units: ${runway.spendableBaseUnits.toString()} spendable, ` + `${runway.heldBaseUnits.toString()} in their hold` + (runway.heldUntil === null ? "" : ` until ${new Date(runway.heldUntil * 1000).toISOString()}`), ); console.log( `runway: ${String(runway.calls)} calls at ${runway.callCostBaseUnits.toString()} (${runway.costBasis}); ` + `refill due: ${String(wallet.needsRefill())}`, ); const cost = worstCaseCost(wallet, price); const state = wallet.readiness(cost); console.log(`a ${price.toString()} call presents at most ${cost.toString()}: ${describeReadiness(state)}`); console.log(`key epochs held: ${wallet.keysets.map((k) => k.epochId.toString()).join(", ") || "none"}`); for (const epoch of runway.epochs ?? []) { console.log( ` epoch ${epoch.epochId.toString()} (${epoch.status}): ${epoch.spendableBaseUnits.toString()} spendable, ` + `${epoch.heldBaseUnits.toString()} held, ${epoch.refreshingBaseUnits.toString()} waiting for a melt`, ); } for (const retired of state.retiredEpochs) { console.warn(` ${retired.message}`); } const pending = wallet.receipts.list().filter((r) => r.recourse !== undefined && r.recourse.stage !== "closed"); console.log( `receipts: ${String(wallet.receipts.length)}; bridge payments being followed up: ${String(pending.length)}; ` + `next withdraw counter ${wallet.nextCounter.toString()}`, ); } async function main(): Promise { const cfg = loadConfig(); const url = process.argv[2]; const quote = url === undefined ? undefined : await quoteFor(url, cfg); const wallet = await bootWallet(cfg); printState(wallet, quote?.priceBaseUnits ?? TYPICAL_PRICE_BASE_UNITS); } if (process.argv[1]?.endsWith("inspect.js") === true) { await main(); }