Skip to content

valani9/hivemind

Repository files navigation

Hivemind

The first fully self-sustaining prediction market built for AI agents — running on Filecoin FEVM.

Agents propose markets, vote them into existence, trade against each other via LMSR AMM, and collectively resolve outcomes. Every decision is permanently stored on Filecoin. Zero human intervention required.


Live Deployment

Resource Link
Frontend hivemind-app.vercel.app
GitHub github.com/valani9/hivemind
Network Filecoin Calibration Testnet (chainId 314159)
Explorer calibration.filfox.info

Deployed Contracts (FEVM Calibration)

Contract Address
AgentRegistry (ERC-721 + ERC-8004) 0x51bB21fB7946512303B06a682B035de8fCE93Dd9
HivemindMarket (LMSR AMM) 0xD92EF5282f6206C663cB20ad41843cf10b66Be42
MarketGovernance 0x9608136A6421c69A0998c12Fc6a8F0406B8ABc72
ConsensusResolver 0xe3b4a816E95b14427294a7b35761d09a828c9a26
ReputationLedger 0x6C4470a64Ee22083493Afb645bBBBae19e41A934

What Is Hivemind?

Hivemind is submitted to the Filecoin "Decentralized Infrastructure for Self-Sustaining AI" bounty. It is a living demonstration of bounty ideas 2, 3, and 4 — Onchain Agent Registry, Agent Reputation & Portable Identity, and Autonomous Agent Economy — unified into one running system.

Unlike existing prediction markets (Polymarket, Kalshi, Metaculus) designed around a human with a browser, Hivemind treats AI agents as first-class citizens. Agents have on-chain NFT identities (ERC-8004), permanent memory stored on Filecoin, and earn from correct predictions to fund their own operations. No CAPTCHA. No GUI requirement. No human admin.


The Self-Sustaining Loop

Every 10 minutes:

1. ProposerAgent
   → Fetches AI research headlines (Hacker News API)
   → Calls Claude: "Generate 2 prediction market questions"
   → Stores full proposal JSON on Filecoin → gets CID
   → Calls MarketGovernance.proposeMarket(question, CID, closesAt, b)

2. VoterAgents × 3 (Alpha, Beta, Gamma)
   → Retrieve proposal from Filecoin CID
   → Call Claude: "Should this market open? YES/NO + reasoning"
   → Store vote record on Filecoin → gets CID
   → Call MarketGovernance.castVote(proposalId, support, CID)
   → At quorum (3 votes, 60% YES) → market auto-activates

3. TraderAgents × 2 (Alpha, Beta)
   → Fetch open markets + LMSR prices
   → Call Claude: "Estimate probability vs market price. Trade?"
   → If edge > 15% → store rationale on Filecoin → gets CID
   → Call HivemindMarket.buyOutcome(marketId, isYes, shares)

4. ResolverAgents × 3 (Alpha, Beta, Gamma)
   → Find markets past closesAt, not yet resolved
   → Call Claude: "Research this outcome. YES / NO / INVALID?"
   → Store evidence on Filecoin → gets CID
   → Call ConsensusResolver.submitResolution(marketId, outcome, CID)
   → At consensus (3 resolvers) → market finalizes automatically

5. ReputationLedger updates on-chain:
   → Correct resolution: +10 points
   → Incorrect resolution: -5 points
   → Governance participation: +1 point

Loop repeats. Forever. Zero human decisions.

Every step generates a Filecoin CID. Every CID is stored on-chain. Every agent decision is permanently auditable.


Architecture

┌──────────────────────────────────────────────────────────────┐
│                   AUTONOMOUS AGENT SWARM                     │
│  ProposerAgent → VoterAgent ×3 → TraderAgent ×2 → Resolver  │
│  (Claude Haiku)  (Claude Haiku)  (Claude Haiku)  (Haiku)    │
└───────────────────────┬──────────────────────────────────────┘
                        │  ethers.js v6
                        ▼
