Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address TWAP coverage, contract-view integration, cache invalidation, and documented fallback inconsistencies.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (4)
What changed in this PR
Adds a cached TWAP endpoint for creator keys using historical price snapshots.
Changes:
- Adds validated TWAP route and calculation service.
- Implements Redis caching and invalidation hooks.
- Adds endpoint and caching tests.
| File | Summary |
|---|---|
src/modules/keys/keys.routes.ts |
Registers the TWAP endpoint. |
src/modules/keys/key-twap.service.ts |
Calculates and caches TWAP responses. |
src/modules/keys/__tests__/key-twap.test.ts |
Tests validation, calculation, caching, and invalidation. |
src/modules/indexer/trade-indexer.service.ts |
Invalidates TWAP caches after trades. |
src/modules/indexer/price-snapshot.service.ts |
Invalidates TWAP caches after price updates. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for (let i = 0; i < snapshots.length - 1; i++) { | ||
| const tCurrent = snapshots[i].recordedAt.getTime(); | ||
| const tNext = snapshots[i + 1].recordedAt.getTime(); | ||
| const dt = Math.max(0, tNext - tCurrent); | ||
|
|
| try { | ||
| const { invalidateKeyTwapCache } = await import('../keys/key-twap.service'); | ||
| await invalidateKeyTwapCache(creatorId); | ||
| } catch { | ||
| // Non-critical cache invalidation failure | ||
| } |
| // Log get_twap contract view call with window in ledger units | ||
| logger.info( | ||
| { | ||
| operation: 'get_twap_contract_view', | ||
| keyId: creatorId, |
| cacheSetJson: jest.fn(async (key: string, value: unknown) => { | ||
| redisStore.set(key, JSON.stringify(value)); | ||
| }), |
Closes accesslayerorg#866 - Add GET /api/v1/keys/:keyId/twap supporting 1h, 24h, and 7d time windows - Compute TWAP via trapezoidal integration over historical price snapshot intervals - Cache calculation in Redis for 60 seconds - Invalidate TWAP cache on new trades and price snapshot writes - Validate window query parameters with 422 error on invalid inputs
…lidate cache on first trade
8064ad6 to
8f8acc5
Compare
|
Superseded by #1007, which carries all four issues (#866, #869, #870, #872) on one branch rebased on latest Splitting these four across separate PRs meant they collided with each other: all four touch Closing this one in favour of #1007. |



Summary
Implements a Time-Weighted Average Price (TWAP) endpoint for creator keys, enabling clients to retrieve moving-average benchmark pricing over configured time windows with Redis caching.
Closes #866
Implementation Details
GET /api/v1/keys/:keyId/twap?window=1h|24h|7dcache:keys:twap:{keyId}:{window}).invalidateKeyTwapCachewhen new trades are processed intrade-indexer.service.tsand when new price snapshots are written inprice-snapshot.service.ts.1h,24h,7d), returning 422 for unsupported values.Testing
src/modules/keys/__tests__/key-twap.test.tsverifying:24hwindow.invalidateKeyTwapCache.