Skip to content

Wayru-Network/tx-tracker-service

Repository files navigation

πŸ” Transaction Tracker Server

Open Source Blockchain Transaction Tracker - Real-time monitoring and tracking of Solana blockchain transactions for staking, unstaking, and reward claims.

TypeScript Node.js Koa PostgreSQL License: ISC

πŸ“‹ Table of Contents

🎯 Overview

Transaction Tracker Server is an open source backend service designed to monitor and track blockchain transactions on the Solana network. It provides real-time event listening for key blockchain programs, processes transaction data, and stores it in a PostgreSQL database for analysis and reporting.

The server actively monitors two main Solana programs:

  • Depin Program: Tracks staking, unstaking, NFT initialization, and node initialization events
  • Reward System Program: Monitors reward claim transactions and processes reward distributions

This server handles the complex task of parsing blockchain logs, extracting transaction data, and maintaining a comprehensive record of all relevant network activities.

⚠️ Important Notice

This project is now open source and community-maintained. WAYRU Network no longer exists and does not provide support, maintenance, or updates for this repository. The code is provided as-is for the community to use, modify, and improve.

✨ Features

πŸ“‘ Real-time Blockchain Event Monitoring

  • Depin Program Listener: Monitors staking, unstaking, NFT initialization, and node initialization events
  • Reward System Listener: Tracks reward claim transactions with optional memo parsing
  • Event Callbacks: Flexible callback system for processing different event types
  • Automatic Reconnection: Robust connection management with automatic reconnection handling
  • Error Resilience: Graceful error handling and recovery mechanisms

πŸ’Ύ Transaction Tracking & Storage

  • Transaction Database: Comprehensive PostgreSQL storage for all tracked transactions
  • Transaction Status Tracking: Monitor transaction states from initiation to completion
  • Reward Tracking: Track reward claims and payment status
  • Historical Data: Maintain complete transaction history for analysis
  • Database Indices: Optimized queries with strategic database indexing

πŸ”— Web3 Integration

  • Solana Blockchain: Full integration with Solana Web3.js for blockchain interactions
  • Anchor Framework: Uses Coral XYZ Anchor for program interaction
  • Smart Contract Integration: Direct interaction with Solana smart contracts
  • Connection Management: Singleton pattern for efficient connection reuse
  • Multi-RPC Support: Configurable RPC endpoints with API key support

⚑ Health Monitoring & Heartbeat System

  • Heartbeat Service: Automatically updates service health status in the database every 15 seconds
  • Database-Backed Health Checks: Frontend can verify service availability by querying the shared database
  • Comprehensive Health Status: Monitors database, Solana RPC, Depin Program, and Reward System connections
  • Service Status Tracking: Real-time status tracking stored in heartbeats table for cross-service communication
  • Automatic Document Creation: Creates heartbeat records automatically if they don't exist

πŸ› οΈ Tech Stack

  • Runtime: Node.js 22+
  • Framework: Koa.js - Lightweight web framework
  • Language: TypeScript 5.4 - Type-safe JavaScript
  • Database: PostgreSQL 8.14+ - Robust relational database
  • Blockchain:
  • Task Scheduling: node-cron - Automated task execution
  • Development:
    • tsx - Fast TypeScript execution
    • nodemon - Development server with hot-reload
    • ESLint - Code quality and linting

πŸš€ Getting Started

Prerequisites

Before you begin, ensure you have the following installed:

Installation

  1. Clone the repository
git clone https://github.com/Wayru-Network/tx-tracker-server.git
cd tx-tracker-server
  1. Install dependencies
npm install
# or
yarn install
  1. Set up environment variables

Create a .env file in the root directory (see Configuration section below for details):

cp .env.example .env  # If .env.example exists
# Or create .env manually
  1. Configure your database

    • Create a PostgreSQL database
    • Run any required migrations or schema setup
    • Update the .env file with your database credentials
  2. Build the project (for production)

npm run build

Configuration

Create a .env file in the root directory with the following variables:

# Server Configuration
PORT=1335
NODE_ENV=development

# Database Configuration
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=your_database_name
DATABASE_USERNAME=your_username
DATABASE_PASSWORD=your_password
DATABASE_SSL=false

# Explorers Database Configuration (optional)
EXPLORERS_DATABASE_HOST=localhost
EXPLORERS_DATABASE_PORT=5432
EXPLORERS_DATABASE_NAME=explorers_db
EXPLORERS_DATABASE_USERNAME=explorer_user
EXPLORERS_DATABASE_PASSWORD=explorer_password
EXPLORERS_DATABASE_SSL=false

# Web3 Configuration
DB_ADMIN_PUBLIC_KEY=your_admin_public_key
SOLANA_WALLET_PRIVATE_KEY=your_solana_wallet_private_key
SOLANA_API_URL=https://api.devnet.solana.com
SOLANA_API_KEY=your_solana_api_key  # Optional, for rate-limited RPC endpoints

