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.

fix(x402-pay): let the buyer pay its own Solana fee when the sponsor is under its floor instead of answering 500

nirholas2287de6eafd4
diff --git a/api/x402-pay.js b/api/x402-pay.js
index f9343f3cd..ff81cdbef 100644
--- a/api/x402-pay.js
+++ b/api/x402-pay.js
@@ -345,10 +345,13 @@ function buildJsonRpc(tool, args) {
};
}
-async function buildSolanaPaymentPayload({ accept, buyer, conn, resourceUrl }) {
+export async function buildSolanaPaymentPayload({ accept, buyer, conn, resourceUrl }) {
const mint = new PublicKey(accept.asset);
const payTo = new PublicKey(accept.payTo);
- const feePayer = new PublicKey(accept.extra.feePayer);
+ // An accept with no feePayer is the x402 self-pay contract (x402-spec.js drops
+ // it while the sponsor sits under its SOL floor): the buyer pays its own fee,
+ // as in x402-checkout.js. Only the self-routed facilitator advertises it.
+ const feePayer = accept.extra?.feePayer ? new PublicKey(accept.extra.feePayer) : buyer.publicKey;
const amount = BigInt(accept.amount);
const senderAta = getAssociatedTokenAddressSync(
diff --git a/tests/x402-pay-self-pay.test.js b/tests/x402-pay-self-pay.test.js
new file mode 100644
index 000000000..69ecfae45
--- /dev/null
+++ b/tests/x402-pay-self-pay.test.js
@@ -0,0 +1,52 @@
+import { describe, it, expect } from 'vitest';
+import { Keypair, VersionedTransaction } from '@solana/web3.js';
+
+// While the sponsor wallet sits under its SOL floor, x402-spec.js advertises the
+// Solana accept WITHOUT extra.feePayer (the self-pay contract). The in-house
+// payer used to read accept.extra.feePayer unconditionally, so `new PublicKey(
+// undefined)` threw and every seeder/showcase purchase answered 500 flow_failed.
+// These tests decode the signed transaction and assert who pays the fee.
+
+const USDC = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
+process.env.X402_ASSET_MINT_SOLANA = USDC;
+
+const { buildSolanaPaymentPayload } = await import('../api/x402-pay.js');
+
+const buyer = Keypair.generate();
+const conn = {
+ getAccountInfo: async () => ({ lamports: 1 }),
+ getLatestBlockhash: async () => ({ blockhash: Keypair.generate().publicKey.toBase58(), lastValidBlockHeight: 1 }),
+};
+
+function accept(extra) {
+ return {
+ scheme: 'exact',
+ network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
+ asset: USDC,
+ payTo: Keypair.generate().publicKey.toBase58(),
+ amount: '1000',
+ extra,
+ };
+}
+
+function signedPayer(payload) {
+ const vtx = VersionedTransaction.deserialize(Buffer.from(payload.payload.transaction, 'base64'));
+ return vtx.message.staticAccountKeys[0].toBase58();
+}
+
+describe('buildSolanaPaymentPayload fee payer', () => {
+ it('makes the buyer its own fee payer when the accept advertises none (self-pay)', async () => {
+ const payload = await buildSolanaPaymentPayload({
+ accept: accept({ name: 'USDC', decimals: 6 }), buyer, conn, resourceUrl: 'https://three.ws/api/x402/crypto-intel',
+ });
+ expect(signedPayer(payload)).toBe(buyer.publicKey.toBase58());
+ });
+
+ it('keeps the advertised sponsor as fee payer when there is one', async () => {
+ const sponsor = Keypair.generate().publicKey.toBase58();
+ const payload = await buildSolanaPaymentPayload({
+ accept: accept({ name: 'USDC', decimals: 6, feePayer: sponsor }), buyer, conn, resourceUrl: 'https://three.ws/api/x402/crypto-intel',
+ });
+ expect(signedPayer(payload)).toBe(sponsor);
+ });
+});