Technical Reference

How ChainForge AI works

A technical walkthrough of the architecture, security model, contract system, and AI pipeline behind ChainForge AI.

System Architecture

EIP-1167 Clone Factory

Solidity

CampaignFactory V2 deploys minimal proxy clones of registered implementation contracts. Each campaign is a separate on-chain contract sharing logic with the implementation but maintaining independent state. Gas cost per deploy is substantially lower than redeploying full contracts.

factory.cloneCampaign(templateName, options, deadline, creator)

Template Registry

Solidity

Implementations are registered by name via setImplementation(). The factory reads bytecode from the implementation at the registered address and clones it. This separates template logic from campaign data, making upgrades possible for new campaigns without touching existing ones.

Creator Attribution

On-Chain

The connected wallet address is passed explicitly as the creator parameter and recorded on-chain in the factory's creator mapping. The server wallet that pays deployment gas is NOT the creator, your address is.

factory.getCreatorCampaigns(creatorAddress) → address[]

Lifecycle State Machine

Solidity

Each V2 campaign moves through explicit states: OPEN → CLOSED → REVEALED (predictions) or OPEN → CLOSED (votes/surveys). State transitions are enforced at the contract level, only the creator can close or reveal, and only in valid sequence.

enum CampaignState { OPEN, CLOSED, REVEALED }

Gas Sponsorship & Relay Security

Relay-Friendly Contract Signatures

V2 Contracts

All V2 contracts accept an explicit voter address parameter instead of relying on msg.sender. This lets the relayer submit transactions while correctly attributing participation to the real user wallet.

submitPrediction(address voter, uint256 optionIndex)
castVote(address voter, uint256 optionIndex)
submitResponse(address voter, uint256 optionIndex)

EIP-191 Signature Verification

Security

Before the relay submits any transaction, it verifies a signed authorization message from the user. The message includes the contract address, option index, user address, and a timestamp. The relay recovers the signer address and rejects requests where the signer doesn't match the declared user.

recoverMessageAddress({ message, signature }) → recoveredAddress
assert(recoveredAddress === userAddress)

5-Minute Authorization Window

Security

The issuedAt timestamp in the authorization message must be within 300 seconds of the relay's server time. Replayed or stale signatures are rejected automatically.

Rate Limiting

Abuse Prevention

Relay: 10 sponsored interactions per wallet per minute. Plan: 5 AI planning requests per IP per minute. Deploy: 3 campaign deployments per wallet per minute. All limits use in-memory sliding windows.

Current Live Contracts

CampaignFactory V2

WireFluid EVM

EIP-1167 clone factory. Registers templates, deploys clones, tracks creator → campaign mappings.

0x596FDe32DDde7e6e471adD72161F455429753c73

PredictionLogicV2

WireFluid EVM

Full OPEN → CLOSED → REVEALED lifecycle. One prediction per wallet. Creator reveals a winner index after close.

0x16FCFEf59360f1e7d8C615014328bD8419a9Be59

VotingLogicV2

WireFluid EVM

OPEN → CLOSED fan vote. One vote per wallet. Deadline-based or creator-controlled close.

0x0225592B56BA91f153AE461C829b6fcC7B575B0e

SurveyLogic

WireFluid EVM

OPEN → CLOSED survey. No deadline, creator closes manually. One response per wallet.

0x582fE72d288aF5621D1B4052D1164403825f7500

AI Planning Pipeline

Architect Agent

OpenAI

Receives the plain-English prompt and produces a structured campaign plan via OpenAI structured outputs. Determines the template type, options, deadline strategy, and metadata. Falls back gracefully when the requested campaign type is not in the supported template set.

Auditor Agent

OpenAI

Independently validates the Architect's plan against the supported template registry and deployment constraints. Returns a pass/fail with named checks and a plain-English summary. A failed audit blocks deployment and surfaces the reason to the user.

Deployment Agent

Next.js API

Converts the validated plan into a ready-to-deploy payload. Selects the implementation address, encodes options, sets deadlines. The user reviews this payload as a structured card before signing and deploying.

NDJSON Streaming

API

The plan endpoint streams events as newline-delimited JSON so the UI can show each agent's progress in real time. Each event carries the agent name, status, and output. The stream closes after all three agents complete or any agent fails.

POST /api/plan → ReadableStream<NDJSON>

Caching & Performance

Upstash Redis Campaign Cache

Redis

Campaign reads (state, vote counts, creator, template) are cached in Upstash Redis with a 30-second TTL. Cache is invalidated on successful relay or lifecycle actions so counts update promptly after participation.

View Snapshot Cache

Redis

The explore, profile, and leaderboard pages write their full computed responses to Redis as view snapshots. Subsequent requests within the TTL window are served directly from cache without re-reading contracts, keeping public pages fast under load.

Optimistic UI Updates

Next.js

After relay success, vote counts and participation state are updated locally in React state immediately, before the cache refreshes. This makes the UI responsive without waiting for a round-trip to the chain.

Expansion Roadmap

Template-Backed Expansion

Roadmap

ChainForge AI expands by adding vetted on-chain templates with known input schemas, security constraints, and proof requirements. The AI maps plain-English prompts into those bounded templates instead of generating arbitrary Solidity directly.

Phase 1: Builder Depth

Live

The next mergeable work is deeper builder intelligence: template-fit rationale, confidence, reveal strategy, editable review fields, Auditor suggestions, and duplicate warnings before deployment.

Phase 1: 10 Template Set

Live

Phase 1 is complete and live: ChainForge AI now has contracts, tests, ABIs, planner support, deployment encoding, relay actions, campaign reads, and WireFluid registrations for seven expansion templates: Quiz, Raffle, Bounty, Fan Pass, Points Pool, Tournament, and Auction.

Phase 2: Capability Registry

Experimental

The high-risk expansion path is a capability registry for more app categories such as fan privilege auctions, access passes, points prediction pools, multi-step prompt building, and bounded generated frontend variants.

Contract Tests

262 / 262 tests passing

Hardhat

Full Hardhat test suite covering CampaignFactory, current V2 public templates, V1 legacy validation contracts, and seven Phase 1 expansion templates: Quiz, Raffle, Bounty, Fan Pass, Points Pool, Tournament, and Auction.

npm run test:contracts

Relay-Friendly Signatures Tested

Hardhat

All V2 test suites pass voter addresses explicitly, matching the relay model. Tests cover single-vote enforcement, wrong-voter rejection, and lifecycle gate violations.

Tech Stack

Frontend

App

Next.js 14 App Router, TypeScript, Tailwind CSS

Web3 Client

App

viem + wagmi v2, typed contract reads, wallet connect, signature verification

Contracts

Contracts

Solidity, Hardhat, OpenZeppelin (Initializable, access control patterns)

AI

AI

OpenAI GPT-4o with structured outputs, Architect and Auditor agents

Cache

Infra

Upstash Redis REST API, campaign state cache and view snapshots

Hosting

Infra

Vercel, edge functions for API routes, automatic preview deployments

Network

Chain

WireFluid EVM, Chain ID 92533, 4s finality, 100% EVM compatible