Comprehensive backend service for Dash providing:
- π Real-time Price Feeds (Pyth Network Oracle)
- βοΈ Price Signing for on-chain verification
- π Relay Service for gasless transactions
- π€ Automated Order Execution (Limit orders, Grid trading, TP/SL)
- π Position Monitoring with auto-liquidation
- π² One Tap Profit betting settlement
- β Pyth Network Integration - Cryptographically verified price feeds
- β Multi-Asset Support - 12 assets (BTC, ETH, SOL, AVAX, NEAR, BNB, XRP, AAVE, ARB, DOGE, LINK, MATIC)
- β Price Signing - ECDSA signatures for on-chain verification
- β WebSocket Broadcasting - Real-time price updates every 5 seconds
- β Binance Fallback - Automatic fallback if Pyth unavailable
- β Limit Order Keeper - Auto-executes limit orders when price triggers
- β Grid Trading Bot - Manages grid trading sessions
- β TP/SL Monitor - Auto-executes take-profit and stop-loss orders
- β Tap-to-Trade Executor - Fast backend-only order execution
- β Position Monitor - Auto-liquidates undercollateralized positions
- β One Tap Profit Settlement - Automatic bet settlement
- β Relay Service - Gasless transactions with USDC gas payment
- β RESTful API - Comprehensive endpoints for all features
- β WebSocket Server - Real-time updates for prices and positions
- β TypeScript - Type-safe development with full type coverage
- Node.js >= 18.x
- npm atau yarn
- Install dependencies:
npm install- Setup environment variables:
cp .env.example .envEdit .env sesuai kebutuhan:
PORT=3001
NODE_ENV=development
DEBUG=trueDefault feeds are defined in code, but you can add/override feeds via env:
# Option A: Individual env vars (read all keys that start with PYTH_FEED_)
PYTH_FEED_BTC=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43
PYTH_FEED_EURUSD=0xa995d00bb36a63cef7fd2c287dc105fc8f3d93779f062f09551b0af3e81ec30b
# Option B: Provide all feeds as a JSON object (SYMBOL -> priceId)
# PYTH_FEEDS={\"BTC\":\"0x...\",\"ETH\":\"0x...\",\"XAUUSD\":\"0x...\"}Precedence: PYTH_FEED_* overrides PYTH_FEEDS, both override defaults. Set an empty value (e.g. PYTH_FEED_SOL=) to disable a default feed.
npm run devServer akan running di http://localhost:3001
# Build TypeScript to JavaScript
npm run build
# Run production
npm start- Backend cron calls
streamToVault()onSTABILITY_FUND_ADDRESSevery 6 hours using the relayer/keeper wallet (RELAY_PRIVATE_KEYandKEEPER_ADDRESS). - Interval can be adjusted with
VAULT_STREAM_INTERVAL_MINUTES(default 360 minutes = 6 hours).
GET http://localhost:3001/api/price/allResponse:
{
"success": true,
"data": {
"BTC": {
"symbol": "BTC",
"price": 97234.56,
"confidence": 45.32,
"expo": -8,
"timestamp": 1704567890123,
"source": "pyth",
"publishTime": 1704567890
},
"ETH": { ... },
...
},
"count": 12,
"timestamp": 1704567890123
}GET http://localhost:3001/api/price/current/BTCResponse:
{
"success": true,
"data": {
"symbol": "BTC",
"price": 97234.56,
"confidence": 45.32,
"expo": -8,
"timestamp": 1704567890123,
"source": "pyth"
},
"timestamp": 1704567890123
}GET http://localhost:3001/healthResponse:
{
"success": true,
"service": "Dash Backend",
"uptime": 123.456,
"priceService": {
"status": "connected",
"lastUpdate": 1704567890123,
"assetsMonitored": 12
},
"timestamp": 1704567890123
}Connect to: ws://localhost:3001/ws/price
Message Format:
{
"type": "price_update",
"data": {
"BTC": {
"symbol": "BTC",
"price": 97234.56,
"confidence": 45.32,
"timestamp": 1704567890123,
"source": "pyth"
},
"ETH": { ... }
},
"timestamp": 1704567890123
}const ws = new WebSocket('ws://localhost:3001/ws/price');
ws.onopen = () => {
console.log('Connected to Pyth price feed');
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'price_update') {
const prices = message.data;
// Update your UI with Pyth Oracle prices
console.log('BTC Price from Pyth:', prices.BTC.price);
console.log('Confidence:', prices.BTC.confidence);
// Display as yellow line on TradingView chart
updateChartWithOraclePrice(prices.BTC.price);
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('Disconnected from price feed');
};async function getPythPrices() {
try {
const response = await fetch('http://localhost:3001/api/price/all');
const result = await response.json();
if (result.success) {
const prices = result.data;
console.log('All prices from Pyth:', prices);
return prices;
}
} catch (error) {
console.error('Failed to fetch prices:', error);
}
}| Symbol | Pyth Price ID | Binance Symbol |
|---|---|---|
| BTC | 0xe62df6... | BTCUSDT |
| ETH | 0xff61491... | ETHUSDT |
| SOL | 0xef0d8b6... | SOLUSDT |
| AVAX | 0x93da335... | AVAXUSDT |
| NEAR | 0xc415de8... | NEARUSDT |
| BNB | 0x2f95862... | BNBUSDT |
| XRP | 0xec5d399... | XRPUSDT |
| AAVE | 0x2b9ab1e... | AAVEUSDT |
| ARB | 0x3fa4252... | ARBUSDT |
| DOGE | 0xdcef50d... | DOGEUSDT |
| LINK | 0x8ac0c70... | LINKUSDT |
| MATIC | 0x5de33a9... | MATICUSDT |
βββββββββββββββββββββββββββββββββββββββββββ
β Pyth Network Oracle β
β (Hermes API - hermes.pyth.network) β
ββββββββββββββββββ¬βββββββββββββββββββββββββ
β Price Feeds (5s interval)
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β Dash Backend Service β
β βββββββββββββββββββββββββββββββββββββ β
β β PythPriceService β β
β β - Fetch all assets prices β β
β β - Fallback to Binance β β
β β - Real-time updates β β
β βββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββ ββββββββββββββββ β
β β REST API β β WebSocket β β
β ββββββββββββ ββββββββββββββββ β
ββββββββββββββ¬ββββββββββββββ¬βββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββββββββββββββββββββββββ
β Frontend Application β
β - TradingView Chart β
β - Yellow line for Oracle price β
β - Real-time price updates β
ββββββββββββββββββββββββββββββββββββββββββ
- Ubuntu/Debian VPS (recommended: 2GB RAM minimum)
- Node.js 18+ installed
- Domain name (optional, for HTTPS)
- Firewall configured
# Update system
sudo apt update && sudo apt upgrade -y
# Install Node.js 18+
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Install PM2 (process manager)
sudo npm install -g pm2
# Install Nginx (reverse proxy)
sudo apt install -y nginx# Clone repository
cd /var/www
sudo git clone <your-repo-url> dash-trade
cd dash-trade/dash-be
# Install dependencies
npm install
# Build TypeScript
npm run build
# Setup environment variables
sudo nano .env
# Copy all values from .env.example and configureEdit .env file with production values:
# Server
PORT=3001
NODE_ENV=production
DEBUG=false
# Smart Contracts (from deployment)
CHAIN_ID=84532
RPC_URL=https://sepolia.base.org
MARKET_EXECUTOR_ADDRESS=0x...
POSITION_MANAGER_ADDRESS=0x...
LIMIT_EXECUTOR_ADDRESS=0x...
TAP_TO_TRADE_EXECUTOR_ADDRESS=0x...
ONE_TAP_PROFIT_ADDRESS=0x...
VAULT_POOL_ADDRESS=0x...
STABILITY_FUND_ADDRESS=0x...
USDC_TOKEN_ADDRESS=0x...
DASH_STAKING_ADDRESS=0x...
USDC_PAYMASTER_ADDRESS=0x...
# ... copy the rest from deployments JSON
# Wallets (IMPORTANT: Keep private keys secure!)
PRICE_SIGNER_PRIVATE_KEY=0x...
RELAY_PRIVATE_KEY=0x...
LIMIT_ORDER_KEEPER_PRIVATE_KEY=0x...# Start backend with PM2
pm2 start npm --name "dash-backend" -- start
# Save PM2 config
pm2 save
# Enable PM2 startup on boot
pm2 startup
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp $HOME
# Check status
pm2 status
pm2 logs dash-backendCreate Nginx config:
sudo nano /etc/nginx/sites-available/dash-backendAdd configuration:
server {
listen 80;
server_name api.yourdomain.com; # or use IP address
location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# WebSocket support
location /ws {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}Enable site:
# Create symlink
sudo ln -s /etc/nginx/sites-available/dash-backend /etc/nginx/sites-enabled/
# Test Nginx config
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx# Install Certbot
sudo apt install -y certbot python3-certbot-nginx
# Get SSL certificate
sudo certbot --nginx -d api.yourdomain.com
# Auto-renewal is configured automatically
sudo certbot renew --dry-run# Allow SSH, HTTP, HTTPS
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
# Check status
sudo ufw status# View logs
pm2 logs dash-backend
pm2 logs dash-backend --lines 100
# Restart service
pm2 restart dash-backend
# Stop service
pm2 stop dash-backend
# Monitor resources
pm2 monit
# Update code
cd /var/www/dash-trade/dash-be
sudo git pull
npm install
npm run build
pm2 restart dash-backend- Private Keys: NEVER commit
.envto Git. Store securely - Relay Wallet: Fund with Base Sepolia ETH for gas fees
- Firewall: Only expose ports 22, 80, 443
- SSL: Always use HTTPS in production
- Monitoring: Setup uptime monitoring (UptimeRobot, etc.)
π° Wallet Funding:
- Relay wallet needs Base Sepolia ETH (~0.1 ETH recommended)
- Price signer wallet doesn't need ETH (signing only)
- Keeper wallet needs Base Sepolia ETH for order execution
π Auto-Restart on Crash: PM2 automatically restarts the service if it crashes. Check logs:
pm2 logs dash-backend --errIssue: Port 3001 already in use
sudo lsof -ti:3001 | xargs sudo kill -9
pm2 restart dash-backendIssue: Nginx 502 Bad Gateway
# Check if backend is running
pm2 status
# Check backend logs
pm2 logs dash-backend
# Restart Nginx
sudo systemctl restart nginxIssue: Out of Memory
# Increase Node.js memory limit
pm2 start npm --name "dash-backend" --max-memory-restart 500M -- startIssue: WebSocket connection fails
- Check firewall allows connections
- Verify Nginx WebSocket config
- Check frontend connects to correct wss:// URL (not ws://)
Update dash-fe/.env:
# Development
NEXT_PUBLIC_BACKEND_URL=http://localhost:3001
NEXT_PUBLIC_WS_URL=ws://localhost:3001
# Production
NEXT_PUBLIC_BACKEND_URL=https://api.yourdomain.com
NEXT_PUBLIC_WS_URL=wss://api.yourdomain.com- Pyth Oracle menyediakan harga yang cryptographically verified
- Price update interval: 5 detik (lebih cepat dari TradingView free plan)
- Confidence interval menunjukkan akurasi harga
- Fallback ke Binance jika Pyth tidak available
# Windows
netstat -ano | findstr :3001
taskkill /PID <PID> /F
# Linux/Mac
lsof -ti:3001 | xargs kill -9- Check firewall settings
- Make sure backend is running
- Verify correct URL and port
MIT
Dash Development Team