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(payouts): stop x402 agent API sales counting as treasury-withdrawable and route Solana payTo to a Solana address
nirholas4fab73f36250
diff --git a/api/_lib/migrations/20260930141000_agent_revenue_events_settled_to_wallet.sql b/api/_lib/migrations/20260930141000_agent_revenue_events_settled_to_wallet.sqlnew file mode 100644index 000000000..b1bc343c1--- /dev/null+++ b/api/_lib/migrations/20260930141000_agent_revenue_events_settled_to_wallet.sql@@ -0,0 +1,19 @@+-- Migration: mark revenue that settled straight into the agent's own wallet.+-- Apply: node scripts/apply-migrations.mjs --apply --file 20260930141000_agent_revenue_events_settled_to_wallet.sql+-- Idempotent.+--+-- agent_revenue_events doubles as the withdrawable-balance ledger: withdrawals+-- pay an owner out of the platform treasury up to sum(net_amount). A sale of an+-- agent as a paid x402 API (POST /api/x402/agents/:id) settles the buyer's USDC+-- on chain directly to the agent's payout wallet, so the owner already holds+-- that money. Counting it as withdrawable would let the treasury pay it a+-- second time. The row is still real income (the Earn tab, the economy summary+-- and every earnings view read it), so it is kept and flagged instead:+-- settled_to_wallet = true means "already in the owner's wallet, never a+-- treasury liability". Every existing row keeps the old meaning (false).++alter table agent_revenue_events+ add column if not exists settled_to_wallet boolean not null default false;++create index if not exists agent_revenue_events_agent_skill_created+ on agent_revenue_events (agent_id, skill, created_at desc);diff --git a/api/_lib/monetization.js b/api/_lib/monetization.jsindex c0811f4b8..dc8faf407 100644--- a/api/_lib/monetization.js+++ b/api/_lib/monetization.js@@ -79,6 +79,8 @@ export async function recordRevenueEvent({* Calculate the available (withdrawable) balance for a user.** available = sum(net_amount from revenue_events) - sum(amount from pending/processing/completed withdrawals)+ * Revenue flagged settled_to_wallet already landed in the owner's own wallet on+ * chain (agent API sales over x402), so it is never withdrawable from the treasury.** @param {string} userId* @param {string} [currencyMint] - filter to a specific currency (default: all)@@ -93,6 +95,7 @@ export async function getAvailableBalance(userId, currencyMint = null) {JOIN agent_identities ai ON ai.id = re.agent_idWHERE coalesce(re.owner_user_id, ai.user_id) = ${userId}AND re.currency_mint = ${currencyMint}+ AND NOT re.settled_to_wallet`: await sql`SELECT@@ -100,6 +103,7 @@ export async function getAvailableBalance(userId, currencyMint = null) {FROM agent_revenue_events reJOIN agent_identities ai ON ai.id = re.agent_idWHERE coalesce(re.owner_user_id, ai.user_id) = ${userId}+ AND NOT re.settled_to_wallet`;const [wResult] = currencyMintdiff --git a/api/_lib/payout.js b/api/_lib/payout.jsindex b438e5b97..86d6efbb3 100644--- a/api/_lib/payout.js+++ b/api/_lib/payout.js@@ -1,5 +1,22 @@import { sql } from './db.js';+const EVM_ADDRESS = /^0x[0-9a-fA-F]{40}$/;++// The agent's own wallet on `chain`, used when the owner has not configured a+// payout wallet. agent_identities.wallet_address is the agent's EVM wallet on+// most rows (the Solana one lives in meta.solana_address, provisioned with the+// custodial wallet), so returning it unconditionally handed a 0x address to a+// Solana payTo. Each chain family only ever gets an address of its own shape.+export function ownWalletFor(chain, row) {+ if (!row) return null;+ const wallet = row.wallet_address || null;+ if (chain === 'solana') {+ if (row.solana_address) return row.solana_address;+ return wallet && !wallet.startsWith('0x') ? wallet : null;+ }+ return wallet && EVM_ADDRESS.test(wallet) ? wallet : null;+}+export async function resolvePayoutAddress(agentId, chain) {const [wallet] = await sql`select address from agent_payout_wallets@@ -10,7 +27,8 @@ export async function resolvePayoutAddress(agentId, chain) {if (wallet) return wallet.address;const [agent] = await sql`- select wallet_address from agent_identities where id = ${agentId}+ select wallet_address, meta->>'solana_address' as solana_address+ from agent_identities where id = ${agentId}`;- return agent?.wallet_address ?? null;+ return ownWalletFor(chain, agent);}diff --git a/api/billing/withdrawals/index.js b/api/billing/withdrawals/index.jsindex d918bda36..80040d2fb 100644--- a/api/billing/withdrawals/index.js+++ b/api/billing/withdrawals/index.js@@ -157,6 +157,7 @@ export default wrap(async (req, res) => {where coalesce(re.owner_user_id, ai.user_id) = ${user.id}and re.currency_mint = ${currency_mint}and ls.id is null+ and not re.settled_to_wallet) + (select coalesce(sum(sd.amount), 0)::bigintfrom split_distributions sddiff --git a/api/cron/[name].js b/api/cron/[name].jsindex 35d16f473..7c26e8587 100644--- a/api/cron/[name].js+++ b/api/cron/[name].js@@ -4331,6 +4331,7 @@ async function handleProcessWithdrawals(req, res) {FROM agent_revenue_events reJOIN agent_identities ai ON ai.id = re.agent_idWHERE coalesce(re.owner_user_id, ai.user_id) = w.user_id AND re.currency_mint = w.currency_mint+ AND NOT re.settled_to_wallet) -(SELECT coalesce(sum(w2.amount), 0)::bigintdiff --git a/api/monetization/withdrawals.js b/api/monetization/withdrawals.jsindex e7f7a153c..a83c93a00 100644--- a/api/monetization/withdrawals.js+++ b/api/monetization/withdrawals.js@@ -249,6 +249,7 @@ export default wrap(async (req, res) => {JOIN agent_identities ai ON ai.id = re.agent_idWHERE coalesce(re.owner_user_id, ai.user_id) = ${userId}AND re.currency_mint = ${currencyMint}+ AND NOT re.settled_to_wallet) - (SELECT COALESCE(SUM(w2.amount), 0)::bigintFROM agent_withdrawals w2diff --git a/tests/api/x402.test.js b/tests/api/x402.test.jsindex e284682fe..5faa4cb5a 100644--- a/tests/api/x402.test.js+++ b/tests/api/x402.test.js@@ -18,7 +18,7 @@ vi.mock('../../api/_lib/db.js', () => ({if (q.includes('agent_payout_wallets')) {return walletState.payout ? [{ address: walletState.payout }] : [];}- if (q.includes('wallet_address from agent_identities')) {+ if (q.includes('from agent_identities')) {return walletState.agentWallet ? [{ wallet_address: walletState.agentWallet }] : [];}return [];