DEV Community

Superwavvy
Superwavvy

Posted on

Building a DeFi Price Alert Bot: What I Learned Building a Production System on My Phone

TL;DR: I built a price monitoring bot for Ethereum tokens that runs on my phone and costs $0/month. Monitors any token, sends instant Telegram alerts, logs everything to Supabase. Here's what I learned about DeFi automation, databases, and building real systems under constraints.


The Problem

You're a trader. You watch 5 altcoins. Market moves 20% while you sleep.

You miss it.

Existing solutions suck:

  • TradingView: $10-100/month
  • Binance alerts: Lag, limited tokens
  • Manual monitoring: Burnout guaranteed

What if you built your own?

That's this project.


Architecture Decisions (The Foundation)

Before coding, I made 8 key decisions:

1. Price Check Frequency: 15 Minutes

  • Every 1-min check for 100 tokens = 144k API calls/day (too much)
  • 15 min = 192 calls/day (well within free tier)
  • Sufficient to catch real moves, not micro-movements

2. Data Source: CoinGecko + Alchemy

  • CoinGecko: Free price feeds, no auth needed
  • Alchemy: Setup for future on-chain reads (liquidity, swaps, etc.)
  • Fallback strategy built in

3. Storage: Supabase PostgreSQL

Why not Firebase or local JSON?

  • SQL = powerful queries later
  • Managed PostgreSQL = no server ops
  • Free tier: 500MB + generous API
  • RLS (Row Level Security) for safety

4. Token Management: JSON Config

Simple, human-editable, version-controllable. Can add tokens without redeploying.

5. Triggers: Both % Changes + Price Targets

  • % changes catch momentum (alert if ±5%, ±10%, ±25%)
  • Price targets catch ranges (buy @ $X, sell @ $Y)
  • Having both = flexibility

