All case studies

Pre-launch · DeFi + AI agent layer

Mandate.Finance

Role
Founding Product Engineer & Technical Co-founder
Period
2026 – present
Screenshot of Mandate.Finance

Key numbers

  • 11Components production-deployed
  • 5Weeks to ship
  • 1,300+Total commits
  • ~140kLines of code

01 · Mandate.Finance

Problem

Build a DeFi platform where autonomous AI agents manage real on-chain funds while preserving user self-custody, gasless UX and real-time oversight. Strong guardrails are mandatory — on-chain irreversibility is unforgiving.

02 · Mandate.Finance

Architecture

Multi-repo platform across 11 workspaces — 6 runtime services on GCP Cloud Run (`backend`, `agent-executor`, `signing-service`, `mcp-gateway`, `jobs`, `stats-api`), a Foundry smart-contract suite (ERC-7821 `KairosBatchExecutor` + UUPS `KairosBillingTreasury` deployed at the same address on Base, Arbitrum and Ethereum mainnet via CREATE2), a private Postgres 16 with 59 versioned SQL migrations, two Next.js 15 frontends (`ui` and `landing`), an SSE-based agent test harness, and a public docs site. Agent layer: a single LLM loop in `agent-executor` parameterised by 7 job types (deploy / scan / trade / rebalance / report / exit / chat) and ~8 strategy templates stored in Postgres — per-task tool whitelists, code-level SELL GUARDs and per-vault prompt overrides at three resolution tiers. The MCP gateway (Python + FastAPI) brokers all read-only DeFi data via JSON-RPC over stdio with request-id correlation to prevent cross-tenant pollution; the signing-service is the sole custodian of per-agent EOAs (AES-256-GCM keystore in Postgres) and the only path to chain writes — every write rides on EIP-7702 Type-4 transactions sponsored by a single hot wallet, with a Redis-backed atomic nonce counter so multi-instance Cloud Run scale-out is safe.

03 · Mandate.Finance

