DEV Community

Cover image for Building a Real-Time Funding Rate Monitor for DreamDEX (Somnia Testnet)
vault vult
vault vult

Posted on

Building a Real-Time Funding Rate Monitor for DreamDEX (Somnia Testnet)

As part of the DreamDEX Perps Builder Bounty, I set out to solve a simple problem for perpetual traders: staring at funding rate dashboards 24/7 is exhausting.

Traders need to know when a funding rate flips (from positive to negative, or vice versa), but they shouldn't have to monitor it manually.

To solve this, I built the DreamDEX Funding Monitor — a lightweight, automated alert bot and web dashboard that monitors the Somnia Hideki Testnet in real-time.

🏗️ The Architecture

I wanted to keep the architecture clean and decoupled. The project is split into two distinct parts:

  1. The Engine: A background worker that polls the @somnia-chain/markets-sdk every 60 seconds to fetch live rates, check for flips, and fire off Discord alerts.
  2. The Frontend: A live Express.js-powered dashboard that visualizes the data without ever touching the engine's core logic.

The Tech Stack

  • Backend / Engine: Node.js & TypeScript
  • Web Server: Express.js + CORS
  • Web3 Integration: @somnia-chain/markets-sdk
  • Security: dotenv (to keep Discord Webhook URLs safe)
  • Frontend: Vanilla HTML/CSS/JS with a custom dark-mode grid layout.

🧠 How the Engine Works

The core engine (funding-alert-bot.ts) connects to the Somnia Testnet Indexer. Smart contracts return raw integers, so I had to handle the decimal precision to make the data human-readable:

// The SDK returns raw integers (e.g., 100000000000000)
const rawRate = Number(market.fundingRate || market.currentFundingRate || 0);

// Dividing by 1e18 converts it to a standard percentage (0.0001 = 0.01%)
const currentRate = rawRate / 1e18; 
Enter fullscreen mode Exit fullscreen mode

The bot stores the previous funding rates in memory. When it detects a sign flip (e.g., prevRate > 0 and currentRate < 0), it triggers an Axios POST request to a Discord Webhook, instantly pinging the trader.

🛡️ Security First

Because this is a public hackathon repository, I couldn't hardcode my Discord Webhook URL into the code. I used dotenv to load environment variables, ensuring the secret stays hidden from GitHub crawlers and malicious actors.

📸 The Dashboard

By decoupling the engine, I was able to spin up an Express server that serves a live-updating, dark-mode dashboard. It refreshes every 5 seconds to pull the latest state from the background engine.

🔗 The Core Engine X (Twitter) Post

I've been documenting the build process on X/Twitter. You can check out the exact post about the core engine, Express.js decoupling, and dotenv integration here:

👉 https://x.com/TrAdArSOY/status/2116170770464526425 👈

🚀 Check out the Code

The full MVP is open-source and available on GitHub.

👉 https://github.com/vaultvult/dreamdex-testnet-proof

Built for the @DreamDEXSomnia Perps Builder Bounty.

Top comments (0)