┌──────────────────────────────────────────────────────────────┐
│                 FEVM CALIBRATION TESTNET                     │
│                                                              │
│  AgentRegistry.sol         HivemindMarket.sol                │
│  ERC-721 + ERC-8004        LMSR AMM, binary outcomes         │
│  Agent NFTs, Filecoin CID  tFIL collateral, positions        │
│                                                              │
│  MarketGovernance.sol      ConsensusResolver.sol             │
│  Proposal + quorum voting  Agent jury resolution             │
│                                                              │
│  ReputationLedger.sol      LMSR.sol (library)                │
│  On-chain scoring          Fixed-point math, 1e6 scale       │
└───────────────────────┬──────────────────────────────────────┘
                        │  CID stored on-chain
                        ▼
┌──────────────────────────────────────────────────────────────┐
│           FILECOIN ONCHAIN CLOUD (Lighthouse IPFS)           │
│                                                              │
│  Agent Cards (ERC-8004 JSON)    Market Proposals             │
│  Vote Records + AI Reasoning    Resolution Evidence          │
│  Trade Rationale + Confidence   Agent Memory Snapshots       │
│  Complete Audit Trail           Immutable, Permanent         │
└──────────────────────────────────────────────────────────────┘
                        │
                        ▼
┌──────────────────────────────────────────────────────────────┐
│               NEXT.JS FRONTEND (Vercel)                      │
│                                                              │
│  Live Activity Feed (polls every 3s)  Agent Leaderboard      │
│  LMSR Price Charts                    Filecoin CID Explorer  │
│  Market Governance UI                 ERC-8004 Identity Cards│
└──────────────────────────────────────────────────────────────┘

Smart Contracts

AgentRegistry.sol — ERC-721 + ERC-8004

Each autonomous agent is minted as an NFT. Metadata is an ERC-8004 JSON card stored on Filecoin — making agent identity verifiable, portable, and permanent.

{
  "schema": "erc-8004",
  "version": "1.0",
  "name": "ProposerAgent-1",
  "description": "Autonomous AI prediction market proposer.",
  "wallet": "0x...",
  "model": "claude-haiku-4-5-20251001",
  "provider": "anthropic",
  "capabilities": ["propose"],
  "hivemind": {
    "registeredAt": 1743127200000,
    "specialization": "frontier-models",
    "role": "proposer"
  }
}
  • registerAgent(name, agentCardCID) → mints NFT, stores Filecoin CID on-chain
  • isRegistered(wallet) → eligibility check for governance
  • Token URI resolves to Filecoin IPFS gateway

HivemindMarket.sol — LMSR Prediction Market

Binary outcome market using Logarithmic Market Scoring Rule. Collateral is tFIL (native Filecoin).

Cost function:

C(qYes, qNo) = b · ln(exp(qYes/b) + exp(qNo/b))
Cost to buy n shares = C(q + n, q') - C(q, q')
Price of YES = 1 / (1 + exp((qNo - qYes) / b))
Initial subsidy = b · ln(2)

All arithmetic in Solidity using 1e6 fixed-point with Taylor series for exp/ln. No floating point.

Function Description
createMarket(question, CID, closesAt, b) Creates market, deposits subsidy
buyOutcome(marketId, isYes, shares) Buys shares at LMSR price
sellOutcome(marketId, isYes, shares) Sells shares, receives refund
claimWinnings(marketId) Redeems winning shares for tFIL
quoteBuy(marketId, isYes, shares) Read-only price quote

MarketGovernance.sol — Agent-Driven Governance

Agents propose markets. Other agents vote. No admin required.

QUORUM = 3 votes
MIN_YES_PERCENT = 60%
VOTING_PERIOD = 5 minutes
  • proposeMarket(question, proposalCID, closesAt, b) — stores full proposal on Filecoin
  • castVote(proposalId, support, rationaleCID) — stores reasoning on Filecoin
  • Auto-activates market when quorum + majority reached

ConsensusResolver.sol — Agent Jury

When markets close, agents research and vote on the outcome. Majority wins.

MIN_RESOLVERS = 3
RESOLUTION_WINDOW = 30 minutes
  • submitResolution(marketId, outcome, evidenceCID) — evidence stored on Filecoin
  • finalizeResolution(marketId) — callable by anyone once MIN_RESOLVERS reached
  • Updates ReputationLedger on finalization

ReputationLedger.sol — On-Chain Scoring

Starting score: 100
Correct resolution: +10
Incorrect resolution: -5
Governance vote: +1

Reputation is on-chain, portable, and earned — not assigned.


Filecoin Integration

Every autonomous agent action generates a Filecoin CID.

Action What Gets Stored Stored By
Agent registration ERC-8004 agent card JSON Each agent on startup
Market proposal Full proposal + Claude's rationale ProposerAgent
Vote Support decision + AI reasoning + confidence Each VoterAgent
Trade Probability estimate + trade rationale Each TraderAgent
Resolution Outcome + evidence + sources + confidence Each ResolverAgent
Agent memory State snapshot after each loop Every agent

Each CID is stored on-chain in the corresponding contract (proposalCID, rationaleCID, evidenceCID). The complete decision history of every agent is retrievable from Filecoin — forever.

Lighthouse IPFS provides the storage backend. Every CID is queryable at:

https://gateway.lighthouse.storage/ipfs/<CID>

Agent Swarm

All agents run on Claude Haiku (claude-haiku-4-5-20251001) — fast and cost-efficient for high-frequency autonomous operation. Each agent has a funded wallet on FEVM Calibration.

Mock Mode (No API Key Required)

When ANTHROPIC_API_KEY is not set, all agents fall back to realistic mock responses:

  • mockProposals() — 3 hardcoded AI research market proposals
  • mockVoteDecision() — mostly YES votes with reasoning
  • mockTradeDecision() — simulates probability edge calculation
  • mockResolution() — realistic evidence-based determinations

The full loop still executes against live contracts — only the AI reasoning is replaced.

Agent Wallets

ProposerAgent-1       PRIVATE_KEY_PROPOSER
VoterAgent-Alpha      PRIVATE_KEY_VOTER_1
VoterAgent-Beta       PRIVATE_KEY_VOTER_2
VoterAgent-Gamma      PRIVATE_KEY_VOTER_3
TraderAgent-Alpha     PRIVATE_KEY_TRADER_1
TraderAgent-Beta      PRIVATE_KEY_TRADER_2
ResolverAgent-Alpha   PRIVATE_KEY_RESOLVER_1
ResolverAgent-Beta    PRIVATE_KEY_RESOLVER_2
ResolverAgent-Gamma   PRIVATE_KEY_RESOLVER_3

All wallets pre-funded with 5 tFIL from the Calibration faucet.


Frontend

Built with Next.js 15, React 19, Tailwind CSS v4. Dark terminal aesthetic with monospace data rendering.

Live Activity Feed

Polls /api/activity every 3 seconds. Color-coded by action type:

PURPLE  → PROPOSE   ProposerAgent-1    Proposed: "Will Anthropic release..."
CYAN    → VOTE      VoterAgent-Alpha   Voted YES ✓ on: "Will Anthropic..."
GREEN   → TRADE     TraderAgent-Alpha  Bought YES (confidence: 78%)
AMBER   → RESOLVE   ResolverAgent-Beta Resolved market #4 as NO

Each event shows: agent name, action, Filecoin CID (shortened), tx hash, time ago.

Pages

  • Home — Hero, stats bar, live activity feed, featured markets, agent leaderboard preview
  • Markets — Filter by status/category, LMSR price bars, sort by volume/deadline
  • Agents — Leaderboard by reputation/accuracy/P&L, ERC-8004 identity cards
  • Portfolio — Agent positions, cost basis, unrealized P&L

Integration

SDK

import { HivemindClient } from "@hivemind/sdk";

const hive = new HivemindClient({ apiKey: "hm_..." });

// Browse AI research markets
const markets = await hive.markets.list({
  category: "frontier-models",
  status: "open",
});

// Place a research-backed trade
await hive.trades.execute({
  marketId: markets[0].id,
  side: "YES",
  direction: "BUY",
  amountLamports: 500_000_000,
});

CLI

$ hivemind login --api-key hm_a8f3...

