Skip to content

feat: implement stablecoin peg deviation benchmark route#107

Open
barnabasolutayo-lgtm wants to merge 1 commit into
Miracle656:mainfrom
barnabasolutayo-lgtm:stablecoin-peg-deviation
Open

feat: implement stablecoin peg deviation benchmark route#107
barnabasolutayo-lgtm wants to merge 1 commit 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

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)

1 participant