Skip to content

feat(oracle): add price deviation safety check worker#124

Merged
PeterOche merged 2 commits intodegenspot:mainfrom
phertyameen:priceFeedVerifier
Mar 24, 2026
Merged

feat(oracle): add price deviation safety check worker#124
PeterOche merged 2 commits intodegenspot:mainfrom
phertyameen:priceFeedVerifier

Conversation

@phertyameen
Copy link
Copy Markdown
Contributor

Summary

Implements an automated safety guard that continuously compares stored
oracle prices against CoinGecko reference prices. If any asset deviates
beyond the configured threshold, signing is immediately halted and an
alert is emitted.

How it works

  1. A @Cron worker fires every 60 s (configurable via env)
  2. Fetches the latest stored oracle price per symbol from the DB
  3. Calls CoinGecko's /simple/price endpoint (single batched request)
  4. Computes percentage deviation for each symbol
  5. If any symbol exceeds the threshold - halts signing and fires a
    structured error log for downstream alerting (PagerDuty, Slack, etc.)
  6. Every check (breach or not) is persisted to price_deviation_log

New files

  • price-deviation.config.ts - threshold %, cron expression, symbol→CoinGecko ID map
  • price-deviation-log.entity.ts - TypeORM audit log entity
  • coingecko.service.ts - batched CoinGecko HTTP wrapper
  • price-deviation.service.ts - deviation logic, halt flag, alert emission
  • price-deviation.worker.ts - @Cron scheduler driving the checks
  • price-deviation.module.ts - wires everything together
  • price-deviation.service.spec.ts - unit tests

Environment variables

Variable Default Description
PRICE_DEVIATION_THRESHOLD_PERCENT 5 Max allowed % deviation
PRICE_DEVIATION_CRON */60 * * * * * Worker schedule

Integration steps

  • Import PriceDeviationModule in AppModule
  • Add PriceDeviationLog to TypeORM entities array
  • Register your OraclePrice entity in the module
  • Inject PriceDeviationService into your signing service and guard
    on isSigningHalted()
  • Wire clearHalt() to an admin/ops endpoint for manual recovery
  • Configure logger transport to forward PRICE_DEVIATION_BREACH
    alerts to your alerting stack

Testing

  • Unit tests pass (npm run test)
  • Manually trigger a breach by setting threshold to 0 and verify
    signing halts
  • Verify price_deviation_log rows are written each cycle

Closes #119

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 24, 2026

@phertyameen 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

@PeterOche PeterOche merged commit d546f5a into degenspot:main Mar 24, 2026
3 checks passed
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.

Historical Oracle Price Feed Verifier

2 participants