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(economy): never plan opposing swaps on one wallet in a single rebalance run
nirholasf17726d8fdd8
diff --git a/api/_lib/economy-rebalance.js b/api/_lib/economy-rebalance.jsindex 778f17fa0..a58570604 100644--- a/api/_lib/economy-rebalance.js+++ b/api/_lib/economy-rebalance.js@@ -98,7 +98,21 @@ export function planRebalance({ solPriceUsd, wallets, bounds }) {}).sort((a, b) => b.shortfallUsd - a.shortfallUsd);+ // A self-pay wallet below BOTH floors submits two rows for the same pubkey+ // wanting opposite assets. Planning both would execute opposing swaps in one+ // run: two fees plus double slippage to mostly undo each other (observed+ // 2026-07-28 on the ring payer: usdc->sol $6.95 then sol->usdc $2.04, netting+ // the wallet AWAY from its USDC floor). The neediest leg wins the run; the+ // opposing leg waits for the next run against fresh balances.+ const plannedDir = new Map();+for (const { w, shortfallUsd } of rows) {+ const wantDir = w.wants === 'usdc' ? 'sol->usdc' : 'usdc->sol';+ const prior = plannedDir.get(w.pubkey);+ if (prior && prior !== wantDir) {+ skipped.push({ name: w.name, reason: 'opposing_leg_same_run' });+ continue;+ }if (runRemainingUsd <= 0) {skipped.push({ name: w.name, reason: 'run_cap_reached' });continue;@@ -122,6 +136,7 @@ export function planRebalance({ solPriceUsd, wallets, bounds }) {inUsd: round(inUsd),reason: `usdc ${w.usdc.toFixed(2)} < floor ${w.floorUsd}`,});+ plannedDir.set(w.pubkey, 'sol->usdc');runRemainingUsd = round(runRemainingUsd - inUsd);} else {// Convert USDC → SOL, keeping the USDC reserve untouched.@@ -138,6 +153,7 @@ export function planRebalance({ solPriceUsd, wallets, bounds }) {inUsd: round(inUsd),reason: `sol ${(w.sol * solPriceUsd).toFixed(2)}USD < floor ${w.floorUsd}`,});+ plannedDir.set(w.pubkey, 'usdc->sol');runRemainingUsd = round(runRemainingUsd - inUsd);}}diff --git a/tests/economy-rebalance.test.js b/tests/economy-rebalance.test.jsindex 8cc970999..3015be0e4 100644--- a/tests/economy-rebalance.test.js+++ b/tests/economy-rebalance.test.js@@ -69,6 +69,39 @@ describe('planRebalance', () => {expect(plan[0].inUsd).toBe(3); // per-swap cap});+ it('never plans opposing swaps on the same wallet in one run', () => {+ // The 2026-07-28 churn: the ring payer sat below BOTH floors, so the cron+ // submitted a wants:'usdc' row and a wants:'sol' row for the same pubkey.+ // The planner executed both directions back to back, paying two swap fees+ // to mostly undo itself and ending FURTHER from the USDC floor. The+ // neediest leg must win the run; the opposing leg is deferred.+ const { plan, skipped } = planRebalance({+ solPriceUsd: SOL,+ bounds,+ wallets: [+ { name: 'ring', pubkey: 'R', sol: 0.05, usdc: 9.9, wants: 'usdc', floorUsd: 12 },+ { name: 'ring', pubkey: 'R', sol: 0.05, usdc: 9.9, wants: 'sol', floorUsd: 15 },+ ],+ });+ expect(plan).toHaveLength(1);+ expect(plan[0].dir).toBe('usdc->sol'); // SOL shortfall ($7.50) beats USDC ($2.10)+ expect(skipped).toContainEqual({ name: 'ring', reason: 'opposing_leg_same_run' });+ });++ it('still serves same-direction rows on one wallet without deferral', () => {+ // Two rows, same pubkey, both wanting USDC: no opposition, both plan.+ const { plan, skipped } = planRebalance({+ solPriceUsd: SOL,+ bounds,+ wallets: [+ { name: 'ring', pubkey: 'R', sol: 1, usdc: 0, wants: 'usdc', floorUsd: 10 },+ { name: 'ring', pubkey: 'R', sol: 1, usdc: 0, wants: 'usdc', floorUsd: 4 },+ ],+ });+ expect(plan).toHaveLength(2);+ expect(skipped.find((s) => s.reason === 'opposing_leg_same_run')).toBeUndefined();+ });+it('aborts everything if the SOL price is unavailable', () => {const { plan, skipped } = planRebalance({solPriceUsd: 0,