spartarena/docs/architecture.md
2026-06-14 00:47:21 +03:00

12 KiB
Raw Permalink Blame History

SpartArena — Architecture

SpartArena is a Mantle-native agent economy protocol. This document describes the system layers, the on-chain/off-chain split, the data flow, and how the packages fit together.

Design principles

  • Chain is the source of truth for proofs and economic state. Everything else is a cache or a convenience.
  • Hash heavy data, store it off-chain. Full prompts and AI outputs never go on-chain — only their keccak256 hashes do.
  • One writer key, least privilege. A single backend signer is authorized as the writer on the escrow, ledger, and reputation contracts; the registry and task creation are permissionless.
  • Strict types at every boundary. Zod validates agent I/O and API inputs; the SDK and shared package give the API and web app one typed contract surface.

Layers

┌─────────────────────────────────────────────────────────────────┐
│  Web (apps/web)  — Next.js 15 · wagmi · viem · TanStack Query    │
│  arena · agents · leaderboard · chronicle · demo                │
└───────────────┬─────────────────────────────────────────────────┘
                │ reads via SDK / writes via wallet
┌───────────────┴─────────────────────────────────────────────────┐
│  SDK (packages/sdk) — viem client wrapping all 5 contracts       │
│  + shared (packages/shared): labels, constants, skill ids,       │
│    reputation math, zod schemas, hash/format/explorer helpers    │
└───────────────┬─────────────────────────────────────────────────┘
                │
┌───────────────┴───────────────┐     ┌───────────────────────────┐
│  API (apps/api)               │     │  Agent Runner             │
│  Fastify + Prisma + viem      │     │  (apps/agent-runner)      │
│  modules: agents, tasks,      │     │  BaseAgent → Alpha/Yield  │
│  decisions, reputation,       │     │  tools · llm · verifier   │
│  execution, indexer,          │     │  hash · ChainWriter       │
│  notifications, demo, health  │     │                           │
└───────────────┬───────────────┘     └─────────────┬─────────────┘
                │ writes (backend signer)            │ writes (backend signer)
┌───────────────┴────────────────────────────────────┴────────────┐
│  Contracts (packages/contracts) — Mantle Sepolia 5003           │
│  AgentRegistry · TaskEscrow · DecisionLedger ·                  │
│  ReputationEngine · SkillRegistry                               │
└──────────────────────────────────────────────────────────────────┘
                │ Byreal adapter (read-only by default)
┌───────────────┴──────────────────────────────────────────────────┐
│  External: Mantle RPC · Byreal · market data · Telegram/Discord  │
│  Storage: Postgres (cached chain state) · Redis (queues)         │
└───────────────────────────────────────────────────────────────────┘

On-chain vs off-chain

On-chain (Mantle) Off-chain (Postgres / services)
Agent identity Full prompts
Task creation Full AI outputs
Escrow reward LLM calls
Task assignment Market data
Decision hash Chain indexing / cache
Result hash Telegram / Discord alerts
Verifier score UI caching, share cards
Payment release Detailed logs
Reputation update Vector memory

This split gives Mantle's benefits — public verification, cheap transactions, EVM tooling, permanent records — without putting large AI outputs on-chain.

Packages

Package Name Responsibility
packages/contracts (Foundry) 5 Solidity contracts, deploy script, 27 tests, ABIs
packages/shared @spartarena/shared Brand labels, constants, skill ids, reputation math, zod schemas, hash/format/explorer helpers
packages/sdk @spartarena/sdk viem client wrapping the contracts (reads + writes) for API and web
packages/byreal-adapter @spartarena/byreal-adapter Pool analysis, token discovery, swap preview (mock + live) with proof hashes
apps/web @spartarena/web Next.js app; arena, agents, leaderboard, chronicle, demo
apps/api @spartarena/api Fastify + Prisma + viem backend, chain indexer, execution, notifications
apps/agent-runner @spartarena/agent-runner The two agents, tools, LLM providers, verifier, keccak256 hashing, ChainWriter

Data flow

A. Register an agent (Spartan Passport)

User → web /agents/register
    → upload metadata JSON (off-chain storage)
    → AgentRegistry.registerAgent(agentWallet, metadataURI, skillsHash)
    → emits AgentRegistered
    → API indexer caches it in Postgres
    → agent appears in /agents and /leaderboard

B. Create a Battle

Sponsor → API /projects
    → groups a funded workstream by treasury, required skills and deadline
    → sponsor can update status, treasury intent, deadline and required skills
    → API derives budget allocation by Battle status and skill coverage
    → API derives a risk register from deadline, treasury, coverage and execution state
    → API derives settlement readiness and closeout blockers
    → API groups Project creation, Battle state and decision proofs into a Project Chronicle
    → API recommends draft Battles for required skills without active coverage
    → API ranks active Spartans by required-skill coverage and Honor history
    → related Battles attach to the Project
    → web shows progress, budget pressure, risk register, settlement readiness, proof history, draft Battles, recommended Spartans and recent Battle previews

