Skip to content

feat: add TWAP price calculation and caching service - #978

Open
shogun444 wants to merge 1 commit into
accesslayerorg:mainfrom
shogun444:feat/twap-price-service
Open

shogun444 wants to merge 1 commit into
accesslayerorg:mainfrom
shogun444:feat/twap-price-service

Conversation

@shogun444

Copy link
Copy Markdown
Contributor

Summary

  • Implemented TWAP calculation service across 1h, 4h, and 24h windows using prior-seed carry-forward price snapshots and graceful spot fallback for keys with 0 snapshots (src/modules/keys/key-twap.service.ts).
  • Added Redis caching with window-matched TTLs (3600s, 14400s, 86400s) and automatic read-through on cache misses (src/constants/redis.constants.ts).
  • Created 5-minute background computation job sweeping active keys with per-key error isolation, plus immediate backfill triggered on keyEventEmitter key registrations (src/jobs/twap-computation.job.ts).
  • Added endpoint GET /keys/:keyId/price/twap?window=1h|4h|24h returning { twap, spotPrice, deltaPct, window, computedAt, stale }, flagging data as stale when older than 10 minutes (src/modules/keys/keys.routes.ts).
  • Wired job start/stop lifecycle in src/server.ts and added TWAP_COMPUTATION_ENABLED / TWAP_COMPUTATION_INTERVAL_MINUTES to src/config.schema.ts and .env.example.

Testing

  • pnpm lint
  • pnpm build (passed on all new/touched files; full build has pre-existing Prisma/implicit-any errors)
  • pnpm exec prisma generate when schema or generated types changed

Test results:

  • pnpm test -- src/modules/keys/key-twap.service.test.ts src/jobs/twap-computation.job.test.ts --runInBand (2 suites, 15/15 passed)
  • Regression suites: key-price-history, detect-price-movements, price-history-cleanup, config (4 suites, 19/19 passed)
  • eslint and prettier clean on all touched files

Checklist

Closes #963

@drips-wave

drips-wave Bot commented Sep 27, 2026

Copy link
Copy Markdown

@shogun444 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.

Add TWAP price calculation and caching service for creator keys

1 participant