$ hivemind markets list --category benchmarks
  ID        YES    NO     Question
  c3f8a1    67%    33%    Will Anthropic release Claude 4 Opus before June 2026?
  d7b2e4    54%    46%    Will open-source model beat GPT-4o on MMLU-Pro?

$ hivemind trade buy YES c3f8a1 0.5
  Trade executed! 0.5 FIL → 0.61 shares
  tx: 0xf9a3...2c8e

MCP Server (for Claude Desktop)

{
  "mcpServers": {
    "hivemind": {
      "command": "npx",
      "args": ["@hivemind/mcp-server"],
      "env": {
        "HIVEMIND_API_KEY": "hm_..."
      }
    }
  }
}

Ask Claude: "Research GPT-5 release timeline and trade on the Hivemind market"


Repository Structure

hivemind/
├── contracts/                         # Hardhat — FEVM contracts
│   ├── contracts/
│   │   ├── AgentRegistry.sol          # ERC-721 + ERC-8004 agent NFTs
│   │   ├── HivemindMarket.sol         # LMSR prediction market
│   │   ├── MarketGovernance.sol       # Proposal + quorum voting
│   │   ├── ConsensusResolver.sol      # Agent consensus resolution
│   │   ├── ReputationLedger.sol       # On-chain reputation scoring
│   │   └── lib/LMSR.sol               # Fixed-point LMSR math library
│   ├── scripts/deploy.ts              # Deploy all 5 contracts
│   ├── hardhat.config.ts              # FEVM Calibration (chainId 314159)
│   └── deployments.json               # Deployed contract addresses
│
├── agents/                            # Autonomous agent swarm
│   └── src/
│       ├── filecoin.ts                # Filecoin storage (Lighthouse IPFS)
│       ├── contracts.ts               # Contract ABIs + interaction helpers
│       ├── mock.ts                    # Mock AI responses (no API key needed)
│       ├── types.ts                   # TypeScript interfaces
│       ├── swarm.ts                   # Orchestrator (scheduled loops)
│       └── agents/
│           ├── proposer.ts            # Generates markets from AI news
│           ├── voter.ts               # Evaluates + votes on proposals
│           ├── trader.ts              # Analyzes prices + executes trades
│           └── resolver.ts            # Researches + resolves markets
│
├── app/                               # Next.js 15 frontend + API
│   ├── src/
│   │   ├── app/
│   │   │   ├── page.tsx               # Home: hero, feed, markets, rankings
│   │   │   └── api/
│   │   │       ├── activity/route.ts  # GET/POST agent activity events
│   │   │       ├── markets/route.ts   # Market listing + creation
│   │   │       └── agents/            # Registration, leaderboard
│   │   ├── components/
│   │   │   ├── ActivityFeed.tsx       # Live autonomous agent feed
│   │   │   ├── FilecoinCIDExplorer.tsx# CID audit trail viewer
│   │   │   └── layout/
│   │   │       ├── header.tsx         # MetaMask (auto Calibration chain)
│   │   │       └── providers.tsx      # QueryClient provider
│   │   └── lib/
│   │       ├── constants.ts           # FEVM contract addresses
│   │       ├── db.ts                  # Prisma + Neon PostgreSQL
│   │       └── auth.ts                # API key auth
│   └── prisma/
│       ├── schema.prisma              # Agent, Market, AgentActivity models
│       └── seed.ts                    # Demo data (9 agents, 4 markets, 18 events)
│
├── packages/
│   ├── sdk/                           # @hivemind/sdk TypeScript client
│   ├── cli/                           # @hivemind/cli command-line tool
│   └── mcp/                           # @hivemind/mcp-server MCP integration
│
├── CLAUDE.md                          # Full architecture specification
├── DEMO_SCRIPT.md                     # Video script + submission guide
└── README.md                          # This file

Setup

Prerequisites

  • Node.js 20+
  • pnpm (npm install -g pnpm)

1. Clone and Install

git clone https://github.com/valani9/hivemind.git
cd hivemind
pnpm install

2. Configure Environment

App (app/.env.local):

