Repository navigation
feat(keys): index SnapshotTaken events and serve GET /keys/:key_id/sn… - #1212
Merged
Chucks1093 merged 1 commit intoOct 27, 2026
Merged
Conversation
Contributor
Author
|
Hi maintainer, kindly review and merge |
Member
✅ PR Ready for ReviewDue to a recent malware incident affecting our repositories, all PRs are required to pass automated security checks before being reviewed for merging. This PR has passed all automated checks:
Next step: Please share this PR link in our Telegram group so a maintainer can review and merge it: https://t.me/c/accesslayerorg/8514 |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… Closes #1112
Summary
Indexes SnapshotTaken contract events into a new key_snapshots table and exposes daily snapshot history per key, powering the historical performance charts on the frontend.
Changes
• Database: new KeyAnalyticsSnapshot model (key_snapshots) plus migration. One row per key per UTC day with price, volume, holder_count and market_cap, unique on (creatorId, snapshotDate), with CHECK constraints against negative values.
• Indexer: key-snapshot-indexer.service.ts handles SnapshotTaken and SNAPSHOT_TAKEN, accepting snake_case or camelCase fields. It upserts with ON CONFLICT and only overwrites when the incoming event is newer by (ledger, eventIndex), so replays and out-of-order delivery are idempotent. Malformed events go to the DLQ, and DB errors rethrow so the ledger cursor does not advance. Wired into the pipeline dispatcher next to TWAPUpdated.
• API: GET /keys/:key_id/snapshots?range=7d|30d|90d|all returns daily snapshots oldest first: date, price, volume, holder_count, market_cap. Big numbers are returned as strings. Ranges count back from the current UTC date (today included), and the default is 30d. Keys with no snapshots return an empty array, unknown keys return 404, and invalid ranges return 422.
• Cache: responses are cached under key-snapshots:{keyId}:{range} for 5 minutes. Each new SnapshotTaken event invalidates all four ranges for that key, after the write is durable.
Acceptance criteria
• SnapshotTaken events indexed with all four metrics
• Snapshots endpoint returns correct data for all supported ranges
• Empty array returned for keys with no snapshots yet
• Cache invalidates correctly on new snapshot event
• Cached requests served under 200ms (covered by a test)
Testing
• 37 new tests across key-snapshot-indexer.service.test.ts and keys/tests/key-snapshots.test.ts: parsing, idempotency, DLQ, error propagation, every range, empty array, 404 and 422, cache hits, and end-to-end invalidation
• tsc passes and the existing keys tests still pass