Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

test(x402): cover the settle recovery probe and repair the stale fee-meter pin

nirholas60a0329871f9
diff --git a/tests/x402-self-facilitator-settle-recovery.test.js b/tests/x402-self-facilitator-settle-recovery.test.js
index 5f8ac1e6e..b09c78213 100644
--- a/tests/x402-self-facilitator-settle-recovery.test.js
+++ b/tests/x402-self-facilitator-settle-recovery.test.js
@@ -98,6 +98,86 @@ describe('settleRingPayment already-landed recovery', () => {
expect(historySearched).toBe(true); // must search history so an aged-out landing is still found
});
+ it('recovers success when the RPC reply was unparseable and the signature landed', async () => {
+ // A fallback endpoint answering in a non-standard shape makes web3.js throw a
+ // superstruct union error instead of anything mentioning simulation. That
+ // message never matched the old probe condition, so the settle was reported
+ // failed WITHOUT ever asking the chain — 502ing a payment that had settled.
+ const p = buildSelfPay();
+ process.env.X402_PAY_TO_SOLANA = p.payTo;
+ let probed = false;
+ const conn = {
+ getBalance: async () => 1_000_000_000,
+ sendRawTransaction: async () => {
+ throw new Error('Expected the value to satisfy a union of `type | type`, but received: [object Object]');
+ },
+ getSignatureStatuses: async () => {
+ probed = true;
+ return { value: [{ err: null, confirmationStatus: 'confirmed' }] };
+ },
+ };
+ const res = await settleRingPayment({
+ paymentPayload: p.paymentPayload,
+ requirement: p.requirement,
+ conn,
+ });
+ expect(probed).toBe(true);
+ expect(res.success).toBe(true);
+ expect(res.replayed).toBe(true);
+ expect(res.transaction).toBe(p.sig);
+ });
+
+ it('skips the probe for a blockhash rejection, which proves the tx never landed', async () => {
+ const p = buildSelfPay();
+ process.env.X402_PAY_TO_SOLANA = p.payTo;
+ let probed = false;
+ const conn = {
+ getBalance: async () => 1_000_000_000,
+ sendRawTransaction: async () => {
+ throw new Error('Simulation failed. Message: Transaction simulation failed: Blockhash not found. Logs: [].');
+ },
+ getSignatureStatuses: async () => {
+ probed = true;
+ return { value: [null] };
+ },
+ simulateTransaction: async () => ({ value: { err: 'BlockhashNotFound', logs: [] } }),
+ };
+ const res = await settleRingPayment({
+ paymentPayload: p.paymentPayload,
+ requirement: p.requirement,
+ conn,
+ });
+ expect(probed).toBe(false); // no wasted round trip on a provably-never-landed tx
+ expect(res.success).toBe(false);
+ });
+
+ it('names the structured cause that web3.js drops from an empty-log preflight failure', async () => {
+ // "Transaction simulation failed. Logs: []" reached production logs with no
+ // cause at all (63 rows in 3 h, none diagnosable). The cause lives in
+ // res.error.data.err, which web3.js discards — re-simulating on the failure
+ // path is the only way to recover it.
+ const p = buildSelfPay();
+ process.env.X402_PAY_TO_SOLANA = p.payTo;
+ const conn = {
+ getBalance: async () => 1_000_000_000,
+ sendRawTransaction: async () => {
+ throw new Error('Simulation failed. Message: Transaction simulation failed. Logs: [].');
+ },
+ getSignatureStatuses: async () => ({ value: [null] }),
+ simulateTransaction: async () => ({
+ value: { err: { InsufficientFundsForRent: { account_index: 1 } }, logs: [] },
+ }),
+ };
+ const res = await settleRingPayment({
+ paymentPayload: p.paymentPayload,
+ requirement: p.requirement,
+ conn,
+ });
+ expect(res.success).toBe(false);
+ expect(res.reason).toMatch(/^broadcast_failed:/);
+ expect(res.reason).toContain('InsufficientFundsForRent');
+ });
+
it('still fails closed when the signature did NOT land (a genuine simulation fault)', async () => {
const p = buildSelfPay();
process.env.X402_PAY_TO_SOLANA = p.payTo;
diff --git a/tests/x402-wallet-fee-meter.test.js b/tests/x402-wallet-fee-meter.test.js
index 7a166b7a2..51f9c6fc1 100644
--- a/tests/x402-wallet-fee-meter.test.js
+++ b/tests/x402-wallet-fee-meter.test.js
@@ -379,8 +379,8 @@ describe('refusal alerting', () => {
});
});
-describe('known accounting gap: recordSettledFee only fires on success', () => {
- // api/x402-facilitator/[action].js line "if (result.success) recordSettledFee
+describe('known accounting gap: recordSettledFee only fires on a granted credit', () => {
+ // api/x402-facilitator/[action].js line "if (credit.granted) recordSettledFee
// (result.feePayer, result.feeLamports)" only debits the in-process cache for
// SUCCESSFUL settles. A settle that broadcasts but comes back non-success
// (e.g. not_confirmed after the tx landed with an on-chain error) can still
@@ -405,13 +405,21 @@ describe('known accounting gap: recordSettledFee only fires on success', () => {
// plus however many failed-but-burned settles occur. The hard SOL floor
// in the settle path remains the real protection.
- it('the handler source pins the success-only guard', () => {
+ // The guard moved from `result.success` to `credit.granted` when the settle-credit
+ // claim landed (one payment per signature). That is strictly narrower: the handler
+ // returns early on !result.success, so a granted credit still implies a successful
+ // settle, and an idempotent replay of an already-credited payment no longer
+ // double-debits the wallet's daily runway. Both halves are pinned below, because
+ // the gap documented above only stays bounded while the guard stays this narrow.
+ it('the handler source pins the granted-credit guard', () => {
const here = path.dirname(fileURLToPath(import.meta.url));
const src = readFileSync(
path.join(here, '..', 'api', 'x402-facilitator', '[action].js'),
'utf8',
);
- expect(src).toMatch(/if \(result\.success\) recordSettledFee\(result\.feePayer, result\.feeLamports\);/);
+ expect(src).toMatch(/if \(credit\.granted\) recordSettledFee\(result\.feePayer, result\.feeLamports\);/);
+ // A failed settle must still return before any fee is metered.
+ expect(src).toMatch(/if \(!result\.success\)[\s\S]{0,600}?return json\(res, 200, \{ success: false/);
});
it('a failed settle that burned fees leaves the cached budget undebited (CURRENT behavior)', async () => {