Skip to content

feat(keys): index SnapshotTaken events and serve GET /keys/:key_id/sn… - #1212

Merged
Chucks1093 merged 1 commit into
accesslayerorg:mainfrom
shinorh247:feat/issue-1112-key-snapshots
Oct 27, 2026
Merged

Chucks1093 merged 1 commit into
accesslayerorg:mainfrom
shinorh247:feat/issue-1112-key-snapshots

Conversation

@shinorh247

Copy link
Copy Markdown
Contributor

… 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

@shinorh247

Copy link
Copy Markdown
Contributor Author

Hi maintainer, kindly review and merge

@Chucks1093

Copy link
Copy Markdown
Member

✅ PR Ready for Review

Due 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:

  • ✅ Security scan — no malicious code detected
  • ✅ No merge conflicts
  • ✅ CI checks passed

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

@Chucks1093
Chucks1093 merged commit 840c630 into accesslayerorg:main Oct 27, 2026
2 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.

Implement key analytics snapshot indexer and historical data REST API

2 participants