# Program IDs (Solana Program Addresses)
DEFAULT_DEPIN_PROGRAM_ID=D1sMCRu3tRwCviHUDj69WrRQzDoVKd2m2YKydRyauYmJ
DEFAULT_REWARD_SYSTEM_PROGRAM_ID=Ey6f9uyT1s3UrCGpc586aeHmEupYdfR2xo8Nh7TpqLhX

Running the Server

Development mode (with hot-reload):

npm run dev

Production mode:

npm run build
npm start

The server will start on http://localhost:1335 (or the port specified in your .env file).

πŸ“ Project Structure

tx-tracker-server/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ api/                       # API routes and handlers
β”‚   β”‚   β”œβ”€β”€ hotspots-stakes/      # Hotspots and stakes API endpoints
β”‚   β”‚   β”œβ”€β”€ keys/                 # Keys API endpoints
β”‚   β”‚   β”œβ”€β”€ nfnodes/              # NFNodes API endpoints
β”‚   β”‚   β”œβ”€β”€ others/               # Other API endpoints
β”‚   β”‚   └── api.routes.ts         # Main API router configuration
β”‚   β”œβ”€β”€ bootstrap/                # Application initialization
β”‚   β”‚   β”œβ”€β”€ bootstrap.ts          # Bootstrap sequence (listeners, services)
β”‚   β”‚   └── shutdown.ts           # Graceful shutdown handlers
β”‚   β”œβ”€β”€ config/                   # Configuration files
β”‚   β”‚   β”œβ”€β”€ env/
β”‚   β”‚   β”‚   └── env.ts           # Environment variables loader
β”‚   β”‚   └── db.ts                 # PostgreSQL connection pools
β”‚   β”œβ”€β”€ constants/                # Application constants
β”‚   β”‚   β”œβ”€β”€ router.ts             # Route path constants
β”‚   β”‚   └── web3.ts               # Web3-related constants
β”‚   β”œβ”€β”€ crons/                    # Scheduled tasks
β”‚   β”‚   └── index.ts              # Cron job definitions
β”‚   β”œβ”€β”€ database/                 # Database scripts
β”‚   β”‚   └── indices.sql           # Database index definitions
β”‚   β”œβ”€β”€ helpers/                  # Utility helper functions
β”‚   β”œβ”€β”€ interfaces/               # TypeScript type definitions
β”‚   β”œβ”€β”€ middlewares/              # Koa middleware
β”‚   β”‚   β”œβ”€β”€ auth-validator.ts     # Authentication middleware
β”‚   β”‚   └── db-error-handler.ts   # Database error handling
β”‚   β”œβ”€β”€ services/                 # Business logic services
β”‚   β”‚   β”œβ”€β”€ health/               # Health check service
β”‚   β”‚   β”‚   └── health-check.service.ts
β”‚   β”‚   β”œβ”€β”€ heartbeat/            # Heartbeat service
β”‚   β”‚   β”‚   β”œβ”€β”€ heartbeat.service.ts
β”‚   β”‚   β”‚   └── heartbeat.queries.ts
β”‚   β”‚   └── web3/                 # Web3-related services
β”‚   β”‚       β”œβ”€β”€ events/           # Blockchain event listeners
β”‚   β”‚       β”‚   β”œβ”€β”€ depin-program/
β”‚   β”‚       β”‚   β”œβ”€β”€ reward-system/
β”‚   β”‚       β”‚   └── token-2022-transfer-listener.service.ts
β”‚   β”‚       β”œβ”€β”€ program/          # Program interaction services
β”‚   β”‚       β”œβ”€β”€ solana/           # Solana connection management
β”‚   β”‚       └── wallet/           # Wallet services
β”‚   β”œβ”€β”€ utils/                    # Utility functions
β”‚   └── server.ts                 # Main server entry point
β”œβ”€β”€ dist/                         # Compiled JavaScript (generated)
β”œβ”€β”€ kubernetes/                   # Kubernetes deployment files
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Dockerfile                     # Docker container definition
β”œβ”€β”€ eslint.config.mjs             # ESLint configuration
β”œβ”€β”€ nodemon.json                   # Nodemon configuration
β”œβ”€β”€ package.json                   # Project dependencies and scripts
β”œβ”€β”€ tsconfig.json                  # TypeScript configuration
β”œβ”€β”€ tsconfig-paths-bootstrap.js   # Path alias bootstrap
└── README.md                      # This file

πŸ“œ Available Scripts

Command Description
npm run dev Start development server with hot-reload using nodemon
npm run build Compile TypeScript to JavaScript, run type checking, and copy SQL files
npm start Start production server (requires build first)
npm run lint Run ESLint to check code quality
npm run lint:fix Run ESLint and automatically fix issues

πŸ“š API Documentation

Authentication

All API endpoints (except health checks) require authentication. The authentication middleware validates requests based on the configured DB_ADMIN_PUBLIC_KEY environment variable.