Decisions

  • 01

    Single agent loop + 7 job types + 8 strategy templates

    One LLM loop (`runAgentLoop` in `agent-executor/src/llm/loop.ts`), one source of truth for prompts (`strategy_templates` table in Postgres), per-job-type tool whitelists (deploy / scan / trade / rebalance / report / exit / chat) and three-tier per-vault prompt overrides. Adding a new strategy is a SQL row, not a service. This replaces the 'multi-agent (evaluator/researcher/manager/executor)' framing — the real story is runtime parameterisation, not a role topology.

  • 02

    Three layers of defense against deploy-step hallucinations

    When small models hallucinated 'Deployment Complete' without calling `register_vault`, the platform deployed second on-chain vaults and stranded funds. Three layers: (1) receipt-parsed auto-register inside the `deploy_vault` branch using the signing-service's `VaultCreated` topic, (2) idempotent `registerVaultInDb` that short-circuits on duplicate-active, (3) post-loop validator that overrides the LLM's claimed decision to `'error'` when `getVaultStatus(vaultId)` shows pending/null.

  • 03

    Code-level SELL GUARD the LLM cannot bypass

    On trader vaults (`category.endsWith('-trader')`), when `factor_swap_openocean` would sell the trading token for the denominator the executor calls `simulate_exit` server-side, computes cost basis from `vault_trades`, and blocks the swap if PnL exceeds the stop-loss threshold. The LLM cannot retry or argue — it only sees `SELL_BLOCKED`. Origin: deepseek bypassed the prompt-level mandate three times → enforcement moved into code.

  • 04

    Per-agent EOA custody + sponsor pays gas

    Backend never sees plaintext keys: each agent EOA is generated and AES-256-GCM-encrypted inside `signing-service/src/keystore.ts`, persisted in `signing_keystores`, decrypted into a 5-min TTL cache. Users sign EIP-7702 authorizations delegating to a single batch executor at the same CREATE2 address on three chains; the platform's hot wallet pays gas. Result: gasless self-custody UX with zero trusted wallet relayers in between.

  • 05

    Atomic sponsor nonce via Redis EVAL Lua

    The in-process `SponsorMutex` only serializes a single replica; multi-instance Cloud Run scale-out could race on `getTransactionCount(sponsor, 'pending')`. Fix: Redis-backed atomic counter `sponsor-nonce:{chainId}` driven by an `EVAL` Lua script (`GET → SET(n+1) → return n` atomically), with cold-start path that reads chain nonce only when the key is absent. Fallback to in-memory `Map` when Redis is unavailable, with warn log for ops.

  • 06

    Multi-tenant MCP-over-stdio with request-id correlation

    Naive JSON-RPC over a single stdin/stdout pipe is unsafe for a multi-tenant gateway: a single `console.log` from any transitive dep polluted the response stream and produced cross-vault data scrambling (observed in production 2026-04-15 — agent A saw agent B's `factor_vault_analytics` result). The gateway loops on `stdout.readline()` discarding any frame whose `id` doesn't match the in-flight request — the only durable defence against this class of multi-tenant bug.

  • 07

    In-process leverage watchdog (HF<1.2 emergency exit)

    `agent-executor/src/leverage-watchdog.ts`: 60s `setInterval` started post-`app.listen`, stopped on SIGTERM. Each tick: (1) load vaults with `mode: leverage`, (2) read `min(healthFactor)` across Aave + Morpho via `factor_vault_analytics`, (3) on `HF < config.minHealthFactor` insert a `custom_jobs` row with the exit-leverage prompt scheduled `* * * * *`, (4) below the hard 1.2 floor emit an EMERGENCY-prefixed prompt and `log.error`. Single-replica Cloud Run + in-process `inFlight` guard prevents concurrent ticks.

  • 08

    Gasless UX with self-custody

    Privy + EIP-7702 Type-4 sponsored transactions + ERC-7821 batch executor: users never touch a private key and never pay gas, while keeping self-custody. Cross-chain via LiFi feels like a single operation.

  • 09

    Cost-driven multi-provider LLM

    Multi-provider abstraction in production (DeepInfra as default, OpenRouter as backup). Anthropic stays for review/research, not in the production LLM router. Migration from Ollama Cloud to DeepInfra cut cost ~50%. Cost-driven pattern, not capability-driven.

04 · Mandate.Finance

Results

  • 11

    Components production-deployed

  • 5

    Weeks to ship

  • 1,300+

    Total commits

  • ~140k

    Lines of code

  • 94%

    UI authorship

  • 127 (15 suite, 7 invarianti × 256 run)

    Smart-contract tests

  • 59

    Versioned SQL migrations

  • 72 eventi · 8 domini · 3 tier

    Notification system

  • Ethereum + Base + Arbitrum

    Chains (same address via CREATE2)

05 · Mandate.Finance

Tech & Pillars

Tech stack

  • Next.js 15
  • React 19
  • TypeScript
  • Tailwind
  • Privy
  • Wagmi / viem
  • Foundry / Solidity 0.8.28
  • EIP-7702 / ERC-7821
  • ERC-7821 batch executor
  • UUPS billing treasury
  • Factor StudioPro vault factory
  • Express
  • Drizzle ORM
  • Postgres 16
  • Python 3.12 / FastAPI
  • MCP (JSON-RPC over stdio)
  • GCP Cloud Run
  • Cloud SQL (private VPC)
  • Memorystore Redis
  • Secret Manager
  • DeepInfra
  • OpenRouter
  • LiFi (cross-chain)
  • Resend + React Email
  • Stripe
  • Pino
  • Vitest
  • Three.js / R3F
  • Custom stats service (Go + GCS)

Pillars touched

  • Traditional Software Engineering
  • AI Engineering
  • Cloud & DevOps
  • Web3 / Blockchain
  • IT Management & Digitalization
  • Business Strategy

Contact

Building something similar?

Walk me through the context in a 30-minute call.

hello@andrea0x.me · @andrea0x_eth

Mandate.Finance — DeFi + AI agent layer · Andrea Ritondale