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(splits): stop double-crediting a seller net the buyer already paid to the payout wallet by counting only accrued ledger rows in balances and withdrawals

nirholas320d4e73bc5d
diff --git a/api/_lib/monetization.js b/api/_lib/monetization.js
index dc8faf407..ad7cdbdd5 100644
--- a/api/_lib/monetization.js
+++ b/api/_lib/monetization.js
@@ -152,12 +152,12 @@ export async function getAvailableBalance(userId, currencyMint = null) {
? await sql`
SELECT COALESCE(SUM(amount), 0)::bigint AS amt
FROM split_distributions
- WHERE recipient_user_id = ${userId} AND mode = 'ledger' AND currency_mint = ${currencyMint}
+ WHERE recipient_user_id = ${userId} AND mode = 'ledger' AND status = 'accrued' AND currency_mint = ${currencyMint}
`
: await sql`
SELECT COALESCE(SUM(amount), 0)::bigint AS amt
FROM split_distributions
- WHERE recipient_user_id = ${userId} AND mode = 'ledger'
+ WHERE recipient_user_id = ${userId} AND mode = 'ledger' AND status = 'accrued'
`;
splitOut = Number(outRow.amt);
splitIn = Number(inRow.amt);
diff --git a/api/_lib/purchase-confirm.js b/api/_lib/purchase-confirm.js
index b26525300..c611ee5c3 100644
--- a/api/_lib/purchase-confirm.js
+++ b/api/_lib/purchase-confirm.js
@@ -598,6 +598,7 @@ async function finalizeSkillConfirmation(pur, txSignature, payoutAddress, platfo
netAtomics: netAmt,
currencyMint: pur.currency_mint,
chain: pur.chain,
+ settledToWallet: true,
});
await logEvent(pur.id, 'split_recorded', {
split_id: split.id,
diff --git a/api/_lib/splits.js b/api/_lib/splits.js
index cb37bad70..9dbc84172 100644
--- a/api/_lib/splits.js
+++ b/api/_lib/splits.js
@@ -298,12 +298,15 @@ export async function clearListingSplit(sql, agentId, skill) {
* @param {bigint|string} o.netAtomics - the creator's net (after platform fee)
* @param {string} o.currencyMint
* @param {string} o.chain
+ * @param {boolean} [o.settledToWallet] - the buyer paid the seller's payout wallet directly
* @returns {Promise<Array<{address, recipient_user_id, share_bps, amount:string, mode, status}>>}
*/
-export async function recordSplitDistribution(sql, { purchaseId, split, netAtomics, currencyMint, chain }) {
+export async function recordSplitDistribution(sql, { purchaseId, split, netAtomics, currencyMint, chain, settledToWallet = false }) {
const allocations = allocateAtomics(netAtomics, split.recipients);
const mode = split.split_mode === 'onchain' ? 'onchain' : 'ledger';
- const status = mode === 'onchain' ? 'settled' : 'accrued';
+ // A net the buyer already paid to the seller's wallet is informational: crediting
+ // it again would let the treasury pay the same money out a second time.
+ const status = mode === 'onchain' || settledToWallet ? 'settled' : 'accrued';
const out = [];
for (const a of allocations) {
diff --git a/api/billing/withdrawals/index.js b/api/billing/withdrawals/index.js
index a27ce54f9..462c84b23 100644
--- a/api/billing/withdrawals/index.js
+++ b/api/billing/withdrawals/index.js
@@ -174,6 +174,7 @@ export default wrap(async (req, res) => {
from split_distributions sd
where sd.recipient_user_id = ${user.id}
and sd.mode = 'ledger'
+ and sd.status = 'accrued'
and sd.currency_mint = ${currency_mint}
) - (
select coalesce(sum(w2.amount), 0)::bigint