Skip to content

feat: implement stablecoin peg deviation benchmark route#107

Open
barnabasolutayo-lgtm wants to merge 2 commits into
Miracle656:mainfrom
barnabasolutayo-lgtm:stablecoin-peg-deviation
Open

feat: implement stablecoin peg deviation benchmark route#107
barnabasolutayo-lgtm wants to merge 2 commits into
Miracle656:mainfrom
barnabasolutayo-lgtm:stablecoin-peg-deviation

Conversation

@barnabasolutayo-lgtm

Copy link
Copy Markdown

##closes #98

# Description

Implements a new route `/benchmark/:asset?target=USD` to track stablecoin peg deviations. The endpoint calculates deviation in basis points and computes rolling 24-hour statistics.

## Key Features

- **Dynamic Pair Resolution**: Resolves the matching watched pair dynamically using `getActivePairs()` from the active registry.
- **Directional Precision**: Automatically checks if the queried asset is the base or counter asset (i.e. `assetA` or `assetB`) in the pair configuration and applies the inverse mathematical operation (`1 / price`) when required.
- **Basis Points (BP) Precision**: Computes deviation using `(Price - 1.0) * 10000`.
- **24-Hour Rolling Statistics**: Performs a single database query to extract:
  - Overall latest price
  - 24h rolling min deviation
  - 24h rolling max deviation
  - 24h rolling max absolute deviation
  - 24h rolling average deviation
  - 24h rolling sample count
- **Strict Response Validation**: Registers a custom Fastify schema `benchmarkResponseSchema` to validate the route payload structure and prevent regression in dev/test setups.

## Files Modified / Added

- **[NEW]** `src/routes/benchmark.ts`: Main route handler and math calculation layer.
- **[NEW]** `src/__tests__/benchmark.test.ts`: Unit tests validating error responses, forward/inverse math calculations, and empty db cases.
- **[MODIFY]** `src/api/schemas.ts`: Added `benchmarkResponseSchema` for response validation.
- **[MODIFY]** `src/index.ts`: Registered the benchmark routes.

## Test Verification

Verified using Vitest:
- Added and ran `src/__tests__/benchmark.test.ts`.
- Verified that all unit tests pass (149/149).

```bash
npx vitest run src/__tests__/benchmark.test.ts

@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@barnabasolutayo-lgtm Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Miracle656 Miracle656 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: registerBenchmarkRoutes(app) wired in index.ts; /benchmark/:asset queries price_points with valid pair_key/timestamp columns, parameterized, 404 on unwatched pair; includes a test. Clean merge with main. Closes #98.

@Miracle656 Miracle656 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The route itself looks good — registerBenchmarkRoutes wired in index.ts, /benchmark/:asset over price_points (valid pair_key/timestamp), 404 on unwatched pair, parameterized, with a test. The only blocker is that it now conflicts with sibling routes that merged since you branched (both touch src/index.ts route registration and src/api/schemas.ts).

A quick rebase resolves it:

git fetch origin
git rebase origin/main
# in src/index.ts keep ALL registerXxxRoutes(app) lines (union), same for schemas.ts
git push --force-with-lease

Then I'll merge. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/benchmark (stablecoin peg deviation)

2 participants