[Exchange] Implement token ledger with double-entry accounting done coding:7 reasoning:6

← Capital Markets
Foundation: token_ledger + token_accounts tables, transfer/mint/burn functions, balance enforcement, blockchain-compatible format ## REOPENED TASK — CRITICAL CONTEXT This task was previously marked 'done' but the audit could not verify the work actually landed on main. The original work may have been: - Lost to an orphan branch / failed push - Only a spec-file edit (no code changes) - Already addressed by other agents in the meantime - Made obsolete by subsequent work **Before doing anything else:** 1. **Re-evaluate the task in light of CURRENT main state.** Read the spec and the relevant files on origin/main NOW. The original task may have been written against a state of the code that no longer exists. 2. **Verify the task still advances SciDEX's aims.** If the system has evolved past the need for this work (different architecture, different priorities), close the task with reason "obsolete: " instead of doing it. 3. **Check if it's already done.** Run `git log --grep=''` and read the related commits. If real work landed, complete the task with `--no-sha-check --summary 'Already done in '`. 4. **Make sure your changes don't regress recent functionality.** Many agents have been working on this codebase. Before committing, run `git log --since='24 hours ago' -- ` to see what changed in your area, and verify you don't undo any of it. 5. **Stay scoped.** Only do what this specific task asks for. Do not refactor, do not "fix" unrelated issues, do not add features that weren't requested. Scope creep at this point is regression risk. If you cannot do this task safely (because it would regress, conflict with current direction, or the requirements no longer apply), escalate via `orchestra escalate` with a clear explanation instead of committing.

Completion Notes

Verify task: all acceptance criteria confirmed satisfied on origin/main (commit 700e194d0). token_ledger.py module with all 7 core functions present and tested. 84 accounts, 22912 ledger entries in production DB. API endpoints at lines 11512-11654 in api.py confirmed. Resolution via prior commits c90bb7ce1, 65be4f7ed, 8955a4b80.

Git Commits (20)

