We’re building a new kind of knowledge economy, one where human expertise isn’t buried in PDFs, lost in group chats, or diluted by generic AI models. Instead, every insight, every discovery, every hard-earned piece of understanding becomes a plug-and-play module you can rent, use, and get paid for instantly. Users get AI responses powered by real experts, not generic training data. Creators earn directly from every chat. And through our Debate Arena, knowledge isn’t just uploaded, it’s proven, rated, and battle-tested. This is where human intelligence becomes a living marketplace, and every conversation has real value.
apps/api— Express API server (Hedera, Supabase, x402 payment flow)apps/web— Next.js frontend (wallet connect, marketplace, arena)apps/facilitator— x402 facilitator proxy (optional upstream)supabase/— SQL migrations and policies.env.example— environment variables template
- Prereqs: Node 18+, a Supabase project, Hedera testnet/mainnet account, HashPack WalletConnect project ID
- Setup:
- Copy
.env.example→.envand fill values - Start API:
npm run dev --workspace=apps/api - Start Facilitator (optional):
npm run dev --workspace=apps/facilitator - Start Web:
npm run dev --workspace=apps/web
- Copy
- Default ports: API
:4000, Web:3000, Facilitator:3000(override viaPORT)
See .env.example for full list.
- Supabase:
SUPABASE_URL,SUPABASE_SERVICE_ROLE_KEY— server SDKNEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY— client SDK
- Hedera & WalletConnect:
HEDERA_NETWORK—testnetormainnetNEXT_PUBLIC_HASHPACK_NETWORK— wallet network for UINEXT_PUBLIC_WC_PROJECT_ID,NEXT_PUBLIC_WC_RELAY_URL— WalletConnect v2
- Token and Treasury:
COK_TOKEN_ID— Hedera token used for paymentsCOK_TREASURY_ACCOUNT_ID,COK_TREASURY_PRIVATE_KEY— treasury as spender for approved transfersCOK_MINT_TINY_AMOUNT,COK_SUPPLY_PRIVATE_KEY— mint utility
- API/Web:
PORT(API),NEXT_PUBLIC_API_URL(Web)
- Facilitator:
FACILITATOR_URL(used by API/playground x402 flows)- Facilitator specific:
EVM_PRIVATE_KEY,SVM_PRIVATE_KEY,HEDERA_PRIVATE_KEY,HEDERA_ACCOUNT_ID,FACILITATOR_UPSTREAM
- Misc:
ADDRESS— treasury/fee payer account id used in approved transfer fallback
- Scripts:
dev,build,typecheck(apps/api/package.json:5-9) - Entry:
src/index.ts— Express + Hedera SDK + Supabase - Integrations:
- Hedera token association, direct token transfers, approved transfers
- x402 payment middleware for 402 flows
- Supabase RLS for
activitiesandrent_activities
- Custodial:
POST /custodial/create— create custodial walletPOST /custodial/associate— associate token to custodial accountPOST /custodial/ensure— ensure custodial wallet existsPOST /custodial/check— check if an account is custodial
- Knowledge Packs:
POST /knowledge-packs— createGET /knowledge-packs— list (optional?accountId=)POST /knowledge-packs/chat— chat with a packPUT /knowledge-packs/:id— updateDELETE /knowledge-packs/:id— delete
- Playground:
POST /playground/chat— chat with selected owned/rented packs
- x402 (Hedera payment helper):
POST /x402/prepare-transfer— prepare transfer bytesPOST /x402/check-allowance— check allowance for spenderPOST /x402/submit-transfer— submit signed transfer
- Marketplace:
GET /marketplace/listings— list allPOST /marketplace/listings— create listing (rent price, per‑use price)GET /marketplace/listings/:id— get listing by idGET /marketplace/listings/by-pack/:packId— get by pack idPOST /marketplace/listings/update— update listingPOST /marketplace/unlist— unlist listingGET /marketplace/rental-status?listingId&accountId— check rental activePOST /marketplace/rent— rent for minutes (on‑chain payment)POST /marketplace/chat— chat with listing’s knowledge pack
- Agents:
POST /agents— createGET /agents— list (optional?ownerAccountId=)PUT /agents/:id— updateDELETE /agents/:id— deleteGET /agents/:id/knowledge-packs— list attached packsPOST /agents/:id/knowledge-packs— add packDELETE /agents/:id/knowledge-packs/:kpId— remove pack
- Matches & Arenas:
POST /matches— create matchGET /matches,GET /matches/:idPOST /arenas— create arena (game typeimportorchallenge)GET /arenas,GET /arenas/:id,GET /arenas/code/:codeGET /arenas/watchers?accountId— list arenas watched by accountPOST /arenas/join,POST /arenas/watch,DELETE /arenas/:idPOST /arenas/select— pick agent for sidePOST /arenas/ready— mark readyPOST /arenas/submit-knowledge— submit writing in challengePOST /arenas/cancel— cancelPOST /arenas/challenge-control— start/pause/resume/finishPOST /arenas/save-draft— save draftPOST /arenas/start— start auto‑debateGET /arenas/:id/stream— SSE stream of auto‑debate
- Tokens:
POST /tokens/mint-cok— mint demoCOKtoken to account
- Leaderboard & Users:
GET /leaderboard— list top accountsPOST /users/name— update display name
- Activities:
GET /activities?accountId— list chat activities for userGET /rent-activities?accountId— list rent activity history for user
COK TOKEN = 0.0.7284519 HCS_ELO_TOPIC_ID = 0.0.7291514 HCS_KNOWLEDGE_TOPIC_ID = 0.0.7292283
- Direct Transfer (if user’s private key is available):
- Associate token, then execute a
TransferTransactionfrom renter → owner - Transaction IDs recorded to
rent_activities.transaction_ids(apps/api/src/index.ts:960-988)
- Associate token, then execute a
- x402 Header Execution:
- Accepts
X-PAYMENTheader withsignedTransaction(HashConnect flow) - Executes bytes and records transaction ID (
apps/api/src/index.ts:1030-1036)
- Accepts
- Approved Transfer Fallback:
- Uses treasury as spender to perform
addApprovedTokenTransfer - Records transaction ID (
apps/api/src/index.ts:1047-1073)
- Uses treasury as spender to perform
- Tables used (examples):
knowledge_packs,agents,arenas,users,activities,rent_activities,custodial_wallets - RLS policies:
activities— only owner (via Supabaseauth.uid()↔ custodial mapping)rent_activities—renter_account_idmatched via custodial wallets
- Migrations:
- RLS enablement and select policies in
supabase/migrations/*_rls.sql
- RLS enablement and select policies in
- Scripts:
dev,build,start,typecheck(apps/web/package.json:5-10) - Pages and Features:
/Home — connect via HashConnect or Google (custodial wallet provisioning)/marketplace— browse listings; Rent or Use; History modal with Hashscan links (apps/web/pages/marketplace.tsx)/packs— manage knowledge and agents; list/unlist marketplace/playground— multi‑source chat; per‑use payments with x402/HashConnect/arena— create/join arenas; challenge mode with timers; auto‑debate stream/profile— token balance and association helper/leaderboard— top accounts by ELO/upload— quick upload for knowledge/agents
- Wallet & Payments:
- HashConnect WalletConnect v2 for signing transactions
NEXT_PUBLIC_HASHPACK_NETWORKselectsLedgerId(mainnet/testnet)- Stores selections in
localStorage(playground_sources:<accountId>) for convenience
- Scripts:
dev,lint,format(apps/facilitator/package.json:5-17) - Entry:
index.tsGET /supported— returns supported x402 kinds (hedera testnet/mainnet with fee payer)POST /verify— proxies to upstream/verifyPOST /settle— proxies to upstream/settle
- Env:
- Provide
HEDERA_PRIVATE_KEY+HEDERA_ACCOUNT_IDfor Hedera support FACILITATOR_UPSTREAMdefault:https://x402-hedera-production.up.railway.app
- Provide
- API:
npm run dev --workspace=apps/api- Base URL:
http://localhost:4000
- Facilitator:
npm run dev --workspace=apps/facilitator- Default:
http://localhost:3000(setPORTto align with.env FACILITATOR_URL)
- Web:
npm run dev --workspace=apps/web- Base URL:
http://localhost:3000
- Custodial accounts detected server‑side; direct signing used where possible
- Non‑custodial users can:
- Approve allowance and rely on approved transfer fallback
- Or sign via HashConnect; client sets
X-PAYMENTheader in Playground
- Allowance checks are skipped for custodial accounts in frontend Playground
- Rent history shows a Hashscan link for each activity when
transaction_idsare present - Network inferred from
rent_activities.network(mainnet/testnet)
- Create Packs (Knowledge & Agents): manage knowledge and agents; attach/detach packs to agents; update or delete as needed (
POST /knowledge-packs,POST /agents,GET/PUT/DELETEvariants; web/packs). - Create Arena: open a new arena with topic and game type
importorchallenge(POST /arenasatapps/api/src/index.ts:1348-1355; web/arena). - Join Arena: join by
idorcode, then mark ready and select sides/agents (POST /arenas/joinatapps/api/src/index.ts:1451-1475,POST /arenas/readyatapps/api/src/index.ts:1522-1550,POST /arenas/selectatapps/api/src/index.ts:1509-1517; web/arena). - List to Marketplace: list a knowledge pack with per‑minute rent price and optional per‑use chat price (
POST /marketplace/listings; web/marketplace). - Rent Knowledge: pay on‑chain to rent a listing for minutes; history includes Hashscan links (
POST /marketplace/rentatapps/api/src/index.ts:872-1110; web/marketplace). - Leaderboard: see top accounts by ELO rating (
GET /leaderboardatapps/api/src/index.ts:1769-1776; web/leaderboard). - Profile: view token balance, associate token if needed, counts for Agents/Knowledge, and ELO (
apps/web/pages/profile.tsx:37-69,269-284). - Pay Per Chat/Use: per‑use billing in Playground and Marketplace chat using x402 or direct transfer (
POST /playground/chatatapps/api/src/index.ts:329-412for x402 requirement generation;POST /marketplace/chat).
- Factors (0–10 each side): argument strength; factual accuracy; direct response; coherence & structure; persuasiveness; logical fallacies (higher is fewer fallacies); rebuttal efficiency; final position (
apps/api/src/services/judge.ts:5-6). - Weights: argument_strength 0.34; factual_accuracy 0.25; direct_response 0.18; rebuttal_efficiency 0.12; persuasiveness 0.07; final_position 0.10 (
apps/api/src/services/judge.ts:17-24). - Fallacy penalty: subtract up to 0.06 based on detected fallacies per side (
apps/api/src/services/judge.ts:25-28). - Aggregation: trimmed mean across multiple judges (drop min and max when 4+ judges) (
apps/api/src/services/judge.ts:56-68). - Conclusion: concise neutral summary generated after judging (
apps/api/src/services/judge.ts:71-76). - Usage in matches/arenas: scores aggregated to pick winner and persist match (
apps/api/src/index.ts:1311-1316,apps/api/src/index.ts:1744-1749,apps/api/src/index.ts:1898-1901).
- Formula:
E = 1/(1+10^((opp - you)/400));R' = R + k*(score - E)withk=32(apps/api/src/services/elo.ts:1-7). - Inputs:
scoreis 1 for winner, 0 for loser, 0.5 for draw. - Updates occur after match creation or arena completion/streamed debate generation (
apps/api/src/index.ts:1324-1330,apps/api/src/index.ts:1756-1761,apps/api/src/index.ts:1909-1914). - Leaderboard reads user ELO from storage (
GET /leaderboardatapps/api/src/index.ts:1769-1776).
- API typecheck:
npm run typecheck --workspace=apps/api - Web typecheck:
npm run typecheck --workspace=apps/web
- Do not commit secrets. Use environment variables.
- Supabase RLS requires JWT auth in the frontend for protected queries.
- Ensure
COK_TOKEN_IDis associated with renter accounts before transfers.