Skip to content

Latest commit

Β 

History

History
297 lines (229 loc) Β· 9.21 KB

File metadata and controls

297 lines (229 loc) Β· 9.21 KB

Stellar Micro-Donation API

A Node.js/Express API for managing micro-donations on the Stellar blockchain network. Supports one-time donations, recurring donation schedules, wallet management, and donation analytics.

πŸ“‹ Table of Contents

✨ Features

  • One-Time Donations: Create and verify donations on Stellar testnet/mainnet
  • Recurring Donations: Schedule automated recurring donations (daily, weekly, monthly)
  • Wallet Management: Track wallets and query transaction history
  • Analytics: Get donation statistics and summaries
  • Mock Mode: Development mode with simulated Stellar operations
  • Automated Scheduler: Background service for executing recurring donations
  • Rate Limiting: Protection against abuse with configurable request limits on donation endpoints
  • Idempotency: Prevent duplicate transactions with idempotency key support

πŸ—οΈ Architecture

High-Level Overview

                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                     β”‚   Clients   β”‚
                                     β”‚ (Web/Mobile)β”‚
                                     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚ HTTP/HTTPS
                                            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚      Express.js API Layer       β”‚
                            β”‚  /donations  /wallets  /stream  β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚       Service Layer             β”‚
                            β”‚  Stellar Service | Scheduler    β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                β–Ό              β–Ό            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚ SQLite   β”‚   β”‚ Stellar  β”‚  β”‚ Horizon β”‚
                            β”‚ Database β”‚   β”‚ Network  β”‚  β”‚   API   β”‚
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

For detailed architecture documentation, see:

Key Components

  • API Layer: Express.js routes handling HTTP requests
  • Service Layer: Business logic and Stellar blockchain integration
  • Data Layer: SQLite database for persistent storage
  • Scheduler: Background service for recurring donations (runs every 60s)

πŸš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn
  • SQLite3

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/Stellar-Micro-Donation-API.git
cd Stellar-Micro-Donation-API
  1. Install dependencies:
npm install
  1. Initialize the database:
npm run init-db
  1. Start the server:
npm start

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

Development Mode

For development with auto-reload:

npm run dev

πŸ“‘ API Endpoints

Donations

  • POST /donations - Create a new donation
  • GET /donations - List all donations
  • GET /donations/recent?limit=10 - Get recent donations
  • GET /donations/:id - Get specific donation
  • POST /donations/verify - Verify transaction on blockchain

Wallets

  • POST /wallets - Create wallet metadata
  • GET /wallets - List all wallets
  • GET /wallets/:id - Get specific wallet
  • GET /wallets/:publicKey/transactions - Get all transactions for a wallet
  • PATCH /wallets/:id - Update wallet metadata

Recurring Donations (Stream)

  • POST /stream/create - Create recurring donation schedule
  • GET /stream/schedules - List all schedules
  • GET /stream/schedules/:id - Get specific schedule
  • DELETE /stream/schedules/:id - Cancel schedule

Statistics

  • GET /stats/donations - Get donation statistics
  • GET /stats/summary - Get summary analytics

Health Check

  • GET /health - API health status

πŸ—„οΈ Database Schema

Users Table

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    publicKey TEXT NOT NULL UNIQUE,
    createdAt DATETIME DEFAULT CURRENT_TIMESTAMP
);

Transactions Table

CREATE TABLE transactions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    senderId INTEGER NOT NULL,
    receiverId INTEGER NOT NULL,
    amount REAL NOT NULL,
    memo TEXT,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (senderId) REFERENCES users(id),
    FOREIGN KEY (receiverId) REFERENCES users(id)
);

Recurring Donations Table

CREATE TABLE recurring_donations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    donorId INTEGER NOT NULL,
    recipientId INTEGER NOT NULL,
    amount REAL NOT NULL,
    frequency TEXT NOT NULL,
    nextExecutionDate DATETIME NOT NULL,
    status TEXT DEFAULT 'active',
    executionCount INTEGER DEFAULT 0,
    FOREIGN KEY (donorId) REFERENCES users(id),
    FOREIGN KEY (recipientId) REFERENCES users(id)
);

πŸ› οΈ Development

Project Structure

Stellar-Micro-Donation-API/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/           # Configuration files
β”‚   β”œβ”€β”€ middleware/       # Express middleware
β”‚   β”œβ”€β”€ routes/           # API route handlers
β”‚   β”‚   β”œβ”€β”€ app.js
β”‚   β”‚   β”œβ”€β”€ donation.js
β”‚   β”‚   β”œβ”€β”€ wallet.js
β”‚   β”‚   β”œβ”€β”€ stream.js
β”‚   β”‚   └── stats.js
β”‚   β”œβ”€β”€ services/         # Business logic services
β”‚   β”‚   β”œβ”€β”€ StellarService.js
β”‚   β”‚   β”œβ”€β”€ MockStellarService.js
β”‚   β”‚   └── RecurringDonationScheduler.js
β”‚   β”œβ”€β”€ scripts/          # Database scripts
β”‚   β”‚   └── initDB.js
β”‚   └── utils/            # Utility functions
β”‚       └── database.js
β”œβ”€β”€ data/                 # SQLite database files
β”œβ”€β”€ docs/                 # Documentation
β”œβ”€β”€ tests/                # Test files
└── package.json

Environment Variables

Create a .env file in the project root:

STELLAR_NETWORK=testnet
HORIZON_URL=https://horizon-testnet.stellar.org
PORT=3000
API_KEYS=your-api-key-here

Required at startup:

  • API_KEYS (must include at least one comma-separated key)
  • ENCRYPTION_KEY (required only when NODE_ENV=production)

Validated at startup (if provided):

  • PORT must be an integer from 1 to 65535
  • STELLAR_NETWORK must be one of testnet, mainnet, futurenet
  • MOCK_STELLAR must be true or false
  • HORIZON_URL must be a valid URL

πŸ§ͺ Testing

Run tests:

npm test

Run specific test file:

npm test -- tests/integration.test.js

Test Recurring Donations

node test-recurring-donations.js

πŸ“š Documentation

πŸ”§ Configuration

Stellar Network

The API can work with both Stellar testnet and mainnet. Configure via environment variables:

  • Testnet (default): For development and testing
  • Mainnet: For production use

Recurring Donation Scheduler

The scheduler runs automatically when the server starts and checks for due donations every 60 seconds. It can be configured in src/services/RecurringDonationScheduler.js.

🀝 Contributing

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

πŸ“ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

πŸ“ž Support

For issues and questions:


Built with ❀️ using Node.js and Stellar