Deal Bot Backend

July 2, 2026 ยท View on GitHub

NestJS-based API server for automated Filecoin deal creation and monitoring

This is the backend service for Deal Bot, built with NestJS and TypeScript. It handles automated deal creation, retrieval testing, metrics collection, and provides a REST API for the web dashboard.

Tech Stack

  • Framework: NestJS 11.x
  • Language: TypeScript 5.x
  • Database: PostgreSQL + TypeORM
  • Blockchain: Filecoin (via Synapse SDK)
  • Scheduling: NestJS Schedule (cron jobs)
  • API Docs: Swagger/OpenAPI

Prerequisites

  • Node.js 20+
  • pnpm
  • PostgreSQL database (running and accessible)
  • Filecoin wallet with private key

Quick Start

1. Install Dependencies

pnpm install

2. Configure Environment

cp .env.example .env

Edit .env with your configuration (see Configuration below).

3. Set Up Database

Ensure PostgreSQL is running and create a database:

CREATE DATABASE filecoin_dealbot;
CREATE USER dealbot WITH PASSWORD 'dealbot_password';
GRANT ALL PRIVILEGES ON DATABASE filecoin_dealbot TO dealbot;

The application will automatically run migrations on startup.

4. Run the Server

Development Mode (with hot-reload)

pnpm start:dev

Server runs at: http://localhost:8080 API Documentation: http://localhost:8080/api

Production Mode

# Build the application
pnpm build

# Run the built application
pnpm start:prod

Configuration

All configuration is done via environment variables in .env.

๐Ÿ“– For detailed documentation on all environment variables, see docs/environment-variables.md

Database Configuration

VariableDescriptionExample
DATABASE_HOSTPostgreSQL hostlocalhost
DATABASE_PORTPostgreSQL port5432
DATABASE_USERDatabase userdealbot
DATABASE_PASSWORDDatabase passworddealbot_password
DATABASE_NAMEDatabase namefilecoin_dealbot

Server Configuration

VariableDescriptionDefault
NODE_ENVEnvironment modedevelopment
DEALBOT_PORTServer port8080
DEALBOT_HOSTServer hostlocalhost
DEALBOT_ALLOWED_ORIGINSCORS allowed origins (comma-separated)http://localhost:5173,http://127.0.0.1:5173

Blockchain Configuration

VariableDescriptionExample
NETWORKFilecoin networkcalibration or mainnet
WALLET_ADDRESSYour Filecoin wallet address0x...
WALLET_PRIVATE_KEYYour wallet private key (keep secure!)0x...
CHECK_DATASET_CREATION_FEESCheck fees before dataset creationtrue
ENABLE_IPNI_TESTINGIPNI testing mode (disabled/random/always)always
USE_ONLY_APPROVED_PROVIDERSOnly use approved storage providerstrue
PDP_SUBGRAPH_ENDPOINTPDP subgraph API endpoint for PDP proof-set/data-retentionhttps://api.thegraph.com/subgraphs/filecoin/pdp
SUBGRAPH_ENDPOINTSubgraph GraphQL endpoint for sampled-retrieval querieshttps://api.goldsky.com/api/public/<project>/subgraphs/dealbot-subgraph/<version>/gn

Scheduling Configuration (pg-boss)

Dealbot uses pg-boss for all job scheduling. See docs/jobs.md for scheduling behavior and docs/environment-variables.md for defaults and full definitions.

VariableDescriptionRecommended
PROVIDERS_REFRESH_INTERVAL_SECONDSProviders refresh interval (seconds)14400 (4 hours)
DATA_RETENTION_POLL_INTERVAL_SECONDSData retention polling interval (seconds)3600 (1 hour)
DEALS_PER_SP_PER_HOURDeal checks per SP per hour4
RETRIEVALS_PER_SP_PER_HOURRetrieval checks per SP per hour2
DATASET_CREATIONS_PER_SP_PER_HOURDataset creation checks per SP per hour1
PG_BOSS_LOCAL_CONCURRENCYPer-process sp.work concurrency20
JOB_SCHEDULER_POLL_SECONDSScheduler poll interval300
JOB_WORKER_POLL_SECONDSWorker poll interval60
JOB_CATCHUP_MAX_ENQUEUEMax catch-up enqueues per schedule per tick10
JOB_SCHEDULE_PHASE_SECONDSPhase offset for multi-deploy staggering0
DEALBOT_PGBOSS_POOL_MAXMax pg-boss DB connections per instance1
DEALBOT_PGBOSS_SCHEDULER_ENABLEDEnable the enqueue looptrue (api/both), false (worker)
DEALBOT_RUN_MODERun mode for the applicationboth (or split api/worker)

Note: If you run multiple deployments in the same environment, use a non-zero JOB_SCHEDULE_PHASE_SECONDS to stagger schedules.

Dataset Configuration

VariableDescriptionDefault
DEALBOT_LOCAL_DATASETS_PATHLocal path for random dataset storage./datasets
RANDOM_PIECE_SIZESComma-separated byte sizes for uploaded content10485760

Project Structure

backend/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.ts                 # Application entry point
โ”‚   โ”œโ”€โ”€ app.module.ts           # Root module
โ”‚   โ”œโ”€โ”€ app.controller.ts       # Root controller
โ”‚   โ”œโ”€โ”€ config/                 # Configuration modules
โ”‚   โ”œโ”€โ”€ database/               # Database entities and migrations
โ”‚   โ”œโ”€โ”€ deal/                   # Deal creation logic
โ”‚   โ”œโ”€โ”€ deal-addons/            # Deal add-ons and extensions
โ”‚   โ”œโ”€โ”€ retrieval/              # Retrieval testing logic
โ”‚   โ”œโ”€โ”€ retrieval-addons/       # Retrieval add-ons (IPNI)
โ”‚   โ”œโ”€โ”€ metrics/                # Metrics collection and analytics
โ”‚   โ”œโ”€โ”€ scheduler/              # Cron job scheduling
โ”‚   โ”œโ”€โ”€ wallet-sdk/             # Wallet and smart contract operations
โ”‚   โ”œโ”€โ”€ http-client/            # HTTP client utilities
โ”‚   โ””โ”€โ”€ common/                 # Shared utilities and decorators
โ”œโ”€โ”€ test/                       # E2E tests
โ”œโ”€โ”€ dist/                       # Compiled output (after build)
โ””โ”€โ”€ README.md                   # This file

API Documentation

Interactive API documentation is available via Swagger UI:

  • Local: http://localhost:8080/api
  • Production: https://dealbot.filoz.org/api

Development

Available Scripts

pnpm start          # Start in normal mode
pnpm start:dev      # Start with hot-reload (recommended for development)
pnpm start:debug    # Start with debugger
pnpm build          # Build for production
pnpm start:prod     # Run production build

Code Quality

pnpm format         # Format code with Biome
pnpm format:check   # Check formatting
pnpm lint           # Lint and auto-fix
pnpm lint:check     # Check linting
pnpm check          # Run both format and lint
pnpm check:ci       # CI checks (no auto-fix)

Testing

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

Database Schema

The application uses TypeORM with PostgreSQL. Key entities:

  • Deal - Storage deal records
  • Retrieval - Retrieval test records
  • Provider - Storage provider information
  • DailyMetric - Daily performance metrics

Migrations run automatically on application startup.

Troubleshooting

Database Connection Issues

# Check PostgreSQL is running
psql -U dealbot -d filecoin_dealbot

# Verify DATABASE_* environment variables in .env

Port Already in Use

# Change DEALBOT_PORT in .env
# Also update VITE_API_BASE_URL in web/.env

Wallet/Blockchain Issues

# Verify WALLET_ADDRESS and WALLET_PRIVATE_KEY are correct
# Ensure wallet has sufficient USDFC tokens on the specified NETWORK

CORS Errors from Frontend

# Add frontend URL to DEALBOT_ALLOWED_ORIGINS in .env
# Example: DEALBOT_ALLOWED_ORIGINS=http://localhost:5173,http://127.0.0.1:5173

Contributing

See the main README for contribution guidelines.

Resources

License

Dual-licensed: MIT, Apache Software License v2 by way of the Permissive License Stack.