6. Alerts: Telegram

  • Instant push notifications (can't miss)
  • Works on any device
  • Rich formatting support
  • Free and reliable

7. Error Handling: Alert on Critical Failures

Don't fail silently. Don't spam every error. Alert when >50% of checks fail.

8. Deployment: Termux on Phone

  • Zero server costs
  • Always online (phone on charger)
  • Can deploy and code from same device

Lesson: Spend 30 mins deciding architecture. Saves hours of rework.


The Tech Stack

Node.js - Event-driven runtime, Termux support

node-cron - Runs your function every 15 minutes without a server

cron.schedule('*/15 * * * *', async () => {
  await runCheck();
});
Enter fullscreen mode Exit fullscreen mode

CoinGecko API - Zero auth, instant price feeds

const response = await fetch(
  `https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=${address}&vs_currencies=usd`
);
Enter fullscreen mode Exit fullscreen mode

Supabase - Managed PostgreSQL with instant backups

  • Why PostgreSQL? SQL means flexible queries later
  • Why managed? You're paying for not running ops

Telegram Bot API - Push notifications

  • Simple HTTP calls
  • Instant delivery
  • Formatted messages

How It Works (Every 15 Minutes)

1. Scheduler wakes up
2. Load token config (JSON)
3. For each token:
   → Fetch current price from CoinGecko
   → Query last stored price from Supabase
   → Calculate % change
   → Check triggers (% change + targets)
   → If triggered → Send Telegram alert
   → Log to database
4. Wait 15 minutes
5. Repeat
Enter fullscreen mode Exit fullscreen mode

Trigger Logic

Type 1: Percentage Changes

"percentageChanges": [5, 10, 25]
Enter fullscreen mode Exit fullscreen mode
  • Price moved 5%+ → Alert
  • Useful for: Catching momentum, volatility spikes

Type 2: Price Targets

"priceTargets": [
  { "price": 5.0, "type": "sell" },
  { "price": 8.0, "type": "buy" }
]
Enter fullscreen mode Exit fullscreen mode
  • Price crossed $5 selling level → Alert
  • Useful for: Range trading, DCA strategies, support/resistance

Example:

  • Previous price: $4.50
  • Current price: $5.10
  • Buy target: $5.00
  • Result: ✅ Alert "Crossed $5.00 buy level"

The Errors I Hit

Error 1: RLS Permissions

permission denied for table price_history
Enter fullscreen mode Exit fullscreen mode

Fix: Grant anonymous API key permissions

GRANT SELECT, INSERT, UPDATE ON public.price_history TO anon;
GRANT USAGE, SELECT ON SEQUENCE public.price_history_id_seq TO anon;
Enter fullscreen mode Exit fullscreen mode

Lesson: Security-first defaults are good, but require understanding.


Error 2: Telegram Connection Timeout

Problem: Telegram API wasn't responding during startup

Fix: Made Telegram optional

if (TELEGRAM_BOT_TOKEN && TELEGRAM_CHAT_ID) {
  await sendPriceAlert(message);
} else {
  console.log('📝 [Mock Alert]', message);
}
Enter fullscreen mode Exit fullscreen mode

Lesson: External dependencies should fail gracefully.


Error 3: CoinGecko Rate Limiting

Error: HTTP 429 Too Many Requests
Enter fullscreen mode Exit fullscreen mode

Fix: Respect rate limits. Added delays between calls.

Lesson: Test with real APIs under real constraints.


Performance After 24 Hours

✅ Price checks: 96 (every 15 min)
✅ Alerts triggered: 3
✅ Price snapshots logged: 96
✅ Errors: 0
✅ Memory used: 5-10 MB
✅ CPU during check: <5%
✅ CPU idle: 0%
✅ Termux stability: Perfect
Enter fullscreen mode Exit fullscreen mode

This tells me: System is reliable, minimal resource usage, scales to many more tokens.


What I'd Do Differently

1. Start with E2E tests
I built everything before testing end-to-end. Caught permission errors late.

2. Mock external APIs during development
Calling CoinGecko 50 times during dev = wasted API calls

3. Use TypeScript
JavaScript is fast, but TypeScript catches type errors before runtime

4. Add logging levels
Current code logs everything equally. Need filtering (debug/info/error)

5. Document while building
Docs after = forgotten context. Docs while = fresh thinking


What's Next

Phase 2: Liquidation Detector

  • Monitor collateral prices on Aave, Compound
  • Alert when liquidation price approaches
  • Rank by urgency

Phase 3: Arbitrage Spotter

  • Compare prices across DEXs
  • Detect profitable spreads
  • Calculate slippage

Key Takeaways

1. Architecture Matters

30 mins of architecture decisions saved hours of rework.

2. Pick Managed Services

Supabase, Alchemy, CoinGecko, Telegram = all managed. I don't run anything.

3. Fail Gracefully

One error shouldn't break the whole system.

4. Data is Everything

Storing every price check means I can analyze later.

5. Deploy Early

Deploy to production ASAP. Production teaches you fast.


Code Structure

All modular and focused:

bot.js          (100 lines) - Scheduler + orchestration
db.js           (80 lines)  - Database I/O
triggers.js     (60 lines)  - Price evaluation logic
notifications.js (50 lines) - Telegram sending
config.json     (20 lines)  - Token watchlist
schema.sql      (40 lines)  - Database setup
Enter fullscreen mode Exit fullscreen mode

Total: ~350 lines of code. You can understand the entire system in 30 mins.


Is This Production-Ready?

For solo/small trading: Yes.

  • Runs 24/7 on phone
  • Zero downtime
  • Costs $0/month
  • Scales to ~50 tokens

For institutional: Not yet.

  • No redundancy
  • No disaster recovery
  • No audit logs

For learning: Absolutely. This is the foundation.


Final Thought

The hardest part of building isn't coding. It's deciding what to build and why.

I spent 20% of time on architecture. That decision paid for itself 5x over.

Next time you build, do the same:

  1. Define the problem
  2. Lock architecture
  3. Build the skeleton
  4. Deploy early
  5. Iterate based on reality

Don't overthink. Build. Learn. Repeat.


Built by: Wavvy

Timeline: Sept 1, 2026 (1 day start-to-production)

Status: Live and monitoring UNI + AAVE

Top comments (0)