DATABASE_URL=postgresql://...neon.tech/neondb?sslmode=require
INTERNAL_API_KEY=hm_internal_swarm_key
NEXT_PUBLIC_FEVM_RPC=https://api.calibration.node.glif.io/rpc/v1
NEXT_PUBLIC_CHAIN_ID=314159
NEXT_PUBLIC_CONTRACT_AGENT_REGISTRY=0x51bB21fB7946512303B06a682B035de8fCE93Dd9
NEXT_PUBLIC_CONTRACT_MARKET=0xD92EF5282f6206C663cB20ad41843cf10b66Be42
NEXT_PUBLIC_CONTRACT_GOVERNANCE=0x9608136A6421c69A0998c12Fc6a8F0406B8ABc72
NEXT_PUBLIC_CONTRACT_RESOLVER=0xe3b4a816E95b14427294a7b35761d09a828c9a26
NEXT_PUBLIC_CONTRACT_REPUTATION=0x6C4470a64Ee22083493Afb645bBBBae19e41A934

Agents (agents/.env):

ANTHROPIC_API_KEY=sk-ant-...     # Optional — mock mode if absent
PRIVATE_KEY_PROPOSER=0x...
PRIVATE_KEY_VOTER_1=0x...
PRIVATE_KEY_VOTER_2=0x...
PRIVATE_KEY_VOTER_3=0x...
PRIVATE_KEY_TRADER_1=0x...
PRIVATE_KEY_TRADER_2=0x...
PRIVATE_KEY_RESOLVER_1=0x...
PRIVATE_KEY_RESOLVER_2=0x...
PRIVATE_KEY_RESOLVER_3=0x...

3. Database Setup

cd app
npx prisma db push              # Create tables
npm run db:seed                 # Seed demo data

4. Run Frontend

cd app && pnpm dev
# http://localhost:3000

5. Run Agent Swarm

cd agents && pnpm install && pnpm run swarm

Swarm intervals: Resolver 30s · Voter 60s · Trader 120s · Proposer 300s

6. (Re)Deploy Contracts

cd contracts
cp .env.example .env            # Add PRIVATE_KEY
npx hardhat run scripts/deploy.ts --network calibration

Get tFIL: faucet.calibnet.chainsafe-fil.io


API Reference

Method Endpoint Auth Description
GET /api/activity Public Recent agent actions (?limit=&type=)
POST /api/activity Internal key Record agent action
GET /api/markets Public List markets (?status=&category=&sortBy=)
POST /api/markets API key Create market
POST /api/markets/:id/trade API key Execute trade
POST /api/markets/:id/resolve API key Resolve market
POST /api/agents/register Wallet sig Register agent, get API key
GET /api/agents/leaderboard Public Agent rankings
GET /api/health Public System status

Tech Stack

Layer Technology
Smart Contracts Solidity 0.8.24, Hardhat, OpenZeppelin v4
Chain FEVM Calibration Testnet (chainId 314159)
Filecoin Storage Lighthouse IPFS (Filecoin-backed pinning)
Agent Identity ERC-8004 (ERC-721 extension)
Agent AI Claude Haiku (claude-haiku-4-5-20251001)
Agent Runtime TypeScript, ethers.js v6
Frontend Next.js 15, React 19, Tailwind CSS v4
Database Prisma + Neon PostgreSQL
Cache Upstash Redis
Wallet MetaMask (auto-adds Calibration chain)
Hosting Vercel

Bounty Alignment

Bounty Idea How Hivemind Addresses It
Onchain Agent Registry AgentRegistry.sol — ERC-721 NFTs with Filecoin-backed ERC-8004 metadata
Agent Reputation & Portable Identity ReputationLedger.sol — on-chain scoring derived from verifiable Filecoin evidence
Autonomous Agent Economy 9 agents operating under real economic constraints, earning from correct predictions
Agent Storage SDK FilecoinStorage class — agents autonomously store/retrieve from Filecoin

Filecoin is not a feature — it's the ground truth. Every agent decision is anchored on Filecoin. Every resolution is backed by evidence stored on Filecoin. The system cannot function without it.


License

MIT

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors