[Backend/Analytics] TimescaleDB Hypertables & Real-Time Protocol TVL and Velocity Aggregation
Overview & Background
Payment streaming protocols operate on continuous flow: total locked collateral (TVL), uncollected claims, and streaming velocity change with every passing second and ledger block. As thousands of streams run concurrently, calculating TVL, daily volume, or historical cashflows by repeatedly aggregating raw transaction tables in PostgreSQL leads to CPU exhaustion and query timeouts. Using TimescaleDB Hypertables with Continuous Aggregates provides instantaneous time-series financial analytics.
Detailed Problem Statement
- Expensive Real-Time Aggregations:
- Querying total protocol TVL or historical streaming velocity requires scanning millions of
StreamandStreamEventrows, taking multiple seconds under load.
- Querying total protocol TVL or historical streaming velocity requires scanning millions of
- Missing Historical Financial Charts:
- The frontend cannot easily render smooth 30-day, 90-day, or 1-year protocol TVL and token distribution charts without pre-aggregated time buckets.
- No DefiLlama / Dune Analytics Compatibility:
- FlowFi lacks a standardized real-time TVL endpoint formatted for DeFi tracking platforms like DefiLlama.
Technical Specification & Architecture
1. TimescaleDB Extension & Hypertables
Enable TimescaleDB extension on PostgreSQL:
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
-- Hypertable for continuous stream flow snapshots
CREATE TABLE stream_flow_snapshots (
time TIMESTAMPTZ NOT NULL,
token_address TEXT NOT NULL,
active_stream_count INT NOT NULL,
total_locked_amount NUMERIC(38, 0) NOT NULL,
cumulative_streamed_amount NUMERIC(38, 0) NOT NULL,
cumulative_withdrawn_amount NUMERIC(38, 0) NOT NULL,
flow_velocity_per_second NUMERIC(38, 0) NOT NULL
);
SELECT create_hypertable('stream_flow_snapshots', 'time');
2. Continuous Aggregates & Compression
- Define materialized continuous views bucketed by
1 hourand1 day:CREATE MATERIALIZED VIEW hourly_protocol_metrics WITH (timescaledb.continuous) AS SELECT time_bucket('1 hour', time) AS bucket, token_address, AVG(total_locked_amount) AS avg_tvl, SUM(flow_velocity_per_second) AS aggregate_velocity, MAX(active_stream_count) AS peak_streams FROM stream_flow_snapshots GROUP BY bucket, token_address; - Configure compression policy on snapshots older than 14 days.
3. TVL & Analytics REST API
GET /api/v1/analytics/tvl: Current TVL partitioned by token asset, denominated in native tokens and USD.GET /api/v1/analytics/historical?period=30d&interval=1d: Time-series data points for charts.GET /api/v1/analytics/defillama: Standardized DefiLlama protocol TVL adapter response.
Target Files
backend/prisma/migrations/20261001000000_add_timescaledb_analytics/migration.sqlbackend/src/services/analytics.service.tsbackend/src/controllers/analytics.controller.tsbackend/src/routes/v1/analytics.routes.tsbackend/tests/analytics.test.ts
Acceptance Criteria
- [ ] TimescaleDB hypertable successfully records snapshots emitted on ledger ingestion cycles.
- [ ] Continuous aggregates compute hourly and daily TVL/velocity without full-table scans.
- [ ] API endpoints respond with time-series historical data in < 50ms.
- [ ] DefiLlama compatible endpoint accurately reports current TVL breakdown.
- [ ] Performance benchmarks demonstrate sub-second response times under 100k simulated historical streams.
@anthonyibam-dotcom has applied to work on this issue as part of the Stellar Wave Program's 9th wave.
I will implement the TimescaleDB hypertable and continuous aggregates for efficient TVL and streaming-velocity analytics.
I will add the /tvl, /historical, and DefiLlama-compatible endpoints and wire them through the analytics service and routes.
I will add API, aggregation, and performance tests covering historical data and 100k simulated streams to verify the sub-second targets.
ℹ️ Repo Maintainers: To accept this application, review their application or assign @anthonyibam-dotcom to this issue.
@sojetunde8 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.
I can fix this.
ℹ️ Repo Maintainers: To accept this application, review their application or assign @sojetunde8 to this issue.
Congratulations, @sojetunde8! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.
🧑💻 @sojetunde8: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.
[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.
🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.
🌊 Happy Wave 🌊
This issue has been marked as completed by a Drips Wave moderator for @sojetunde8 as part of the Stellar Wave Program's 9th Wave 🥳
Moderator's reason: [Auto-assessed] The PR has been merged by a maintainer (mergedAt 2026-10-06T10:36:16Z) and the issue is closed. The code changes comprehensively implement the TimescaleDB hypertable analytics backbone, continuous aggregates, and associated REST API endpoints as specified in Issue #1480.
😎 @sojetunde8: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here.
🧑💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.
ℹ️ Note: This issue was marked complete via moderation. Points were issued even though the GitHub issue remains open.