Skip to content

degenspot/onchainsage-offchain

Repository files navigation

OnChain Sage - Backend API & Data Processing

OnChain Sage Logo

Decentralized Trading Intelligence Platform - Backend Services

Node.js NestJS TypeScript PostgreSQL Redis License: MIT

🎯 Overview

The OnChain Sage backend serves as the central nervous system for the decentralized trading intelligence platform, providing:

  • AI-Driven Market Analysis: Real-time processing of market data and social sentiment
  • Multi-Chain Trading Intelligence: Comprehensive analysis across multiple blockchain networks
  • Community Features: Forum management and reputation system backend
  • External Data Integration: Twitter/X, DEX Screener, Raydium, and other market data sources

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  API Gateway Layer                          β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                   β”‚
β”‚           β”‚     NestJS Backend API      β”‚                   β”‚
β”‚           β”‚   (Authentication, Logic,   β”‚                   β”‚
β”‚           β”‚   Data Processing)          β”‚                   β”‚
β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 Core Modules                                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚   Trading    β”‚ β”‚     AI/ML    β”‚ β”‚   Data Ingestion     β”‚ β”‚
β”‚  β”‚   Signals    β”‚ β”‚  Processing  β”‚ β”‚   (External APIs)    β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Community    β”‚ β”‚     Auth     β”‚ β”‚   Notifications      β”‚ β”‚
β”‚  β”‚   Forum      β”‚ β”‚   & Users    β”‚ β”‚   & Alerts           β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Data Layer                                β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”‚
β”‚           β”‚   PostgreSQL + Redis Cache   β”‚                  β”‚
β”‚           β”‚   Message Queue (Bull)       β”‚                  β”‚
β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ and npm/yarn
  • PostgreSQL 15+
  • Redis 7+
  • Git

Environment Setup

  1. Clone the repository

    git clone https://github.com/your-org/onchainsage-offchain.git
    cd onchainsage-offchain
  2. Install dependencies

    npm install
  3. Environment Configuration

    cp .env.example .env

    Configure your .env file with:

    # Database
    DATABASE_URL=postgresql://username:password@localhost:5432/onchainsage_db
    REDIS_URL=redis://localhost:6379
    
    # API Keys
    TWITTER_API_KEY=your_twitter_api_key
    TWITTER_API_SECRET=your_twitter_api_secret
    DEX_SCREENER_API_KEY=your_dex_screener_api_key
    
    # StarkNet
    STARKNET_RPC_URL=https://starknet-mainnet.public.blastapi.io
    STARKNET_PRIVATE_KEY=your_private_key
    
    # JWT
    JWT_SECRET=your_jwt_secret
    JWT_EXPIRES_IN=24h
    
    # Application
    PORT=3000
    NODE_ENV=development
  4. Database Setup

    # Run migrations
    npm run migration:run
    
    # Seed data (optional)
    npm run seed
  5. Start Development Server

    npm run start:dev

The API will be available at http://localhost:3000

πŸ“ Project Structure

src/
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ auth/                   # Authentication & wallet verification
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”‚   β”œβ”€β”€ auth.module.ts
β”‚   β”‚   β”œβ”€β”€ guards/
β”‚   β”‚   └── strategies/
β”‚   β”œβ”€β”€ trading/                # Trading signals & bot logic
β”‚   β”‚   β”œβ”€β”€ trading.controller.ts
β”‚   β”‚   β”œβ”€β”€ trading.service.ts
β”‚   β”‚   β”œβ”€β”€ signals/
β”‚   β”‚   └── bots/
β”‚   β”œβ”€β”€ data-ingestion/         # External API integrations
β”‚   β”‚   β”œβ”€β”€ twitter/
β”‚   β”‚   β”œβ”€β”€ dex-screener/
β”‚   β”‚   β”œβ”€β”€ raydium/
β”‚   β”‚   └── processors/
β”‚   β”œβ”€β”€ ai-processing/          # ML/AI analysis engines
β”‚   β”‚   β”œβ”€β”€ sentiment/
β”‚   β”‚   β”œβ”€β”€ pattern-recognition/
β”‚   β”‚   β”œβ”€β”€ risk-assessment/
β”‚   β”‚   └── signal-generation/
β”‚   β”œβ”€β”€ community/              # Forum & social features
β”‚   β”‚   β”œβ”€β”€ forum/
β”‚   β”‚   β”œβ”€β”€ reputation/
β”‚   β”‚   └── badges/
β”‚   └── notifications/          # Alert & notification system
β”‚       β”œβ”€β”€ notifications.controller.ts
β”‚       β”œβ”€β”€ notifications.service.ts
β”‚       └── channels/
β”œβ”€β”€ shared/
β”‚   β”œβ”€β”€ dto/                    # Data transfer objects
β”‚   β”œβ”€β”€ interfaces/             # Shared interfaces
β”‚   β”œβ”€β”€ utils/                  # Utility functions
β”‚   β”œβ”€β”€ constants/              # Application constants
β”‚   └── decorators/             # Custom decorators
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ migrations/             # Database migrations
β”‚   β”œβ”€β”€ seeds/                  # Seed data
β”‚   └── entities/               # Database entities
β”œβ”€β”€ config/                     # Configuration files
β”‚   β”œβ”€β”€ database.config.ts
β”‚   β”œβ”€β”€ redis.config.ts
β”‚   └── app.config.ts
└── main.ts                     # Application entry point

πŸ”§ Core Modules

πŸ” Authentication Module

  • Wallet Integration: StarkNet wallet signature verification
  • JWT Tokens: Secure session management
  • Role-based Access: User hierarchy and permissions

πŸ“Š Trading Module

  • Signal Generation: AI-powered buy/sell recommendations
  • Bot Configurations: Conservative, Moderate, Aggressive, Degen modes
  • Risk Assessment: Multi-factor risk scoring system
  • Performance Tracking: Signal accuracy and profitability metrics

πŸ“‘ Data Ingestion Module

  • Twitter/X Integration: Real-time narrative and sentiment analysis
  • DEX Screener API: Price, volume, and market data
  • Raydium Integration: Liquidity pools and trading metrics
  • On-chain Analytics: Whale movements and holder analysis

πŸ€– AI Processing Module

  • Sentiment Analysis: NLP processing of social media content
  • Pattern Recognition: Technical analysis algorithms
  • Risk Scoring: Multi-dimensional risk assessment
  • Signal Generation: Automated trading recommendations

πŸ‘₯ Community Module

  • Forum Management: Discussion threads and moderation
  • Reputation System: Performance-based user rankings
  • Badge System: Achievement and milestone tracking
  • Voting Mechanisms: Community-driven decision making

πŸ”” Notifications Module

  • Real-time Alerts: WebSocket-based notifications
  • Email Notifications: Critical updates and reports
  • Push Notifications: Mobile and browser notifications
  • Custom Channels: User-defined notification preferences

πŸ› οΈ Available Scripts

# Development
npm run start:dev          # Start development server with hot reload
npm run start:debug        # Start with debug mode
npm run start:prod         # Start production server

# Building
npm run build              # Build the application
npm run build:webpack      # Build with webpack

# Testing
npm run test               # Run unit tests
npm run test:watch         # Run tests in watch mode
npm run test:e2e           # Run end-to-end tests
npm run test:cov           # Run tests with coverage

# Database
npm run migration:generate # Generate new migration
npm run migration:run      # Run pending migrations
npm run migration:revert   # Revert last migration
npm run seed               # Seed database with initial data

# Code Quality
npm run lint               # Run ESLint
npm run lint:fix           # Fix ESLint issues
npm run format             # Format code with Prettier

# Documentation
npm run docs:generate      # Generate API documentation
npm run docs:serve         # Serve documentation locally

πŸ”Œ API Endpoints

Authentication

POST   /auth/wallet-login     # Wallet signature authentication
POST   /auth/refresh          # Refresh JWT token
GET    /auth/profile          # Get user profile

Trading

GET    /trading/signals       # Get trading signals
POST   /trading/signals       # Submit trading signal
GET    /trading/bots          # Get bot configurations
POST   /trading/bots          # Create/update bot config
GET    /trading/performance   # Get performance metrics

Community

GET    /community/forum       # Get forum threads
POST   /community/forum       # Create new thread
GET    /community/reputation  # Get user rankings
POST   /community/vote        # Vote on content

Data

GET    /data/market           # Get market data
GET    /data/sentiment        # Get sentiment analysis
GET    /data/narratives       # Get trending narratives

For detailed API documentation, visit /api/docs when running the server.

🐳 Docker Support

Development with Docker Compose

# Start all services
docker-compose up -d

# Start specific services
docker-compose up -d postgres redis

# View logs
docker-compose logs -f api

# Stop services
docker-compose down

Production Deployment

# Build production image
docker build -t onchainsage-backend .

# Run container
docker run -p 3000:3000 --env-file .env onchainsage-backend

πŸ§ͺ Testing

Running Tests

# Unit tests
npm run test

# Integration tests
npm run test:e2e

# Test coverage
npm run test:cov

Test Structure

  • Unit Tests: Individual module testing
  • Integration Tests: API endpoint testing
  • E2E Tests: Full application flow testing

πŸš€ Deployment

Environment Variables

Ensure the following environment variables are set in production:

NODE_ENV=production
DATABASE_URL=your_production_database_url
REDIS_URL=your_production_redis_url
JWT_SECRET=your_secure_jwt_secret
TWITTER_API_KEY=your_twitter_api_key
# ... other API keys

Deployment Options

  1. Docker: Use provided Dockerfile and docker-compose
  2. PM2: Process manager for Node.js applications
  3. Cloud Platforms: AWS, GCP, Azure with container services
  4. Serverless: AWS Lambda, Vercel Functions

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
    npm run test
    npm run lint
  6. Commit your changes
    git commit -m 'Add amazing feature'
  7. Push to the branch
    git push origin feature/amazing-feature
  8. Open a Pull Request

Code Standards

  • TypeScript: Strict mode enabled
  • ESLint: Airbnb configuration
  • Prettier: Code formatting
  • Conventional Commits: Commit message format
  • Test Coverage: Minimum 80% coverage required

πŸ“Š Monitoring & Logging

Application Metrics

  • Performance: Response times and throughput
  • Health Checks: Database and external service status
  • Error Tracking: Comprehensive error logging
  • Custom Metrics: Trading signal accuracy, user activity

Logging Strategy

  • Structured Logging: JSON format for easy parsing
  • Log Levels: Error, Warn, Info, Debug
  • Request Tracing: Unique request IDs
  • Security Logging: Authentication and authorization events

πŸ”’ Security

Security Measures

  • Input Validation: All inputs validated and sanitized
  • Rate Limiting: API rate limiting to prevent abuse
  • CORS: Properly configured CORS policies
  • Helmet: Security headers middleware
  • JWT Security: Secure token handling and rotation

Security Best Practices

  • Regular dependency updates
  • Security audits and penetration testing
  • Secure API key management
  • Database security and encryption

πŸ“š Additional Resources

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support


Built with ❀️ by the OnChain Sage Community

Website β€’ Discord β€’ Twitter

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 16