## Phase 1 Implementation Complete: Database & Smart Contract Infrastructure ### ๐๏ธ Database Schema (Prisma) - **14 production-ready models** covering all platform features - User management with profiles, settings, and privacy controls - Trading: Trade, Position, Order models with full lifecycle tracking - AI Agents: Agent and AgentDecision models with performance metrics - Staking: StakingPosition model with APY and lock period support - Token: TokenHolder model for IMMBOT token tracking - Social: CopyTrading and Follow models for social trading - DeFi: ArbitrageExecution model for flash loan tracking - Notifications: Multi-channel notification system - **Strategic indexes** on all high-query fields for performance - **Backward compatibility** maintained with existing Models, Invocations, ToolCalls ### ๐ Smart Contract Integration - **Contract ABIs** extracted for IMMBotToken, Staking, FlashLoanArbitrage - **TypeScript types** generated from ABIs for type-safe contract calls - **ContractService** class providing clean abstraction for: - Token operations (transfer, approve, balanceOf, token info) - Staking operations (stake, withdraw, claim rewards, get pools) - Arbitrage operations (simulate, execute flash loans) - Utility methods (gas estimation, balance checks) - **Singleton pattern** for global contract service access - **Comprehensive error handling** and logging ### ๐ณ Docker Infrastructure - **PostgreSQL 15** with automated backups and health checks - **Redis 7** for caching with persistence - **Prometheus** for metrics collection - **Grafana** for dashboards and visualization - **Networked services** with proper dependencies and health checks - **Volume management** for data persistence ### ๐ Environment Configuration - Added DATABASE_URL for PostgreSQL connection - Added REDIS_URL for cache configuration - Added POLYGON_RPC_URL for Polymarket CLOB - Added contract addresses (IMMBOT_TOKEN_CONTRACT, STAKING_CONTRACT, etc.) - Added monitoring credentials (Grafana admin) - Updated both .env.example and .env.development ### ๐ Documentation - **DATABASE_SETUP.md** - Comprehensive 500+ line setup guide covering: - Quick start instructions - Database schema overview - Migration strategies - Seeding examples - Production deployment checklist - Performance optimization tips - Monitoring and troubleshooting - Backup and recovery procedures - Includes code examples for all common operations - Production security checklist - Connection pooling strategies ### ๐๏ธ Architecture Improvements - Service-oriented architecture with clear separation of concerns - Type-safe contract interactions with ethers.js - Prepared for horizontal scaling with Redis caching - Ready for metrics collection with Prometheus - Foundation for monitoring dashboards with Grafana ### ๐ Database Models Summary **Users & Social:** - User (with stats, settings, profile) - Follow (social graph) - CopyTrading (copy trading relationships) **Trading:** - Trade (execution history with fees, gas, slippage) - Position (current holdings with PnL tracking) - Order (advanced order types: limit, stop-loss, take-profit) **AI & Automation:** - Agent (strategy configs, performance metrics) - AgentDecision (decision logs with reasoning) **DeFi:** - StakingPosition (lock periods, APY, rewards) - TokenHolder (IMMBOT holder tracking) - ArbitrageExecution (flash loan PnL tracking) **System:** - Notification (multi-channel alerts) ### ๐ Next Steps (Phase 1 Remaining) 1. Initialize contract service in API server 2. Create token backend endpoints (/api/token/*) 3. Create staking backend endpoints (/api/staking/*) 4. Set up Prometheus metrics collection 5. Create Grafana dashboards 6. Build token frontend (/token) 7. Build staking frontend (/staking) ### ๐ฆ Files Changed - prisma/schema.prisma (67 โ 600 lines, 14 new models) - docker-compose.yml (added postgres, redis, prometheus, grafana) - .env.example (added database, redis, contracts, monitoring config) - .env.development (added same configurations) - src/contracts/abis.ts (NEW - 3 contract ABIs) - src/services/contractService.ts (NEW - 500+ lines) - DATABASE_SETUP.md (NEW - comprehensive setup guide) This commit completes the foundational infrastructure for production deployment. All smart contracts can now be integrated with type-safe calls. Database is ready for migration and seeding. Monitoring stack is configured and ready to deploy.
11 KiB
11 KiB
Database Setup Guide
Overview
This guide will help you set up the production-ready PostgreSQL database for Immortal BNB.
Prerequisites
- Docker and Docker Compose installed
- Node.js 18+ or Bun 1.0+
- Git
Quick Start
1. Start Database Services
# Start PostgreSQL, Redis, Prometheus, and Grafana
docker compose up -d postgres redis prometheus grafana
# Verify services are running
docker compose ps
Expected output:
NAME STATUS PORTS
immortal-bnb-postgres Up 0.0.0.0:5432->5432/tcp
immortal-bnb-redis Up 0.0.0.0:6379->6379/tcp
immortal-bnb-prometheus Up 0.0.0.0:9090->9090/tcp
immortal-bnb-grafana Up 0.0.0.0:3002->3000/tcp
2. Run Database Migrations
# Create and apply migrations
npx prisma migrate dev --name production_schema
# Or with Bun
bunx prisma migrate dev --name production_schema
3. Generate Prisma Client
# Generate TypeScript types and Prisma Client
npx prisma generate
# Or with Bun
bunx prisma generate
4. Verify Setup
# Open Prisma Studio to browse the database
npx prisma studio
# This will open http://localhost:5555
Database Schema
The production database includes the following models:
User Management
- User - User accounts with wallet addresses, profiles, settings
- Follow - Social following relationships
Trading
- Trade - Trade execution history
- Position - Current market positions
- Order - Active and historical orders
AI Agents
- Agent - AI trading agent configurations
- AgentDecision - Agent decision logs
Staking & Token
- StakingPosition - User staking positions
- TokenHolder - IMMBOT token holder data
DeFi
- ArbitrageExecution - Flash loan arbitrage history
Social
- CopyTrading - Copy trading relationships
System
- Notification - User notifications across all channels
Environment Variables
Ensure your .env or .env.development file contains:
# Database
DATABASE_URL=postgresql://immortal:immortal_dev_password@localhost:5432/immortal_bnb
DATABASE_PASSWORD=immortal_dev_password
# Redis
REDIS_URL=redis://localhost:6379
# Polygon (for Polymarket CLOB)
POLYGON_RPC_URL=https://polygon-rpc.com
POLYGON_WALLET_PRIVATE_KEY=your_private_key_here
# Smart Contracts (after deployment)
IMMBOT_TOKEN_CONTRACT=0x...
STAKING_CONTRACT=0x...
FLASH_LOAN_ARBITRAGE_CONTRACT=0x...
Database Migrations
Create a New Migration
# Make changes to prisma/schema.prisma, then:
npx prisma migrate dev --name descriptive_migration_name
Apply Migrations in Production
# In production, use migrate deploy instead of migrate dev
npx prisma migrate deploy
Reset Database (Development Only)
# WARNING: This will delete all data!
npx prisma migrate reset
Seeding the Database
Create Seed File
Create prisma/seed.ts:
import { PrismaClient } from '../generated/prisma';
const prisma = new PrismaClient();
async function main() {
// Create test user
const testUser = await prisma.user.create({
data: {
walletAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
username: 'test_trader',
displayName: 'Test Trader',
bio: 'Testing the Immortal BNB platform',
publicProfile: true,
},
});
console.log('โ
Created test user:', testUser.username);
// Create test agent
const testAgent = await prisma.agent.create({
data: {
userId: testUser.id,
name: 'Polymarket Pro',
description: 'AI agent for Polymarket trading',
agentType: 'POLYMARKET',
strategyName: 'momentum_trading',
config: {
indicators: ['rsi', 'macd'],
timeframe: '1h',
},
maxTradeAmount: 100,
minConfidence: 0.7,
riskTolerance: 0.5,
allowedMarkets: [],
active: false,
},
});
console.log('โ
Created test agent:', testAgent.name);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Run Seed
# Add to package.json:
{
"prisma": {
"seed": "ts-node prisma/seed.ts"
}
}
# Then run:
npx prisma db seed
Accessing Services
PostgreSQL
- Host: localhost
- Port: 5432
- Database: immortal_bnb
- Username: immortal
- Password: immortal_dev_password
# Connect using psql
docker exec -it immortal-bnb-postgres psql -U immortal -d immortal_bnb
# Common queries
\dt # List tables
\d users # Describe users table
SELECT * FROM "User" LIMIT 10;
Redis
- Host: localhost
- Port: 6379
# Connect using redis-cli
docker exec -it immortal-bnb-redis redis-cli
# Common commands
KEYS * # List all keys
GET key_name # Get value
FLUSHALL # Clear all data (careful!)
Prometheus
- URL: http://localhost:9090
- Targets: http://localhost:9090/targets
- Queries: http://localhost:9090/graph
# Example queries:
rate(http_requests_total[5m])
http_request_duration_seconds_bucket
Grafana
- URL: http://localhost:3002
- Username: admin
- Password: admin
Import dashboards from monitoring/grafana/dashboards/.
Production Deployment
1. Use Managed Database (Recommended)
Recommended Providers:
- AWS RDS for PostgreSQL
- DigitalOcean Managed Databases
- Railway (easiest, includes Redis)
- Supabase (PostgreSQL + realtime features)
2. Security Checklist
- Use strong passwords (not
immortal_dev_password) - Enable SSL/TLS for database connections
- Use connection pooling (PgBouncer)
- Set up automated backups
- Enable point-in-time recovery
- Restrict database access by IP
- Use read replicas for scaling
- Monitor query performance
- Set up alerts for high CPU/memory
3. Connection Pooling
Update prisma/schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
relationMode = "prisma" // For serverless
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}
Use connection pooling:
import { PrismaClient } from '../generated/prisma';
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: ['query', 'error', 'warn'],
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
});
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
4. Migration Strategy
# 1. Backup production database
pg_dump -U immortal -d immortal_bnb > backup.sql
# 2. Test migrations on staging
export DATABASE_URL="postgresql://user:pass@staging-db:5432/db"
npx prisma migrate deploy
# 3. Apply to production (zero-downtime)
export DATABASE_URL="postgresql://user:pass@prod-db:5432/db"
npx prisma migrate deploy
# 4. Verify
npx prisma migrate status
Performance Optimization
Indexes
The schema includes strategic indexes on:
- User lookups (walletAddress, username)
- Trading queries (userId, marketId, createdAt)
- Agent performance (totalPnL, winRate)
- Social queries (followerId, leaderId)
Query Optimization
// Bad: N+1 query
const users = await prisma.user.findMany();
for (const user of users) {
const trades = await prisma.trade.findMany({ where: { userId: user.id } });
}
// Good: Include related data
const users = await prisma.user.findMany({
include: {
trades: {
take: 10,
orderBy: { createdAt: 'desc' },
},
},
});
Caching Strategy
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
async function getUserWithCache(userId: string) {
// Try cache first
const cached = await redis.get(`user:${userId}`);
if (cached) return JSON.parse(cached);
// Query database
const user = await prisma.user.findUnique({ where: { id: userId } });
// Cache for 5 minutes
await redis.setex(`user:${userId}`, 300, JSON.stringify(user));
return user;
}
Troubleshooting
Migration Conflicts
# If migrations are out of sync:
npx prisma migrate resolve --applied "20241117_migration_name"
# Or reset (development only):
npx prisma migrate reset
Connection Issues
# Test database connection
docker exec -it immortal-bnb-postgres pg_isready -U immortal
# Check logs
docker logs immortal-bnb-postgres
# Restart database
docker compose restart postgres
Performance Issues
-- Find slow queries
SELECT * FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
-- Check table sizes
SELECT
schemaname,
tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
Monitoring
Health Checks
import { prisma } from './lib/prisma';
app.get('/health/db', async (req, res) => {
try {
await prisma.$queryRaw`SELECT 1`;
res.json({ status: 'healthy', database: 'connected' });
} catch (error) {
res.status(503).json({ status: 'unhealthy', error: error.message });
}
});
Metrics to Track
- Connection pool utilization
- Query duration (p50, p95, p99)
- Active connections
- Database size
- Index usage
- Cache hit ratio
Backup & Recovery
Automated Backups
# Backup script (add to cron)
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backups"
docker exec immortal-bnb-postgres pg_dump -U immortal immortal_bnb > "$BACKUP_DIR/backup_$DATE.sql"
# Compress
gzip "$BACKUP_DIR/backup_$DATE.sql"
# Keep only last 7 days
find $BACKUP_DIR -name "backup_*.sql.gz" -mtime +7 -delete
Restore from Backup
# Restore from backup
gunzip backup_20241117_120000.sql.gz
docker exec -i immortal-bnb-postgres psql -U immortal -d immortal_bnb < backup_20241117_120000.sql
Next Steps
- โ Database schema created
- โญ๏ธ Run migrations:
npx prisma migrate dev - โญ๏ธ Generate Prisma client:
npx prisma generate - โญ๏ธ Create seed data:
npx prisma db seed - โญ๏ธ Implement backend services using Prisma Client
- โญ๏ธ Set up monitoring dashboards in Grafana
- โญ๏ธ Configure production database (RDS, Railway, etc.)