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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(x402): restore the fresh-workers balance reader fix a concurrent commit reverted
nirholas80a82336d3be
diff --git a/api/_lib/x402/fresh-workers/chain.js b/api/_lib/x402/fresh-workers/chain.jsindex 332b27432..f1f69fc02 100644--- a/api/_lib/x402/fresh-workers/chain.js+++ b/api/_lib/x402/fresh-workers/chain.js@@ -74,18 +74,22 @@ async function sendIxs(conn, feePayer, signers, instructions, { confirmMs = 30_0}/** SOL balance in lamports (0 for a never-seen account), or null when unreadable. */+// readAccountInfoOrNull({ withCause: true }) always resolves to { info, cause }:+// info === null with no cause is an account that does not exist, a cause is an+// RPC transport failure. Reading the wrapper itself as the account reported 0+// lamports for every wallet and kept the lane on funder_headroom forever.export async function readSolLamports(conn, pubkey) {- const info = await readAccountInfoOrNull(conn, pubkey, { commitment: 'confirmed', withCause: true });- if (info === null) return 0;- if (info === undefined) return null;+ const { info, cause } = await readAccountInfoOrNull(conn, pubkey, { commitment: 'confirmed', withCause: true });+ if (cause) return null;+ if (!info) return 0;return Number(info.lamports || 0);}/** USDC held by an owner's ATA: { exists, atomic } or null when unreadable. */export async function readUsdc(conn, ata) {- const info = await readAccountInfoOrNull(conn, ata, { commitment: 'confirmed', withCause: true });- if (info === null) return { exists: false, atomic: 0n };- if (info === undefined) return null;+ const { info, cause } = await readAccountInfoOrNull(conn, ata, { commitment: 'confirmed', withCause: true });+ if (cause) return null;+ if (!info) return { exists: false, atomic: 0n };return { exists: true, atomic: tokenAmountFromAccountData(info.data) };}diff --git a/tests/x402-fresh-workers-chain.test.js b/tests/x402-fresh-workers-chain.test.jsnew file mode 100644index 000000000..df1c55aff--- /dev/null+++ b/tests/x402-fresh-workers-chain.test.js@@ -0,0 +1,57 @@+// Balance readers of the fresh-wallet workers lane+// (api/_lib/x402/fresh-workers/chain.js). These two reads gate every tick: the+// SOL funder's headroom over the sponsor floor and the treasury's USDC. Both+// once read the { info, cause } wrapper returned by readAccountInfoOrNull as if+// it were the account, so every balance came back 0 and the lane skipped with+// funder_headroom on every tick from launch, never minting a single wallet.+//+// What these lock down, for both readers:+// • a funded account reports its real balance.+// • an account that does not exist reads as empty (0 / exists:false).+// • an RPC transport failure reads as unknown (null), never as zero.++import { describe, it, expect, vi } from 'vitest';++import { readSolLamports, readUsdc } from '../api/_lib/x402/fresh-workers/chain.js';++const PUBKEY = 'WwwuGbqHrwF5RG89KhUbmRWEvjnRH9k5kVM5p7T3WwW';++const connReturning = (value) => ({ getAccountInfo: vi.fn(async () => value) });+const connFailing = () => ({+ getAccountInfo: vi.fn(async () => { throw Object.assign(new Error('fetch failed'), { name: 'TypeError' }); }),+});++function tokenAccountData(amount) {+ const data = Buffer.alloc(165);+ data.writeBigUInt64LE(BigInt(amount), 64);+ return data;+}++describe('readSolLamports', () => {+ it('returns the live lamports of a funded account', async () => {+ expect(await readSolLamports(connReturning({ lamports: 651_828, data: Buffer.alloc(0) }), PUBKEY)).toBe(651_828);+ });++ it('returns 0 for an account that does not exist', async () => {+ expect(await readSolLamports(connReturning(null), PUBKEY)).toBe(0);+ });++ it('returns null, not 0, when the RPC is unreachable', async () => {+ expect(await readSolLamports(connFailing(), PUBKEY)).toBeNull();+ });+});++describe('readUsdc', () => {+ it('returns the atomic amount held by an existing token account', async () => {+ expect(await readUsdc(connReturning({ lamports: 2_039_280, data: tokenAccountData(2_587_000) }), PUBKEY))+ .toEqual({ exists: true, atomic: 2_587_000n });+ });++ it('reports a missing token account as not existing', async () => {+ expect(await readUsdc(connReturning(null), PUBKEY)).toEqual({ exists: false, atomic: 0n });+ });++ it('returns null when the RPC is unreachable', async () => {+ expect(await readUsdc(connFailing(), PUBKEY)).toBeNull();+ });+});