Skip to content

Eras256/DeFiGuard-IA

Repository files navigation

πŸ›‘οΈ DeFiGuard AI - AI-Powered Smart Contract Security Auditor

DeFiGuard AI License Base Sepolia Verified

πŸš€ Overview

DeFiGuard AI is an advanced AI-powered smart contract security auditor that leverages Gemini 2.5 Flash and Model Context Protocol (MCP) architecture to identify vulnerabilities in Solidity contracts within seconds.

✨ Key Features

  • πŸ€– AI-Powered Analysis - Gemini 2.5 Flash with 2M token context window
  • ⚑ Lightning Fast - Complete audits in under 30 seconds
  • 🌐 EVM-Compatible Analysis - Analyze contracts from any EVM-compatible chain. On-chain registration on Base Sepolia
  • πŸ”§ Automated Fixes - AI-generated secure code patches
  • πŸ“Š Risk Scoring - Comprehensive security analysis with severity levels
  • πŸ† NFT Certification - On-chain verification badges for audited contracts
  • πŸ“‘ Real-Time Monitoring - Continuous surveillance of deployed contracts
  • 🧠 MCP Architecture - Model Context Protocol for enhanced security analysis
  • πŸ” Decentralized Minting - Contract owners mint badges directly from their wallets

πŸ› οΈ Tech Stack

Frontend

  • Next.js 14 - React framework with App Router
  • TypeScript - Type-safe development
  • Tailwind CSS - Utility-first styling with glassmorphism
  • Framer Motion - Advanced animations
  • Thirdweb - Wallet connection and blockchain infrastructure

AI & Agents

  • Gemini 2.5 Flash - Google's latest AI model
  • MCP Architecture - Model Context Protocol implementation
  • Custom MCP Servers - Slither, Blockchain Data, DeFi Analytics
  • AI Agents - AdvancedAuditorAgent, RiskAgent, RemediationAgent

Blockchain

  • Thirdweb SDK - Multi-chain infrastructure
  • Thirdweb React - Wallet connection and blockchain hooks
  • Viem - TypeScript Ethereum library
  • Base Sepolia - Primary deployment network

Smart Contracts

  • Solidity 0.8.20 - Smart contract language
  • OpenZeppelin - Secure contract libraries
  • Hardhat - Development environment

πŸ“¦ Installation

Prerequisites

Setup Steps

# Clone repository
git clone https://github.com/yourusername/defiguard-ai.git
cd defiguard-ai

# Install dependencies with pnpm
pnpm install

# Copy environment variables
cp .env.example .env.local

# Edit .env.local with your API keys
nano .env.local

# Run development server
pnpm dev

Visit http://localhost:3000 to see the application.

πŸ”‘ Environment Variables

Create .env.local file with:

# AI Model
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-2.5-flash-latest

# Blockchain
NEXT_PUBLIC_THIRDWEB_CLIENT_ID=your_thirdweb_client_id
THIRDWEB_SECRET_KEY=your_secret_key

# Contract Addresses (Updated December 2025)
NEXT_PUBLIC_AUDIT_REGISTRY_ADDRESS=0x6D3d5487c41E1759b5457f5C29f8d41caC51a8eF
NEXT_PUBLIC_GUARD_NFT_ADDRESS=0xE429b1AFD7BDd12ceDB69777538f5925CB6CeF52
NEXT_PUBLIC_GUARD_TOKEN_ADDRESS=0xBc3035ed036B280AdB9A6Ad19a46089E39e2eDED

# RPC Endpoints
NEXT_PUBLIC_BASE_SEPOLIA_RPC=https://sepolia.base.org
NEXT_PUBLIC_ARBITRUM_SEPOLIA_RPC=https://sepolia-rollup.arbitrum.io/rpc

# Explorer APIs (for contract verification)
BASESCAN_API_KEY=your_basescan_key
ARBISCAN_API_KEY=your_arbiscan_key
ETHERSCAN_API_KEY=your_etherscan_key

# Deployment (only needed for deploying contracts)
DEPLOYER_PRIVATE_KEY=0xyour_deployer_private_key_here

# Note: GUARD_NFT_OWNER_PRIVATE_KEY is NO LONGER NEEDED
# Users now mint badges directly from their wallets!

πŸš€ Deployment

Deploy to Vercel

# Login to Vercel
pnpm vercel login

# Deploy to production
pnpm deploy

Deploy to Production

Deploy your application to your preferred hosting platform (Vercel, Netlify, etc.) for production use.

Deploy Smart Contracts

# Ensure you have testnet ETH and DEPLOYER_PRIVATE_KEY in .env.local
# Deploy all contracts to Base Sepolia
pnpm deploy:contracts

# Redeploy only updated contracts (AuditRegistry & GuardNFT)
pnpm redeploy:updated

# Verify contracts (after deployment)
# Requires BASESCAN_API_KEY in .env.local
pnpm verify:updated

βœ… Latest Deployed and Verified Contracts (Updated December 2025):

πŸ†• Key Updates (December 2025):

  • Decentralized Badge Minting: Contract owners can now mint certification badges directly from their wallets
  • No Server Required: Users no longer need server-side private keys to mint badges
  • contractOwner Mapping: AuditRegistry now tracks who registered each audit for ownership verification

πŸ—οΈ System Architecture

Complete Architecture Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                    CLIENT LAYER                                         β”‚
β”‚                              (Browser / Next.js Frontend)                               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚   Home Page  β”‚  β”‚  Audit Page  β”‚  β”‚  Dashboard   β”‚  β”‚  Monitoring  β”‚              β”‚
β”‚  β”‚  (Landing)   β”‚  β”‚  (Analysis)  β”‚  β”‚  (Stats)     β”‚  β”‚  (Alerts)    β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚         β”‚                  β”‚                  β”‚                  β”‚                        β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                        β”‚
β”‚                              β”‚                                                             β”‚
β”‚                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                 β”‚
β”‚                    β”‚   Shared Components β”‚                                                 β”‚
β”‚                    β”‚  - Navbar/Footer   β”‚                                                 β”‚
β”‚                    β”‚  - UI Components   β”‚                                                 β”‚
β”‚                    β”‚  - Chatbot         β”‚                                                 β”‚
β”‚                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                 β”‚
β”‚                              β”‚                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ HTTP Requests
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                 API ROUTES LAYER                                         β”‚
β”‚                              (Next.js API Routes)                                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚  β”‚  /api/analyze    β”‚  β”‚ /api/record-auditβ”‚  β”‚  /api/mint-badge β”‚                      β”‚
β”‚  β”‚  (POST)         β”‚  β”‚  (POST)          β”‚  β”‚  (POST)          β”‚                      β”‚
β”‚  β”‚                  β”‚  β”‚                  β”‚  β”‚                  β”‚                      β”‚
β”‚  β”‚  - Validates     β”‚  β”‚  - Prepares      β”‚  β”‚  - Checks        β”‚                      β”‚
β”‚  β”‚    contract code β”‚  β”‚    transaction   β”‚  β”‚    certification β”‚                      β”‚
β”‚  β”‚  - Calls         β”‚  β”‚  - Uses          β”‚  β”‚  - Mints NFT     β”‚                      β”‚
β”‚  β”‚    AdvancedAgent β”‚  β”‚    Thirdweb SDK  β”‚  β”‚    badge         β”‚                      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚           β”‚                      β”‚                      β”‚                                  β”‚
β”‚           β”‚                      β”‚                      β”‚                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚  β”‚              /api/chat (Chatbot API)                         β”‚                       β”‚
β”‚  β”‚              /api/gemini (Gemini Proxy)                     β”‚                       β”‚
β”‚  β”‚              /api/transactions (TX History)                   β”‚                       β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ Function Calls
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                            AI AGENTS LAYER                                               β”‚
β”‚                    (MCP Architecture)                                                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚              AdvancedAuditorAgent (Main Agent)                       β”‚              β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚              β”‚
β”‚  β”‚  β”‚  analyzeContract(code, contractAddress?)                      β”‚  β”‚              β”‚
β”‚  β”‚  β”‚  1. Calls MCP Servers in parallel                            β”‚  β”‚              β”‚
β”‚  β”‚  β”‚  2. Builds enriched context                                   β”‚  β”‚              β”‚
β”‚  β”‚  β”‚  3. Sends to Gemini 2.5 Flash via AI SDK                     β”‚  β”‚              β”‚
β”‚  β”‚  β”‚  4. Returns VulnerabilityAnalysis                             β”‚  β”‚              β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                              β”‚                                                           β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                     β”‚
β”‚         β”‚                    β”‚                    β”‚                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”                            β”‚
β”‚  β”‚ RiskAgent   β”‚    β”‚ Remediation   β”‚    β”‚ AuditorAgent β”‚                            β”‚
β”‚  β”‚             β”‚    β”‚ Agent         β”‚    β”‚ (Legacy)     β”‚                            β”‚
β”‚  β”‚ - Calculatesβ”‚    β”‚               β”‚    β”‚              β”‚                            β”‚
β”‚  β”‚   risk scoreβ”‚    β”‚ - Generates   β”‚    β”‚ - Basic      β”‚                            β”‚
β”‚  β”‚ - Classifiesβ”‚    β”‚   secure fixesβ”‚    β”‚   analysis   β”‚                            β”‚
β”‚  β”‚   risk levelβ”‚    β”‚ - Code patchesβ”‚    β”‚              β”‚                            β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                            β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ MCP Protocol Calls
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          MCP SERVERS LAYER                                               β”‚
β”‚                    (Model Context Protocol)                                              β”‚
β”‚                    Configured via mcp.json                                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚   SlitherMCP         β”‚  β”‚   BlockchainMCP       β”‚  β”‚   DeFiDataMCP       β”‚        β”‚
β”‚  β”‚   (slither-analyzer) β”‚  β”‚   (blockchain-data)   β”‚  β”‚   (defi-data)        β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚                      β”‚  β”‚                      β”‚        β”‚
β”‚  β”‚  Methods:           β”‚  β”‚  Methods:            β”‚  β”‚  Methods:           β”‚        β”‚
β”‚  β”‚  - analyze()        β”‚  β”‚  - getContractInfo() β”‚  β”‚  - getExploitHistory()β”‚        β”‚
β”‚  β”‚  - getVulnerability  β”‚  β”‚  - getTransaction     β”‚  β”‚  - getHistorical     β”‚        β”‚
β”‚  β”‚    Patterns()       β”‚  β”‚    History()          β”‚  β”‚    Exploits()        β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚                      β”‚  β”‚  - getProtocolTVL()  β”‚        β”‚
β”‚  β”‚  Output:            β”‚  β”‚  Output:             β”‚  β”‚  - getSecurityRating β”‚        β”‚
β”‚  β”‚  - Static analysis   β”‚  β”‚  - Contract info     β”‚  β”‚    ()                β”‚        β”‚
β”‚  β”‚  - Vulnerability     β”‚  β”‚  - TX history        β”‚  β”‚                      β”‚        β”‚
β”‚  β”‚    patterns          β”‚  β”‚  - On-chain data     β”‚  β”‚  Output:             β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚                      β”‚  β”‚  - Historical        β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚  Integrates:         β”‚  β”‚    exploits          β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚  - Thirdweb SDK      β”‚  β”‚  - DeFi protocol     β”‚        β”‚
β”‚  β”‚                      β”‚  β”‚  - Basescan API     β”‚  β”‚    data               β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                                                                                           β”‚
β”‚  All MCP servers called in parallel via Promise.allSettled()                             β”‚
β”‚  Context aggregated and injected into Gemini prompt                                      β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ API Calls
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          AI/ML LAYER                                                      β”‚
β”‚                    (Google Gemini 2.5 Flash)                                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚                    Gemini API Client                                 β”‚              β”‚
β”‚  β”‚  (lib/gemini/client.ts)                                              β”‚              β”‚
β”‚  β”‚                                                                       β”‚              β”‚
β”‚  β”‚  - analyzeContractWithGemini()                                       β”‚              β”‚
β”‚  β”‚  - generateRemediationCode()                                         β”‚              β”‚
β”‚  β”‚  - Multi-model fallback system:                                      β”‚              β”‚
β”‚  β”‚    1. gemini-2.5-flash (primary)                                    β”‚              β”‚
β”‚  β”‚    2. gemini-2.5-pro (fallback)                                     β”‚              β”‚
β”‚  β”‚    3. gemini-2.0-flash (fallback)                                   β”‚              β”‚
β”‚  β”‚    4. gemini-1.5-flash (fallback)                                   β”‚              β”‚
β”‚  β”‚    5. gemini-1.5-pro (fallback)                                     β”‚              β”‚
β”‚  β”‚                                                                       β”‚              β”‚
β”‚  β”‚  Input: Contract code + MCP context                                   β”‚              β”‚
β”‚  β”‚  Output: VulnerabilityAnalysis JSON                                  β”‚              β”‚
β”‚  β”‚    - vulnerabilities[] (type, severity, line, description, fix)      β”‚              β”‚
β”‚  β”‚    - riskScore (0-100)                                               β”‚              β”‚
β”‚  β”‚    - gasOptimizations[]                                              β”‚              β”‚
β”‚  β”‚    - bestPractices[]                                                 β”‚              β”‚
β”‚  β”‚    - summary                                                          β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ Blockchain Calls
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        BLOCKCHAIN LAYER                                                   β”‚
β”‚                    (Base Sepolia Network)                                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚                    Thirdweb SDK                                     β”‚              β”‚
β”‚  β”‚  - Wallet connection (ConnectButton)                                  β”‚              β”‚
β”‚  β”‚  - Contract interaction                                               β”‚              β”‚
β”‚  β”‚  - Transaction preparation                                           β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                              β”‚                                                           β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                     β”‚
β”‚         β”‚                    β”‚                    β”‚                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”                            β”‚
β”‚  β”‚ AuditRegistryβ”‚    β”‚   GuardNFT   β”‚    β”‚  GuardToken  β”‚                            β”‚
β”‚  β”‚              β”‚    β”‚   (ERC-721)  β”‚    β”‚   (ERC-20)   β”‚                            β”‚
β”‚  β”‚              β”‚    β”‚              β”‚    β”‚              β”‚                            β”‚
β”‚  β”‚ Functions:   β”‚    β”‚ Functions:   β”‚    β”‚ Functions:   β”‚                            β”‚
β”‚  β”‚ - recordAuditβ”‚    β”‚ - mintBadge() β”‚    β”‚ - claimAirdropβ”‚                            β”‚
β”‚  β”‚ - getAllAuditsβ”‚   β”‚ - getBadgeInfoβ”‚   β”‚ - mintReward β”‚                            β”‚
β”‚  β”‚ - checkCert  β”‚    β”‚ - isCertified β”‚   β”‚ - batchMint  β”‚                            β”‚
β”‚  β”‚ - contractOwnerβ”‚ β”‚              β”‚    β”‚              β”‚                            β”‚
β”‚  β”‚              β”‚    β”‚              β”‚    β”‚              β”‚                            β”‚
β”‚  β”‚ Address:     β”‚    β”‚ Address:     β”‚    β”‚ Address:     β”‚                            β”‚
β”‚  β”‚ 0x6D3d...8eF β”‚    β”‚ 0xE429...F52 β”‚    β”‚ 0xBc30...DED β”‚                            β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                            β”‚
β”‚                                                                                           β”‚
β”‚  All contracts verified on Basescan & Sourcify                                            β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ External API Calls
                               β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        EXTERNAL SERVICES                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚ Basescan API β”‚  β”‚ DefiLlama API β”‚  β”‚ Google Geminiβ”‚  β”‚ Thirdweb     β”‚              β”‚
β”‚  β”‚              β”‚  β”‚               β”‚  β”‚ API          β”‚  β”‚ Infrastructureβ”‚              β”‚
β”‚  β”‚ - Contract   β”‚  β”‚ - Protocol TVL β”‚  β”‚ - AI Analysis β”‚  β”‚ - RPC Nodes  β”‚              β”‚
β”‚  β”‚   verificationβ”‚  β”‚ - DeFi data   β”‚  β”‚ - Code gen    β”‚  β”‚ - Indexing   β”‚              β”‚
β”‚  β”‚ - TX history β”‚  β”‚               β”‚  β”‚               β”‚  β”‚              β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              DATA FLOW EXAMPLE                                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                                           β”‚
β”‚  1. User uploads contract code β†’ Frontend (Audit Page)                                    β”‚
β”‚  2. Frontend β†’ POST /api/analyze { code, contractAddress? }                              β”‚
β”‚  3. API Route β†’ AdvancedAuditorAgent.analyzeContract()                                  β”‚
β”‚  4. AdvancedAuditorAgent calls MCP servers in parallel:                                  β”‚
β”‚     β”œβ”€ SlitherMCP.analyze() β†’ Static analysis                                            β”‚
β”‚     β”œβ”€ DeFiDataMCP.getHistoricalExploits() β†’ Exploit correlation                        β”‚
β”‚     └─ BlockchainMCP.getContractInfo() β†’ On-chain data (if address provided)            β”‚
β”‚  5. MCP context aggregated β†’ buildAnalysisPrompt()                                      β”‚
β”‚  6. Prompt sent to Gemini 2.5 Flash via AI SDK                                           β”‚
β”‚  7. Gemini returns VulnerabilityAnalysis JSON                                            β”‚
β”‚  8. Response sent back to Frontend                                                       β”‚
β”‚  9. User clicks "Record Audit" β†’ POST /api/record-audit                                 β”‚
β”‚  10. API prepares transaction β†’ Thirdweb SDK β†’ AuditRegistry.recordAudit()              β”‚
β”‚  11. If riskScore < 40 β†’ Contract certified β†’ User can mint NFT badge                 β”‚
β”‚  12. User clicks "Mint Badge" β†’ POST /api/mint-badge                                   β”‚
β”‚  13. API checks certification β†’ GuardNFT.mintBadge() β†’ NFT minted                       β”‚
β”‚                                                                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Architecture Components Breakdown

1. Client Layer (Frontend)

  • Framework: Next.js 14 with App Router
  • Pages: Home, Audit, Dashboard, Monitoring
  • Components: Modular React components with Tailwind CSS
  • State Management: React hooks (useState, useEffect, custom hooks)
  • Wallet Integration: Thirdweb ConnectButton

2. API Routes Layer

  • Framework: Next.js API Routes (Node.js runtime)
  • Endpoints:
    • /api/analyze - Contract analysis with MCP integration
    • /api/record-audit - On-chain audit registration
    • /api/mint-badge - NFT badge minting
    • /api/chat - AI chatbot for security questions
    • /api/gemini - Direct Gemini API proxy
    • /api/transactions - Transaction history

3. AI Agents Layer (MCP Architecture)

  • AdvancedAuditorAgent: Main agent orchestrating MCP servers and AI analysis
  • RiskAgent: Calculates risk scores (0-100) and classifies risk levels
  • RemediationAgent: Generates secure code fixes for vulnerabilities
  • AuditorAgent: Legacy agent for basic analysis

4. MCP Servers Layer (Model Context Protocol)

  • Configuration: mcp.json following Model Context Protocol specification
  • SlitherMCP: Static code analysis, vulnerability pattern detection
  • BlockchainMCP: On-chain data fetching via Thirdweb SDK and Basescan API
  • DeFiDataMCP: Historical exploit correlation and DeFi protocol data
  • Execution: Parallel execution via Promise.allSettled() for fault tolerance
How MCP Architecture Works

The Model Context Protocol (MCP) architecture is the core innovation that makes DeFiGuard AI's analysis so powerful. Here's how it works:

1. Parallel MCP Server Execution When analyzing a contract, the AdvancedAuditorAgent simultaneously calls three specialized MCP servers:

  • SlitherMCP Server (lib/mcp/slither-mcp.ts):

    • Performs static code analysis on the Solidity contract
    • Detects known vulnerability patterns (reentrancy, overflow, access control issues)
    • Analyzes code structure and identifies potential security weaknesses
    • Returns structured vulnerability data with line numbers and severity levels
  • BlockchainMCP Server (lib/mcp/blockchain-mcp.ts):

    • Fetches on-chain data when a contract address is provided
    • Retrieves transaction history and contract verification status
    • Analyzes contract interactions and token holder information
    • Provides real-world context about the contract's deployment and usage
  • DeFiDataMCP Server (lib/mcp/defi-data-mcp.ts):

    • Correlates contract patterns with historical exploits
    • Matches vulnerabilities to real-world hacks (DAO Hack 2016, Parity Wallet, etc.)
    • Provides exploit scenarios based on similar contracts that were compromised
    • Enhances AI understanding with DeFi protocol security data

2. Context Aggregation All MCP server responses are aggregated into a rich context string that includes:

  • Static analysis results from Slither
  • On-chain data and transaction patterns
  • Historical exploit correlations
  • Vulnerability pattern matches

3. Enhanced AI Analysis The aggregated MCP context is injected into the Gemini AI prompt, providing the model with:

  • Multi-dimensional analysis: Not just code review, but real-world context
  • Historical knowledge: Understanding of how similar vulnerabilities were exploited
  • On-chain validation: Verification of contract behavior in production
  • Pattern recognition: Detection of vulnerabilities that static analysis alone might miss

4. Fault Tolerance The system uses Promise.allSettled() to ensure that if one MCP server fails, the analysis continues with data from the other servers. This makes the system resilient and ensures partial failures don't break the entire analysis.

Example Flow:

User submits contract β†’ AdvancedAuditorAgent.analyzeContract()
  ↓
Parallel execution:
  β”œβ”€ SlitherMCP.analyze() β†’ Static analysis results
  β”œβ”€ DeFiDataMCP.getHistoricalExploits() β†’ Exploit correlations
  └─ BlockchainMCP.getContractInfo() β†’ On-chain data (if address provided)
  ↓
Context aggregation β†’ buildMCPContext()
  ↓
Enhanced prompt with MCP data β†’ Gemini AI
  ↓
Comprehensive vulnerability analysis with real-world context

This architecture combines the best of:

  • Static Analysis (Slither) for pattern detection
  • On-Chain Intelligence (Blockchain) for real-world validation
  • Historical Knowledge (DeFi) for exploit correlation
  • AI Reasoning (Gemini) for comprehensive understanding

The result is a security analysis that's not just code review, but a comprehensive security assessment that understands both the code and its real-world implications.

5. AI/ML Layer

  • Provider: Google Gemini 2.5 Flash (primary)
  • Fallback Chain: Multi-model fallback system for reliability
  • Integration: AI SDK (@ai-sdk/google) for streaming and error handling
  • Context Window: 2M tokens for large contract analysis
  • Output Format: Structured JSON (VulnerabilityAnalysis)

6. Blockchain Layer

  • Network: Base Sepolia (Chain ID: 84532)
  • SDK: Thirdweb SDK v5 for contract interaction
  • Smart Contracts:
    • AuditRegistry: On-chain audit registry with contractOwner mapping
    • GuardNFT: ERC-721 certification badges (decentralized minting)
    • GuardToken: ERC-20 rewards token (1B supply)
  • Verification: All contracts verified on Basescan & Sourcify

7. External Services

  • Basescan API: Contract verification and transaction history
  • DefiLlama API: DeFi protocol TVL and data
  • Google Gemini API: AI model inference
  • Thirdweb Infrastructure: RPC nodes and blockchain indexing

Key Architectural Patterns

  1. MCP Architecture: Model Context Protocol for agent-server communication
  2. Parallel Processing: MCP servers called in parallel for performance
  3. Fault Tolerance: Promise.allSettled() ensures partial failures don't break analysis
  4. Multi-Model Fallback: Automatic fallback to alternative Gemini models
  5. Decentralized Minting: Users mint badges directly from wallets (no server dependency)
  6. On-Chain Registry: Immutable audit records stored on blockchain
  7. Type Safety: Full TypeScript coverage across all layers

πŸ“Š Project Structure

defiguard-ai/
β”œβ”€β”€ app/                    # Next.js App Router pages
β”‚   β”œβ”€β”€ audit/              # Contract auditing interface
β”‚   β”œβ”€β”€ dashboard/           # User dashboard
β”‚   β”œβ”€β”€ monitoring/          # Real-time monitoring
β”‚   └── api/                # API routes
β”œβ”€β”€ components/             # React components
β”‚   β”œβ”€β”€ ui/                 # Base UI components
β”‚   β”œβ”€β”€ layout/             # Layout components
β”‚   β”œβ”€β”€ home/               # Landing page sections
β”‚   β”œβ”€β”€ audit/               # Audit-specific components
β”‚   └── dashboard/          # Dashboard widgets
β”œβ”€β”€ lib/                    # Core logic
β”‚   β”œβ”€β”€ agents/             # AI agents
β”‚   β”œβ”€β”€ mcp/                # MCP server implementations
β”‚   β”œβ”€β”€ gemini/             # Gemini API client
β”‚   └── thirdweb/           # Blockchain utilities
β”œβ”€β”€ contracts/              # Solidity smart contracts
β”œβ”€β”€ public/                 # Static assets
└── .env.local              # Environment variables

πŸ§ͺ Testing

Run Contract Tests

The project includes comprehensive test suites for all smart contracts:

# Run all contract tests
pnpm test:contracts

Test Coverage:

  • βœ… AuditRegistry.test.ts - Tests for audit recording, certification, circular buffer DoS protection
  • βœ… GuardNFT.test.ts - Tests for badge minting, query functions, URI updates
  • βœ… GuardToken.test.ts - Tests for airdrop claims, reward minting, batch operations

Execute Test Transactions on Base Sepolia

Run real transactions on Base Sepolia testnet to verify contract functionality:

# Execute test transactions for all contracts (requires DEPLOYER_PRIVATE_KEY in .env.local)
pnpm test:transactions

# Execute test transactions for updated contracts only (AuditRegistry & GuardNFT)
pnpm test:updated

What it does:

  • Executes 4 transactions per contract (AuditRegistry, GuardNFT, GuardToken)
  • Records audits with different risk scores (15, 25, 30, 35)
  • Mints NFT badges for certified contracts using decentralized minting
  • Tests reward minting (single and batch operations)
  • All transactions are verifiable on Basescan

Transaction Results:

  • βœ… AuditRegistry: 4 audits recorded successfully (~236,331 gas each)
  • βœ… GuardToken: 4 reward transactions successful (~57,458-88,639 gas)
  • βœ… GuardNFT: Badge minting works with owner-based verification (~251,974 gas)

Gas Usage Statistics:

  • AuditRegistry.recordAudit: ~236,331 gas per transaction
  • GuardToken.mintReward: ~57,458 gas per transaction
  • GuardToken.batchMintRewards (2 recipients): ~88,639 gas
  • GuardToken.batchMintRewards (4 recipients): ~78,181 gas
  • GuardNFT.mintBadge: ~251,974 gas per transaction

Updated Contracts Test Results (December 2025):

AuditRegistry Tests:

  • βœ… 4 audit recordings successful
  • βœ… All contracts certified automatically (risk score < 40)
  • βœ… contractOwner mapping working correctly
  • βœ… Gas usage: ~236,331 gas per audit

GuardNFT Tests:

  • βœ… Decentralized badge minting working correctly
  • βœ… Owner verification working (only contract owner can mint)
  • βœ… Certification verification working
  • βœ… Gas usage: ~251,974 gas per badge mint

Sample Test Transactions:

AuditRegistry:

  1. Contract 1 (Gold) - Risk Score: 15

  2. Contract 2 (Bronze) - Risk Score: 25

  3. Contract 3 (Bronze) - Risk Score: 30

  4. Contract 4 (Bronze) - Risk Score: 35

GuardNFT:

View Transactions on Basescan:

Verify Transactions

Verify the executed test transactions on Base Sepolia:

# Verify transactions using Basescan API
pnpm verify:transactions

# Verify updated contracts on Basescan
pnpm verify:updated

Verification Status: βœ… All transactions verified successfully

  • βœ… AuditRegistry recordAudit transactions confirmed
  • βœ… GuardToken transactions confirmed (single mints + batch mints)
  • βœ… GuardNFT badge minting transactions confirmed (decentralized minting working)
  • βœ… All transactions visible and verifiable on Basescan
  • βœ… Contracts verified on Basescan and Sourcify

For detailed verification report, see TRANSACTION_VERIFICATION.md.

πŸ§ͺ Usage Examples

Analyze a Contract

import { auditorAgent } from "@/lib/agents/auditor-agent";

const code = `pragma solidity ^0.8.0; contract MyContract { // Your code here }`;

const analysis = await auditorAgent.analyzeContract(code);
console.log(`Risk Score: ${analysis.riskScore}`);
console.log(`Vulnerabilities: ${analysis.vulnerabilities.length}`);

Use Gemini 2.5 Flash Directly

import { analyzeContractWithGemini } from "@/lib/gemini/client";

const result = await analyzeContractWithGemini(contractCode);

Mint Certification Badge (Decentralized)

import { mintBadgeForContract } from "@/lib/contracts/mint-badge";

// User mints badge directly from their wallet
const txHash = await mintBadgeForContract(
  contractAddress,
  userAddress,
  riskScore,
  userAccount
);

πŸ€– AI Agents & MCP Architecture

🎯 AI Agents Implemented

  1. AdvancedAuditorAgent - Analyzes smart contracts for vulnerabilities using Gemini AI with MCP integration
  2. RiskAgent - Calculates comprehensive risk scores
  3. RemediationAgent - Generates secure code fixes

πŸ”Œ MCP Servers Implemented

DeFiGuard AI leverages the Model Context Protocol (MCP) architecture to provide multi-dimensional security analysis. The system integrates three specialized MCP servers that work in parallel to enhance AI-powered contract analysis:

1. SlitherMCP Server (lib/mcp/slither-mcp.ts)

  • Purpose: Static code analysis and vulnerability pattern detection
  • Capabilities:
    • Analyzes Solidity code structure and syntax
    • Detects known vulnerability patterns (reentrancy, overflow, access control)
    • Identifies code quality issues and gas optimization opportunities
    • Provides line-by-line vulnerability mapping
  • Integration: Called automatically for every contract analysis
  • Output: Structured vulnerability data with severity levels and recommendations

2. BlockchainMCP Server (lib/mcp/blockchain-mcp.ts)

  • Purpose: On-chain data fetching and transaction analysis
  • Capabilities:
    • Retrieves contract verification status from blockchain explorers
    • Fetches transaction history and interaction patterns
    • Analyzes token holder distribution and contract usage
    • Validates contract behavior in production environment
  • Integration: Called when a contract address is provided (optional)
  • Output: Real-world contract usage data and on-chain validation

3. DeFiDataMCP Server (lib/mcp/defi-data-mcp.ts)

  • Purpose: Historical exploit correlation and DeFi security intelligence
  • Capabilities:
    • Correlates contract patterns with historical exploits
    • Matches vulnerabilities to real-world hacks (DAO Hack 2016, Parity Wallet, etc.)
    • Provides exploit scenarios based on similar compromised contracts
    • Enhances AI understanding with DeFi protocol security data
  • Integration: Called automatically for every contract analysis
  • Output: Historical exploit correlations and real-world attack scenarios

πŸš€ How MCP Architecture Enhances AI Analysis

The power of DeFiGuard AI comes from the seamless integration of MCP servers with Gemini AI:

Step 1: Parallel Data Collection

Contract Code Submitted
    ↓
AdvancedAuditorAgent triggers parallel MCP calls:
    β”œβ”€ SlitherMCP β†’ Static analysis results
    β”œβ”€ DeFiDataMCP β†’ Historical exploit data
    └─ BlockchainMCP β†’ On-chain data (if address provided)

Step 2: Context Enrichment All MCP server responses are aggregated into a comprehensive context that includes:

  • Static analysis findings (vulnerability patterns, code quality)
  • Historical exploit correlations (similar vulnerabilities that were exploited)
  • On-chain validation (real-world contract behavior and usage)

Step 3: Enhanced AI Prompt The enriched context is injected into the Gemini AI prompt, providing:

  • Multi-dimensional understanding: Not just code review, but real-world context
  • Historical knowledge: Understanding of how similar vulnerabilities were exploited
  • Pattern recognition: Detection of vulnerabilities that static analysis alone might miss
  • Contextual recommendations: Fixes based on proven solutions from similar cases

Step 4: Comprehensive Analysis Gemini AI processes the enriched context and generates:

  • Detailed vulnerability reports with exploit scenarios
  • Risk scores based on both code analysis and historical data
  • Secure code fixes informed by real-world exploit patterns
  • Best practices recommendations validated by on-chain data

Benefits of MCP Architecture:

  • βœ… Fault Tolerance: Uses Promise.allSettled() - partial failures don't break analysis
  • βœ… Performance: Parallel execution reduces analysis time
  • βœ… Accuracy: Multi-dimensional analysis catches more vulnerabilities
  • βœ… Context: Real-world data enhances AI understanding
  • βœ… Scalability: Easy to add new MCP servers for additional capabilities

βœ… MCP Configuration: This project implements Model Context Protocol (MCP) with mcp.json configuration file for seamless MCP server integration.

⛓️ Web3 Integration

  • AuditRegistry.sol - On-chain audit registry with contractOwner mapping
  • GuardNFT.sol - Certification NFTs for audited contracts (decentralized minting)
  • GuardToken.sol - ERC-20 token for rewards
  • Network - Currently deployed on Base Sepolia (EVM-compatible contract analysis supported)
  • Wallet Integration - Thirdweb ConnectButton

πŸ“ Deployed Contracts on Base Sepolia (Updated December 2025)

Contract Address Basescan Sourcify
AuditRegistry 0x6D3d5487c41E1759b5457f5C29f8d41caC51a8eF βœ… Verified βœ… Verified
GuardNFT 0xE429b1AFD7BDd12ceDB69777538f5925CB6CeF52 βœ… Verified βœ… Verified
GuardToken 0xBc3035ed036B280AdB9A6Ad19a46089E39e2eDED βœ… Verified βœ… Verified

Network: Base Sepolia (Chain ID: 84532)
Deployer: 0xF93F07b1b35b9DF13e2d53DbAd49396f0A9538D9

πŸ’‘ Note: βœ… All contracts are verified on Basescan and Sourcify, available for public inspection. Contracts use OpenZeppelin v5.0.2 and follow security best practices as of December 2025.

πŸ†• December 2025 Updates:

  • Decentralized Badge Minting: Contract owners can now mint certification badges directly from their wallets
  • No Server Required: Users mint badges without needing server-side private keys
  • contractOwner Mapping: AuditRegistry tracks who registered each audit for ownership verification

πŸ”’ Security Improvements Implemented (December 2025)

AuditRegistry:

  • βœ… Circular buffer O(1) to prevent DoS in write operations
  • βœ… Explicit limit on read functions (50 active audits)
  • βœ… Custom errors for gas optimization
  • βœ… Complete protection against DoS attacks
  • βœ… contractOwner mapping for decentralized badge minting

GuardNFT:

  • βœ… CEI pattern (Checks-Effects-Interactions) implemented
  • βœ… Reentrancy protection
  • βœ… Enhanced security documentation
  • βœ… Decentralized minting: Only contract owners can mint their certification badges
  • βœ… Ownership verification: Uses AuditRegistry's contractOwner mapping

GuardToken:

  • βœ… Batch size limit (100 recipients) to prevent DoS
  • βœ… Custom errors implemented
  • βœ… Protection in batch operations

πŸ” How Decentralized Badge Minting Works

  1. User Registers Audit: When a user records an audit for their contract, their wallet address is stored as contractOwner in AuditRegistry
  2. Contract Gets Certified: If risk score < 40, the contract is automatically certified
  3. User Mints Badge: The same user (contract owner) can now mint the certification badge directly from their wallet
  4. Verification: GuardNFT contract verifies:
    • The caller is the contractOwner of the audited contract
    • The contract is certified (risk score < 40)
    • No badge already exists for this contract

Benefits:

  • βœ… Fully decentralized - no server dependency
  • βœ… Users control their own badges
  • βœ… More secure - no single point of failure
  • βœ… True Web3 experience

πŸ“Š Test Coverage

Unit Tests:

  • βœ… Comprehensive test suites for all three contracts
  • βœ… Tests cover deployment, core functionality, edge cases, and security features
  • βœ… Tests use Hardhat and Chai for assertions

Integration Tests:

  • βœ… Real transactions executed on Base Sepolia testnet
  • βœ… 4 transactions per contract for verification
  • βœ… All transactions verifiable on Basescan
  • βœ… Gas usage tracked and documented

Test Files:

  • test/AuditRegistry.test.ts - 15+ test cases
  • test/GuardNFT.test.ts - 12+ test cases
  • test/GuardToken.test.ts - 18+ test cases

Transaction Verification:

  • βœ… All test transactions verified on Base Sepolia
  • βœ… Script available: pnpm verify:transactions
  • βœ… Detailed report: TRANSACTION_VERIFICATION.md
  • βœ… Total gas used: ~1,037,000 gas across all transactions

Demo Video

Watch Demo Video

Live Deployment

πŸ”’ Security Features

Vulnerability Detection

  • Reentrancy attacks
  • Integer overflow/underflow
  • Unchecked external calls
  • Access control issues
  • Denial of service vectors
  • Front-running risks
  • Timestamp manipulation
  • Uninitialized storage
  • Delegatecall vulnerabilities
  • tx.origin authentication

AI Analysis

Gemini 2.5 Flash provides:

  • Pattern recognition across 2M token context
  • Historical exploit correlation
  • Natural language vulnerability explanations
  • Automated remediation code generation

πŸ“ˆ Roadmap

  • Core auditing functionality
  • EVM-compatible contract analysis (on-chain registration on Base Sepolia)
  • AI-powered analysis with Gemini
  • MCP server architecture
  • Decentralized badge minting (December 2025)
  • Solana contract support
  • Automated GitHub integration
  • Real-time monitoring alerts
  • DAO governance for security ratings
  • Insurance integration for audited contracts
  • Mobile app (React Native)

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

πŸ“„ License

MIT License - see LICENSE file for details

πŸ™ Acknowledgments

  • Thirdweb for blockchain infrastructure
  • Google for Gemini 2.5 Flash API access
  • OpenZeppelin for secure contract libraries
  • Model Context Protocol for MCP architecture specification

πŸ“ž Contact


Made with ❀️ by Vaiosx & M0nsxx

About

AI-Powered Smart Contract Security Auditor - Built for NullShot Hacks Season 0

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors