/** * What a payment costs: the price, plus the facilitator's fee, rounded up to * a whole smallest coin. Pure arithmetic, the same the wallet does. */ import { feeCeiling, presentedAmount, PX402FeeError } from "px402"; /** A $0.003 call at 100 bps on the launch ladder, whose smallest coin is $0.001. */ export function threeTenthsOfACent(): void { const { price, fee, presented } = presentedAmount(3_000n, 100, 1_000n); // price 3000, fee 1000, presented 4000. The rate owes ceil(3000 × 100 / 10 000) = 30; // 3030 rounds up to 4000, and the 970 of rounding is part of the relayer's fee. console.log(price, fee, presented); // The most a wallet with maxFeeBps 100 will ever pay in fee on this price: 30 + 1000 - 1. console.log(feeCeiling(3_000n, 100, 1_000n)); // 1029n } /** What a fee refusal tells you. Nothing was presented and no coin was retired. */ export function explainFeeRefusal(error: PX402FeeError): string { switch (error.refusal) { case "fee-over-max": return `${error.facilitator} charges ${String(error.feeBps)} bps (${String(error.feeBaseUnits)} base units here); your maxFeeBps is ${String(error.maxFeeBps)}`; case "unreachable": return `${error.facilitator}/supported did not answer; retry later`; default: return `${error.facilitator} publishes a fee this wallet cannot use (${error.refusal})`; } }