[Exchange] Update spec work log — rebase and cleanup [task:exch-cm-04-BOOK]2026-04-26
[Exchange] Restore market bidding compatibility routes [task:exch-cm-04-BOOK]2026-04-26
[Exchange] Guard market-book depth payload shape [task:exch-cm-04-BOOK]2026-04-25
[Exchange] Add LMSR order book and artifact market APIs [task:exch-cm-04-BOOK]2026-04-25
Squash merge: orchestra/task/exch-cm--bidding-system-capital-backed-positions (11 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--bidding-system-capital-backed-positions (11 commits)2026-04-25
[Exchange] Add Bearer API-key auth to /api/market/* write endpoints [task:exch-cm-03-BID]2026-04-25
[Exchange] Market bidding system — capital-backed positions on artifacts [task:exch-cm-03-BID]2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (1 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (2 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--blockchain-bridge-prep-ledger-compatible (1 commits)2026-04-25
[Exchange] Portfolio management — positions, P&L, exposure, Sharpe [task:exch-cm-05-PORT]2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (1 commits)2026-04-25
[Verify] Settlement mechanics atomicity — already resolved on main [task:exch-cm-06-SETL]2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (2 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--blockchain-bridge-prep-ledger-compatible (1 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--blockchain-bridge-prep-ledger-compatible (1 commits)2026-04-25
[Exchange] Blockchain bridge prep — ledger EVM-compatible, export/import, migration guide [task:exch-cm-08-BRGE]2026-04-25
[Exchange] Portfolio management — positions, P&L, exposure, Sharpe [task:exch-cm-05-PORT]2026-04-25
[Verify] Capital-weighted resource allocation — already resolved on main [task:exch-cm-07-RALL]2026-04-25
Spec File

Goal

Build the foundation of SciDEX's virtual economy: a double-entry token ledger that tracks
every token movement with full audit trail. The ledger is designed for eventual crypto
migration — every entry maps to an on-chain transaction.

Current State

  • actor_reputation table exists but is empty (0 records)
  • token_ledger is designed in spec (agent_reputation_tokens_spec.md) but not implemented
  • resource_allocations table exists but is empty
  • No actual token balances anywhere in the system

Schema

CREATE TABLE token_ledger (
    id TEXT PRIMARY KEY,                    -- UUID
    timestamp TEXT NOT NULL DEFAULT (datetime('now')),
    from_account TEXT NOT NULL,             -- agent_id or 'system' (for minting)
    to_account TEXT NOT NULL,               -- agent_id or 'system' (for burning)
    amount REAL NOT NULL CHECK(amount > 0), -- always positive; direction from from/to
    reason TEXT NOT NULL,                   -- structured: 'earn:debate', 'bid:artifact', etc.
    reference_type TEXT,                    -- 'debate_session', 'artifact', 'hypothesis', etc.
    reference_id TEXT,                      -- ID of the referenced entity
    memo TEXT,                              -- human-readable description
    balance_after_from REAL,               -- sender's balance after transaction
    balance_after_to REAL                  -- receiver's balance after transaction
);

CREATE TABLE token_accounts (
    account_id TEXT PRIMARY KEY,            -- agent_id
    balance REAL NOT NULL DEFAULT 0 CHECK(balance >= 0),
    total_earned REAL NOT NULL DEFAULT 0,
    total_spent REAL NOT NULL DEFAULT 0,
    total_staked REAL NOT NULL DEFAULT 0,   -- currently locked in positions
    created_at TEXT DEFAULT (datetime('now')),
    updated_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX idx_ledger_from ON token_ledger(from_account);
CREATE INDEX idx_ledger_to ON token_ledger(to_account);
CREATE INDEX idx_ledger_time ON token_ledger(timestamp);
CREATE INDEX idx_ledger_reason ON token_ledger(reason);

Acceptance Criteria

token_ledger and token_accounts tables created via migration
token_ledger.py module with core functions:
- create_account(agent_id, initial_balance=1000) — endow new agent
- transfer(from_id, to_id, amount, reason, reference_type, reference_id) — move tokens
- mint(to_id, amount, reason, ...) — create new tokens (from 'system')
- burn(from_id, amount, reason, ...) — destroy tokens (to 'system')
- get_balance(agent_id) — current available balance
- get_statement(agent_id, since=None) — transaction history
- get_total_supply() — all tokens in circulation
☐ Balance can never go negative (CHECK constraint + pre-transfer validation)
☐ Every transaction is atomic (single DB transaction)
☐ Initial endowment: seed all existing agents with 1,000 tokens
☐ API: GET /api/token/balance/{agent_id} — balance query
☐ API: GET /api/token/supply — total supply, velocity, distribution
☐ API: GET /api/token/ledger?account={id}&limit=50 — transaction history

Blockchain Compatibility

The ledger is designed so each row maps to an on-chain transaction:

  • from_account → sender address
  • to_account → receiver address
  • amount → transfer value
  • timestamp → block timestamp
  • reason + reference_type + reference_id → transaction memo / event log
  • id → transaction hash

When migrating to crypto, the token_ledger becomes a local index of on-chain state.

Dependencies

  • None (foundation task)

Dependents

  • All other exch-cm-* tasks depend on this ledger

Work Log

Payload JSON
{
  "requirements": {
    "coding": 7,
    "reasoning": 6
  },
  "completion_shas": [
    "700e194d0"
  ],
  "completion_shas_checked_at": "2026-04-19T05:00:23.852643+00:00",
  "_reset_note": "This task was reset after a database incident on 2026-04-17.\n\n**Context:** SciDEX migrated from SQLite to PostgreSQL after recurring DB\ncorruption. Some work done during Apr 16-17 may have been lost.\n\n**Before starting work:**\n1. Check if the task's goal is ALREADY satisfied (run the relevant checks)\n2. Check `git log --all --grep=task:YOUR_TASK_ID` for prior commits\n3. If complete, verify and mark done. If partial, continue. If not done, proceed.\n\n**DB change:** SciDEX now uses PostgreSQL. `get_db()` auto-detects via\nSCIDEX_DB_BACKEND=postgres env var.",
  "_reset_at": "2026-04-18T06:29:22.046013+00:00",
  "_reset_from_status": "done"
}

Sibling Tasks in Quest (Capital Markets) ↗