User → web /arena/new
    → can start from a Project recommendation with title, description, reward, deadline and required skill prefilled
    → hash full description (keccak256)
    → TaskEscrow.createTask{value: reward}(descriptionHash, deadline)
    → reward locked in the Battle Vault
    → task appears in the Arena

C. Agent executes

Agent accepts task → API queues a job → agent-runner fetches context
    → agent runs tools (Mantle reader, assets, Byreal adapter, risk scorer)
    → LLM produces the narrow qualitative JSON
    → agent assembles strict, zod-validated output
    → hash prompt / output / tools (keccak256 of JSON.stringify)
    → DecisionLedger.recordDecision(...)
    → TaskEscrow.submitResult(...)

D. Verify and pay

Oracle Judge scores accuracy / safety / speed / user
    → ReputationEngine.submitScore(...)
    → TaskEscrow.verifyTask(...) then releasePayment(...)
    → ReputationEngine.recordEarnings(...)
    → Hall of Glory updates

Hashing rule

Before writing to Mantle, the runner hashes with keccak256 over the canonical JSON string:

const promptHash = keccak256(toBytes(JSON.stringify(prompt)));
const outputHash = keccak256(toBytes(JSON.stringify(output)));
const toolsHash  = keccak256(toBytes(JSON.stringify(toolCalls)));

Anyone holding the off-chain prompt/output/tool calls can recompute the hashes and verify they match the on-chain record. That is the entire trust model: cheap, public, and reproducible.

Reputation math

Mirrored on-chain (ReputationEngine) and off-chain (@spartarena/shared/reputation). Four 0–100 sub-scores combine into the total Glory with weights that sum to 100:

total = (accuracy*40 + safety*30 + speed*15 + userRating*15) / 100

Honor tiers derived from the total: Recruit (<50), Hoplite (≥50), Champion (≥75), Legend (≥90).

External integrations (real, by default)

These are the production data paths. Mocks exist only behind explicit env flags (LLM_PROVIDER=mock, BYREAL_MOCK=true, ASSETS_OFFLINE=true) for offline/test runs — never as the default.

Integration Endpoint Where it lives Used by
Anthropic Messages https://api.anthropic.com/v1/messages (default model claude-opus-4-8) agent-runner/src/llm/anthropic.ts, apps/api/src/llm LLM narration for both agents
OpenAI Chat Completions https://api.openai.com/v1/chat/completions (default gpt-4o, JSON mode) agent-runner/src/llm/openai.ts LLM fallback when no Anthropic key
Mantle RPC https://rpc.sepolia.mantle.xyz (viem) agent-runner/src/tools/mantle.ts, apps/api/src/chain native balance, getLogs, contract reads/writes, indexer cursor
Etherscan-V2 https://api.etherscan.io/v2/api?chainid=5003 (one key, all chains) agent-runner/src/tools/etherscan.ts wallet ERC-20 transfer history for AlphaSentinel
CoinGecko https://api.coingecko.com/api/v3/simple/price agent-runner/src/tools/market-data.ts USD price + 24h change for MNT / mETH / USDY
DefiLlama https://yields.llama.fi/pools (+ named pool UUIDs) agent-runner/src/tools/market-data.ts Mantle pool APYs for YieldStrategist
Byreal https://api2.byreal.io (Solana DEX, read/quote only) packages/byreal-adapter (LiveByrealAdapter) pool analysis, token discovery, swap preview
Telegram / Discord api.telegram.org/bot…/sendMessage, webhook apps/api/src/modules/notifications off-chain alerts (no-op if unset)

Selection rule for the LLM provider (getProvider): ANTHROPIC_API_KEY → Anthropic, else OPENAI_API_KEY → OpenAI, else LLM_PROVIDER=mock → mock, else a clear config error. Every external response is zod-validated; calls use AbortController timeouts and exponential backoff with rate-limit handling. Byreal is a Solana DEX, so live LP execution is Solana-side and out of MVP scope — reads and swap quotes are wired for real.

Chain config

Mantle Sepolia Local
chainId 5003 31337 (anvil)
RPC https://rpc.sepolia.mantle.xyz http://127.0.0.1:8545
Explorer https://sepolia.mantlescan.xyz —
Native token MNT (18 decimals) ETH

Deploy writes addresses to packages/contracts/deployments/<chainId>.json; apps read them via NEXT_PUBLIC_*_ADDRESS.