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
Add skill_payment_earnings + payouts schema for creator revenue ledger
nirholas80fb32b156a1
diff --git a/api/_lib/migrations/20260618120000_skill_payment_earnings.sql b/api/_lib/migrations/20260618120000_skill_payment_earnings.sqlnew file mode 100644index 000000000..75901c26c--- /dev/null+++ b/api/_lib/migrations/20260618120000_skill_payment_earnings.sql@@ -0,0 +1,103 @@+-- Migration: skill_payment_earnings + payouts — creator revenue ledger.+-- Apply: node scripts/apply-migrations.mjs --apply --file 20260618120000_skill_payment_earnings.sql+-- Idempotent.+--+-- One earnings row is written per settled skill payment, attributing the revenue+-- to the agent's creator and splitting the gross charge into the platform fee and+-- the creator's net share. `payout_id` stays NULL until the earnings are swept+-- into a payout batch, at which point the creator is paid their accumulated net.+--+-- This is the accounting source of truth behind the creator revenue dashboard and+-- the payout pipeline. Every money column is stored in the currency's smallest+-- unit (lamports for SOL, atomics for SPL tokens) as BIGINT, never as a float.+--+-- FK mapping (this codebase vs. the generic spec):+-- payment_id → skill_purchases(id) -- canonical settled skill-payment ledger+-- agent_id → agent_identities(id) -- canonical agent table+-- creator_id → users(id) -- the agent's creator is agent_identities.user_id++begin;++-- ── payouts — a batch withdrawal of accumulated creator earnings ──────────────+-- Created before skill_payment_earnings because that table FKs into it.+create table if not exists payouts (+ id uuid primary key default gen_random_uuid(),+ creator_id uuid not null references users(id),+ amount bigint not null check (amount > 0),+ currency_mint text not null,+ status text not null default 'pending'+ check (status in ('pending', 'processing', 'completed', 'failed')),+ destination_address text not null,+ tx_signature text,+ failure_reason text,+ created_at timestamptz not null default now(),+ updated_at timestamptz not null default now(),+ completed_at timestamptz+);++-- A settled payout has exactly one on-chain signature. Partial-unique so many+-- pending/failed payouts (null signature) coexist while a signature never repeats.+create unique index if not exists payouts_tx_signature_unique+ on payouts (tx_signature) where tx_signature is not null;++-- Creator dashboard: a creator's payout history, newest first.+create index if not exists idx_payouts_creator_status+ on payouts (creator_id, status, created_at desc);++-- Payout worker: pick up the open (pending/processing) batches to settle.+create index if not exists idx_payouts_status+ on payouts (status, created_at desc)+ where status in ('pending', 'processing');++do $$ begin+ create trigger payouts_set_updated_at before update on payouts+ for each row execute function set_updated_at();+exception when duplicate_object then null; end $$;++-- ── skill_payment_earnings — per-sale creator revenue ledger ──────────────────+create table if not exists skill_payment_earnings (+ id uuid primary key default gen_random_uuid(),+ -- Exactly one earnings row per payment; cascade so a deleted payment can't+ -- leave an orphaned earnings record behind.+ payment_id uuid not null unique+ references skill_purchases(id) on delete cascade,+ agent_id uuid not null references agent_identities(id) on delete cascade,+ creator_id uuid not null references users(id),++ gross_amount bigint not null check (gross_amount >= 0),+ platform_fee_bps integer not null check (platform_fee_bps between 0 and 10000),+ platform_fee_amount bigint not null check (platform_fee_amount >= 0),+ net_amount bigint not null check (net_amount >= 0),++ currency_mint text not null,++ -- NULL until the earnings are swept into a payout batch. ON DELETE SET NULL so+ -- voiding a payout returns its earnings to the unpaid pool rather than deleting+ -- the financial record.+ payout_id uuid references payouts(id) on delete set null,++ created_at timestamptz not null default now(),++ -- The split must reconcile exactly: gross = platform fee + creator net.+ constraint skill_payment_earnings_amounts_balance+ check (gross_amount = platform_fee_amount + net_amount)+);++-- Creator dashboard: list a creator's earnings newest-first.+create index if not exists idx_skill_earnings_creator_id+ on skill_payment_earnings (creator_id, created_at desc);++-- Find every earnings row included in a given payout batch.+create index if not exists idx_skill_earnings_payout_id+ on skill_payment_earnings (payout_id) where payout_id is not null;++-- Hot path for payouts: a creator's unpaid (available-to-withdraw) balance.+create index if not exists idx_skill_earnings_unpaid+ on skill_payment_earnings (creator_id, created_at)+ where payout_id is null;++-- Per-agent revenue analytics.+create index if not exists idx_skill_earnings_agent_id+ on skill_payment_earnings (agent_id, created_at desc);++commit;diff --git a/prompts/monetization/18-db-schema-earnings.md b/prompts/monetization/18-db-schema-earnings.mddeleted file mode 100644index 0620d1c09..000000000--- a/prompts/monetization/18-db-schema-earnings.md+++ /dev/null@@ -1,89 +0,0 @@-# Prompt 18: DB Schema for Earnings and Platform Fees--## Objective-Design and create the database table schema for `skill_payment_earnings` to reliably track revenue for creators and calculate platform fees.--## Explanation-When a payment is successfully completed, the revenue needs to be allocated. This table will store a record of each sale, attributing the earnings to the correct creator, and clearly separating the gross amount, the platform's cut, and the net amount due to the creator. This is fundamental for accurate accounting and payouts.--## Instructions-1. **Define the Table Schema:**- * Create a new table named `skill_payment_earnings`.- * It should include the following columns:- * `id`: Primary key (UUID or auto-incrementing integer).- * `payment_id`: A unique foreign key referencing `skill_payments(id)`. This ensures each payment has only one corresponding earnings entry.- * `agent_id`: Foreign key to `agents(id)`.- * `creator_id`: Foreign key to `users(id)`, identifying the user who gets the earnings.- * `gross_amount`: The total amount paid by the user (in lamports or smallest unit).- * `platform_fee_bps`: The platform fee basis points at the time of the transaction (e.g., `500` for 5%).- * `platform_fee_amount`: The calculated platform fee (`gross_amount * platform_fee_bps / 10000`).- * `net_amount`: The amount due to the creator (`gross_amount - platform_fee_amount`).- * `currency_mint`: The mint address of the currency used.- * `payout_id`: A nullable foreign key that will link to a `payouts` table when the earnings are withdrawn.- * `created_at`: Timestamp.--2. **Create Indexes:**- * Create an index on `creator_id` to quickly query for all earnings belonging to a specific creator for their dashboard.- * Create an index on `payout_id` to find all earnings included in a specific payout batch.--3. **Write the SQL Migration:**- * Create a new SQL migration file to apply this schema change to your database.--## SQL Schema Example--```sql-CREATE TABLE skill_payment_earnings (- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),- payment_id UUID NOT NULL UNIQUE REFERENCES skill_payments(id) ON DELETE CASCADE,- agent_id UUID NOT NULL REFERENCES agents(id),- creator_id UUID NOT NULL REFERENCES users(id),-- gross_amount BIGINT NOT NULL,- platform_fee_bps INTEGER NOT NULL, -- e.g., 500 for 5.00%- platform_fee_amount BIGINT NOT NULL,- net_amount BIGINT NOT NULL,-- currency_mint VARCHAR(255) NOT NULL,-- -- This will be NULL until the earnings are included in a payout- payout_id UUID REFERENCES payouts(id),-- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),-- CONSTRAINT check_amounts CHECK (gross_amount = platform_fee_amount + net_amount)-);---- Index for creator dashboard queries-CREATE INDEX idx_skill_earnings_creator_id ON skill_payment_earnings(creator_id);---- Index for finding earnings in a payout-CREATE INDEX idx_skill_earnings_payout_id ON skill_payment_earnings(payout_id);----- You will also need a payouts table for later prompts-CREATE TABLE payouts (- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),- creator_id UUID NOT NULL REFERENCES users(id),- amount BIGINT NOT NULL,- currency_mint VARCHAR(255) NOT NULL,- status VARCHAR(50) NOT NULL DEFAULT 'pending', -- pending, completed, failed- destination_address VARCHAR(255) NOT NULL,- tx_signature VARCHAR(255),- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),- completed_at TIMESTAMPTZ-);-```--<!-- AUTO:self-delete-on-complete -->-------## ✅ On completion — delete this file--This file is a unit of work, not a permanent doc. The moment every item above is **built, wired, verified, and committed** to the "Definition of done" in the repo-root `CLAUDE.md`, remove it in the same change:--```bash-git rm "prompts/monetization/18-db-schema-earnings.md"-```--Stage the deletion alongside your implementation and include it in the completion commit. This directory is the backlog: a file that still exists is unfinished work; a file that is gone has shipped. Do not delete early, and never leave a completed prompt behind.