160 lines
2.9 KiB
Markdown
160 lines
2.9 KiB
Markdown
# Uphold currency rate bot for interview
|
|
This project includes a simple API to checkout statistics and health monitoring (requires running DB)
|
|
|
|
## Requirements
|
|
|
|
- Node.js >= v20
|
|
- Docker and Docker Compose (optionally, Podman and Podman compose )
|
|
- PostgreSQL (optional, runs in-memory without it)
|
|
|
|
## Quick Start
|
|
|
|
### 1. Install Dependencies
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
### 2. Run the Bot
|
|
|
|
**With default parameters, no DB or API:**
|
|
```bash
|
|
node index.js
|
|
```
|
|
or
|
|
|
|
```bash
|
|
npm start
|
|
```
|
|
|
|
**With custom parameters:**
|
|
```bash
|
|
node index.js --pairs BTC-USD,ETH-USD --interval 10000 --threshold 0.05
|
|
```
|
|
|
|
or
|
|
|
|
```bash
|
|
npm start -- --pairs BTC-USD,ETH-USD --interval 10000 --threshold 0.01
|
|
```
|
|
|
|
**With environment file:**
|
|
```bash
|
|
node --env-file=.env.example index.js
|
|
```
|
|
|
|
### 3. Run with Docker/Podman Compose (includes DB and API)
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
````
|
|
|
|
Edit .env with your settings:
|
|
|
|
```
|
|
POSTGRES_USER=uphold
|
|
POSTGRES_PASSWORD=uphold
|
|
POSTGRES_DB=uphold_db
|
|
PAIRS=BTC-USD,ETH-USD
|
|
INTERVAL=5000
|
|
THRESHOLD=0.01
|
|
```
|
|
|
|
```
|
|
# Start services
|
|
docker compose up -d --build
|
|
|
|
```
|
|
or
|
|
|
|
```
|
|
podman compose up -d --build
|
|
```
|
|
|
|
# View logs
|
|
|
|
```
|
|
docker compose logs -f uphold_bot_1
|
|
```
|
|
|
|
or
|
|
|
|
```
|
|
podman compose logs -f uphold_bot_1
|
|
```
|
|
|
|
## Configuration
|
|
|
|
| Parameter | Flag | Environment | Default | Description |
|
|
|-----------|------|-------------|---------|-------------|
|
|
| Pairs | `-p, --pairs` | `PAIRS` | `BTC-USD` | Comma-separated currency pairs |
|
|
| Interval | `-i, --interval` | `INTERVAL` | `5000` | Uphold API retrieval interval in milliseconds |
|
|
| Threshold | `-t, --threshold` | `THRESHOLD` | `0.01` | Alert difference percentage |
|
|
|
|
## Running Tests
|
|
|
|
```bash
|
|
npm test
|
|
```
|
|
|
|
## API
|
|
### Endpoints
|
|
|
|
**Health Check**
|
|
```bash
|
|
curl http://localhost:3000/health
|
|
```
|
|
|
|
**Statistics**
|
|
```bash
|
|
curl http://localhost:3000/stats
|
|
```
|
|
|
|
**Alerts**
|
|
```bash
|
|
# Get last 50 alerts (default)
|
|
curl http://localhost:3000/alerts
|
|
|
|
# Get last 100 alerts
|
|
curl http://localhost:3000/alerts?limit=100
|
|
```
|
|
|
|
## Example Output
|
|
|
|
```
|
|
[18:44:51.898] INFO: Bot starting...
|
|
[18:44:51.898] INFO: [WATCHING] BTC-USD | Every 5000ms | Threshold 0.01%
|
|
[18:44:51.898] WARN: [DB] specificiation missing. Running in memor
|
|
[18:44:52.167] INFO: [BTC-USD] Monitoring every 5000ms, +/-0.01%
|
|
[18:44:52.367] INFO: [BTC-USD] Initial price: 90977.65
|
|
[18:45:07.879] INFO: [ALERT] BTC-USD DOWN -0.0116%
|
|
pair: "BTC-USD"
|
|
direction: "DOWN"
|
|
change: "-0.0116%"
|
|
prev: "90977.648894"
|
|
curr: "90967.085674"
|
|
```
|
|
|
|
## Architecture
|
|
- `index.js` - Entry point
|
|
- `src/bot.js` - Bot logic
|
|
- `src/api.js` - Uphold API client
|
|
- `src/db.js` - PostgreSQL persistence layer
|
|
- `src/logger.js` - Simple logging
|
|
- `src/server.js` - API server for docker diagnosis
|
|
- `src/stats.js` - Runtime statistics tracking
|
|
- `tests/bot.test.js` - Tests
|
|
|
|
## Stopping the Bot
|
|
|
|
Press `Ctrl+C` or send `SIGTERM` for graceful shutdown.
|
|
|
|
With Docker:
|
|
```bash
|
|
docker compose down
|
|
```
|
|
or Podman
|
|
```bash
|
|
podman compose down
|
|
podman volume rm uphold_pgdata
|
|
```
|