Endpoints

Health Check

GET /api/health

Returns the health status of the server and its dependencies.

Response:

{
  "status": "ok" | "degraded" | "down",
  "timestamp": "2024-01-15T10:30:00.000Z",
  "services": {
    "database": "connected" | "disconnected" | "unknown",
    "solana": "connected" | "disconnected" | "unknown",
    "depin": "connected" | "disconnected" | "unknown",
    "rewardSystem": "connected" | "disconnected" | "unknown"
  }
}

Protected Endpoints

All endpoints under /api/* (except /api/health ) require authentication. See the source code in src/api/ for specific endpoint documentation.

πŸ’“ Heartbeat System

The Transaction Tracker Server includes a heartbeat system that allows other services (like frontends) to verify its health status by querying a shared database table. This approach avoids exposing HTTP endpoints and IP addresses while providing reliable health status information.

How It Works

  1. Automatic Updates: The heartbeat service runs automatically and updates the heartbeats table every 15 seconds
  2. Service Identification: Uses service_name = 'tx_tracker_service' to identify this service
  3. Health Status Storage: Stores comprehensive health information in the extra_info JSON field
  4. Database-Backed: Frontend and other services can query the database directly to check service status

Database Table Structure

The heartbeats table contains the following fields:

  • service_name (VARCHAR): Service identifier ('tx_tracker_service')
  • last_seen_at (TIMESTAMP): Last heartbeat update timestamp
  • extra_info (JSON): Complete health status information
  • created_at (TIMESTAMP): Record creation timestamp
  • published_at (TIMESTAMP): Record publication timestamp

Health Status Format

The extra_info field contains a JSON object with the following structure:

{
  "status": "ok" | "degraded" | "down",
  "timestamp": "2024-01-15T10:30:00.000Z",
  "services": {
    "database": "connected" | "disconnected" | "unknown",
    "solana": "connected" | "disconnected" | "unknown",
    "depin": "connected" | "disconnected" | "unknown",
    "rewardSystem": "connected" | "disconnected" | "unknown"
  }
}

Frontend Integration

To check if the service is healthy before performing transactions, query the database:

SELECT service_name, last_seen_at, extra_info
FROM heartbeats
WHERE service_name = 'tx_tracker_service';

Health Check Logic:

  • If last_seen_at is older than 30-60 seconds β†’ Service may be down
  • If extra_info.status is "down" β†’ Do not proceed with transactions
  • If extra_info.status is "degraded" β†’ Show warning but allow transactions
  • If extra_info.status is "ok" β†’ Proceed normally

Features

  • Automatic Document Creation: If no heartbeat record exists, one is created automatically with default status
  • Table Existence Check: Gracefully handles cases where the table doesn't exist yet
  • Error Resilience: Heartbeat failures don't crash the service
  • Update Strategy: Uses UPDATE-first approach (no UNIQUE constraint required)

πŸ’» Development

Code Style

  • Follow TypeScript best practices
  • Use async/await for asynchronous operations
  • Implement proper error handling
  • Add JSDoc comments for public functions
  • Use ESLint for code quality checks
  • Follow the existing code structure and patterns

Database Setup

  1. Create the database:
CREATE DATABASE your_database_name;
  1. Run database migrations (if applicable):
# Migration commands will depend on your setup
npm run migrate
  1. Create required indices:

The project includes an indices.sql file that creates performance-optimized indices. Run it against your database:

psql -d your_database_name -f src/database/indices.sql

Environment Variables

See the Configuration section for all available environment variables. Make sure to set up all required variables before running the server.

🐳 Docker Deployment

The project includes a Dockerfile for containerized deployment. To build and run:

# Build the Docker image
docker build -t tx-tracker-server .

# Run the container
docker run -p 1335:1335 --env-file .env tx-tracker-server

For Kubernetes deployment, see the kubernetes/ directory for deployment configurations.

🀝 Contributing

Contributions are welcome! Since this project is now community-maintained, your contributions help keep it alive and improve it for everyone.

How to Contribute

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Make your changes
  4. Run linting and ensure code quality (npm run lint)
  5. Commit your changes (git commit -m 'Add some AmazingFeature')
  6. Push to the branch (git push origin feature/AmazingFeature)
  7. Open a Pull Request

Guidelines

  • Write clear commit messages
  • Follow the existing code style
  • Add tests for new features (when test infrastructure is available)
  • Update documentation as needed
  • Be respectful and constructive in discussions

πŸ“„ License

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

This project is open source and available for use, modification, and distribution by the community.


πŸ’™ Farewell Message

With gratitude and love, we say goodbye.

WAYRU is closing its doors, but we are leaving these repositories open and free for the community.

May they continue to inspire builders, dreamers, and innovators.

With love, WAYRU


Note: This project is open source. Wayru, Inc and The Wayru Foundation are no longer operating entities, and will not provide any kind of support. The community is welcome to use, modify, and improve this codebase.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages