Skip to content

Hijanhv/AgentMart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AgentMart πŸ€–πŸ›’

Decentralized AI Agent Marketplace - A full-stack Web3 application for buying, selling, and executing AI agents on the blockchain.

πŸ—οΈ Architecture

                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚        React UI          β”‚
                      β”‚   wagmi + ethers.js      β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                          Wallet Connect / Buy
                                   β”‚
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚       Smart Contract      β”‚
                      β”‚     AgentMarket.sol       β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚ Events
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚          The Graph            β”‚
                    β”‚ (Indexes AgentPurchased evt)  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚ GraphQL
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚      React Analytics       β”‚
                      β”‚ (Purchases, revenue etc.)  β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚       Node.js Backend       β”‚
                      β”‚ Express + Postgres + JWT    β”‚
                      β”‚ Server-side TX (optional)   β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Tech Stack

Frontend

  • React + TypeScript + Vite - Fast, modern development
  • wagmi + ethers.js - Ethereum wallet integration
  • Zustand - State management
  • Tailwind CSS - Styling
  • React Query - Data fetching

Backend

  • Node.js + Express + TypeScript
  • PostgreSQL (pg) - Database
  • JWT - Authentication (wallet sign-in)
  • Socket.io - Real-time updates
  • ethers.js - Event listening & server-side transactions

Blockchain

  • Solidity - Smart contracts
  • Hardhat - Development environment
  • Alchemy - RPC provider & WebSocket
  • The Graph - Event indexing

πŸš€ Quick Start

Prerequisites

  • Node.js >= 18
  • PostgreSQL >= 14
  • MetaMask or compatible wallet
  • Alchemy account (free tier works)

1. Clone & Install

git clone <your-repo>
cd AgentMart
npm run install:all

2. Configure Environment

cp .env.example .env
# Edit .env with your values

Required variables:

  • ALCHEMY_API_KEY - Get from Alchemy
  • DATABASE_URL - PostgreSQL connection string
  • JWT_SECRET - Random secret for JWT signing
  • RELAY_PRIVATE_KEY - Private key for server-side transactions (optional)

3. Set Up Database

# Create database
createdb agentmart

# Run migrations
npm run migrate:db

# Seed with sample agents
npm run seed:db

4. Deploy Smart Contract

# Option A: Local Hardhat network (for development)
cd contracts
npx hardhat node  # Keep running in separate terminal
npx hardhat run scripts/deploy.ts --network localhost

# Option B: Testnet (Sepolia)
npx hardhat run scripts/deploy.ts --network sepolia

Copy the deployed contract address to .env as CONTRACT_ADDRESS.

5. Start Development Servers

# Start all services (frontend, backend, listener)
npm run dev:start

# Or start individually:
npm run dev:frontend   # http://localhost:5173
npm run dev:backend    # http://localhost:3001
npm run dev:listener   # Event listener

🎬 2-Minute Demo Script

Setup (30 seconds)

  1. Ensure all services are running (npm run dev:start)
  2. Open http://localhost:5173 in browser
  3. Have MetaMask installed and connected to Sepolia testnet

Demo Flow (90 seconds)

Step 1: Connect Wallet

  • Click "Connect Wallet" button
  • Approve MetaMask connection
  • Click "Sign In" to authenticate with backend
  • See your address displayed

Step 2: Browse Agents

  • View list of AI agents with descriptions and prices
  • Click on an agent card to see details
  • Example: "Code Review Agent" - 0.001 ETH

Step 3: Purchase Agent

  • Click "Buy Agent" button
  • Choose purchase method:
    • Client-side: You pay gas (MetaMask popup)
    • Server-relay: Server pays gas (faster, sponsored)
  • Confirm transaction

Step 4: Watch Transaction

  • See transaction hash and Etherscan link
  • Backend listener detects AgentPurchased event
  • Order status updates: Pending β†’ Confirmed
  • Real-time toast notification via Socket.io

Step 5: View Result

  • Order marked as "Confirmed" in your purchases
  • Agent execution result displayed (or mock data)
  • Transaction visible on Etherscan

Optional: The Graph Query

  • View analytics dashboard
  • GraphQL query shows all purchases, total revenue
  • Indexed from blockchain events

πŸ“ Project Structure

AgentMart/
β”œβ”€β”€ frontend/                 # React + Vite app
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/      # UI components
β”‚   β”‚   β”‚   β”œβ”€β”€ WalletConnect.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ AgentList.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ AgentDetail.tsx
β”‚   β”‚   β”‚   └── BuyButton.tsx
β”‚   β”‚   β”œβ”€β”€ state/          # Zustand store
β”‚   β”‚   β”œβ”€β”€ utils/          # ethers.js helpers
β”‚   β”‚   β”œβ”€β”€ api.ts          # API client
β”‚   β”‚   └── App.tsx
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.ts
β”‚
β”œβ”€β”€ backend/                 # Express API
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ routes/         # API endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.ts     # Wallet sign-in
β”‚   β”‚   β”‚   β”œβ”€β”€ agents.ts   # Agent CRUD
β”‚   β”‚   β”‚   └── purchase.ts # Order management
β”‚   β”‚   β”œβ”€β”€ db/             # PostgreSQL client
β”‚   β”‚   β”œβ”€β”€ listener.ts     # Event listener (Alchemy WS)
β”‚   β”‚   β”œβ”€β”€ relayer.ts      # Server-side signer
β”‚   β”‚   └── index.ts        # Express app
β”‚   └── package.json
β”‚
β”œβ”€β”€ contracts/               # Solidity + Hardhat
β”‚   β”œβ”€β”€ contracts/
β”‚   β”‚   └── AgentMarket.sol
β”‚   β”œβ”€β”€ scripts/
β”‚   β”‚   └── deploy.ts
β”‚   β”œβ”€β”€ test/
β”‚   β”‚   β”œβ”€β”€ AgentMarket.test.ts
β”‚   β”‚   └── gas-usage.test.ts
β”‚   β”œβ”€β”€ abi/
β”‚   β”‚   └── AgentMarket.json
β”‚   └── hardhat.config.ts
β”‚
β”œβ”€β”€ subgraph/               # The Graph indexing
β”‚   β”œβ”€β”€ schema.graphql
β”‚   β”œβ”€β”€ subgraph.yaml
β”‚   └── mappings/
β”‚       └── mapping.ts
β”‚
β”œβ”€β”€ scripts/                # Database & deployment
β”‚   β”œβ”€β”€ migrate.sql
β”‚   β”œβ”€β”€ seed_agents.sql
β”‚   └── deploy_contract.sh
β”‚
β”œβ”€β”€ .env.example
β”œβ”€β”€ package.json
└── README.md

πŸ”‘ Key Features

Dual Purchase Modes

1. Client-Side Purchase (User pays gas)

// Frontend signs transaction
const tx = await contract.purchase(agentId, { value: price });
// Backend records order with txHash
await fetch('/api/purchase', { 
  method: 'POST', 
  body: { agentId, txHash: tx.hash } 
});

2. Server-Relay Purchase (Server pays gas - sponsored experience)

// Frontend requests purchase
const { txHash } = await fetch('/api/purchase', {
  method: 'POST',
  body: { agentId, amount }
});
// Backend signs & sends transaction with RELAY_PRIVATE_KEY

Event-Driven Architecture

  1. Smart Contract emits AgentPurchased event
  2. Listener Service (Alchemy WebSocket) catches event
  3. Database updated with confirmation
  4. Socket.io pushes update to frontend
  5. The Graph indexes for analytics

πŸ§ͺ Testing

Smart Contract Tests

cd contracts

# Run all tests
npm test

# Gas usage analysis
npm run test:gas

# Coverage
npm run coverage

Backend Tests

cd backend
npm test

🚒 Deployment

Smart Contract

# Deploy to Sepolia testnet
cd contracts
npm run deploy:sepolia

# Verify on Etherscan
npx hardhat verify --network sepolia <CONTRACT_ADDRESS>

Backend API

cd backend
npm run build

# Deploy to your preferred platform:
# - Railway, Render, Fly.io for Node.js
# - Configure DATABASE_URL for production PostgreSQL

Frontend

cd frontend
npm run build

# Deploy to Vercel, Netlify, or Cloudflare Pages
# Set environment variables in hosting platform

The Graph Subgraph

# Authenticate with The Graph Studio
graph auth --studio <DEPLOY_KEY>

# Deploy subgraph
npm run subgraph:codegen
npm run subgraph:build
npm run subgraph:deploy

πŸ” Security Notes

⚠️ Important for Production:

  1. Never commit private keys - Use environment variables
  2. Rotate JWT secrets - Use strong, random values
  3. Limit relayer funds - Use dedicated wallet with minimal ETH
  4. Validate inputs - Add rate limiting, input sanitization
  5. Use HTTPS - Enable SSL/TLS in production
  6. Database security - Use connection pooling, prepared statements
  7. Audit contracts - Get professional audit before mainnet

πŸ“š API Documentation

Authentication

POST /api/auth/sign-message

{ "address": "0x..." }
β†’ { "message": "Sign this message to authenticate: <nonce>" }

POST /api/auth/verify

{ "address": "0x...", "signature": "0x..." }
β†’ { "token": "jwt_token_here" }

Agents

GET /api/agents

β†’ [{ "id": 1, "name": "Code Review Agent", "price": "0.001", ... }]

GET /api/agents/:id

β†’ { "id": 1, "name": "Code Review Agent", "description": "...", "price": "0.001" }

Purchases

POST /api/purchase (Authenticated)

// Client-side purchase
{ "agentId": 1, "txHash": "0x..." }

// Server-relay purchase  
{ "agentId": 1, "amount": "0.001" }

β†’ { "orderId": 123, "txHash": "0x...", "status": "pending" }

GET /api/orders/:id (Authenticated)

β†’ { "id": 123, "status": "confirmed", "txHash": "0x...", "agent": {...} }

🎨 Frontend Components

  • WalletConnect: Wallet connection, sign-in flow
  • AgentList: Browse available AI agents
  • AgentDetail: View agent details, purchase
  • BuyButton: Purchase flow (client or relay)
  • ToastNotification: Real-time transaction updates

πŸ”§ Troubleshooting

Database connection failed

# Check PostgreSQL is running
pg_ctl status

# Verify connection string
psql $DATABASE_URL

Contract deployment fails

# Check network configuration in hardhat.config.ts
# Verify ALCHEMY_API_KEY is set
# Ensure deployer wallet has ETH for gas

Frontend can't connect to wallet

# Install MetaMask extension
# Switch to correct network (Sepolia)
# Check VITE_CONTRACT_ADDRESS in frontend/.env

Listener not receiving events

# Verify ALCHEMY_API_KEY supports WebSockets
# Check CONTRACT_ADDRESS is correct
# Ensure contract has Purchase events emitted

πŸ› οΈ Development Tips

Add a new agent

INSERT INTO agents (name, description, price, category)
VALUES ('New Agent', 'Description', 0.001, 'Development');

Test contract locally

# Terminal 1: Start local node
npx hardhat node

# Terminal 2: Deploy & test
npx hardhat run scripts/deploy.ts --network localhost
npx hardhat test --network localhost

Monitor events in real-time

# Backend listener logs all events
npm run dev:listener

# Or manually test with ethers.js
node scripts/test-listener.js

πŸ“ˆ TODO / Future Enhancements

  • Implement actual OpenAI agent execution
  • Add agent rating/review system
  • Support multiple payment tokens (ERC-20)
  • Gas optimization in smart contract
  • Add agent marketplace curation
  • Implement dispute resolution
  • Mobile responsive UI improvements
  • Add agent execution history dashboard
  • Support batch purchases
  • Implement agent versioning

About

a small decentralized marketplace where creators publish AI agents (metadata), users connect wallets, rent/buy an agent, and the app handles on-chain transaction lifecycle + backend reconciliation.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors