feat: Implement Contract Event Indexer for Soroban Events - #91
Merged
Wilfred007 merged 4 commits intoMar 6, 2026
Conversation
- Add background service to poll Soroban RPC for contract events - Create contract_events and indexer_state database tables - Implement REST API endpoints for querying indexed events - Add idempotent event processing with duplicate detection - Support graceful recovery from last indexed ledger - Include comprehensive tests and documentation Acceptance criteria met: ✓ Background worker polls/streams contract events from Soroban RPC ✓ Events stored in contract_events table with all required fields ✓ REST endpoint GET /api/events/:contractId returns paginated events ✓ Duplicate events idempotently skipped on re-indexing ✓ Worker restarts gracefully from last indexed ledger on crash
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.
Overview
This PR implements a production-ready contract event indexer that streams Soroban contract events from the Stellar RPC and persists them to PostgreSQL, providing a reliable, queryable audit trail for the frontend.
Closes #78
Implementation Summary
✅ All Acceptance Criteria Met
contract_eventstable withcontract_id,event_type,payload, andledger_sequenceGET /api/events/:contractIdreturns paginated eventsKey Features
Files Changed (15 files, 2,484+ lines)
Core Implementation
src/services/contractEventIndexer.ts(306 lines) - Background indexer servicesrc/controllers/contractEventController.ts(200 lines) - REST API controllersrc/routes/contractEventRoutes.ts(56 lines) - Express routessrc/types/contractEvent.ts(63 lines) - TypeScript typessrc/db/migrations/016_create_contract_events.sql(50 lines) - Database schemaTests
src/services/__tests__/contractEventIndexer.test.ts(229 lines)src/controllers/__tests__/contractEventController.test.ts(228 lines)Documentation
CONTRACT_EVENT_INDEXER.md(288 lines) - Complete technical documentationCONTRACT_EVENT_INDEXER_QUICKSTART.md(177 lines) - Quick start guideCONTRACT_EVENT_INDEXER_ARCHITECTURE.md(358 lines) - Architecture diagramsIMPLEMENTATION_SUMMARY.md(200 lines) - Implementation overviewPR_CHECKLIST.md(312 lines) - Review checklistIntegration
src/app.ts- Added route registrationsrc/index.ts- Added indexer initialization and shutdown.env.example- Added configuration variablesAPI Endpoints
1. Get Events by Contract
2. Get All Events
3. Get Indexer Status
Database Schema
contract_events Table
id- Serial primary keyorganization_id- Foreign key to organizationscontract_id- Stellar contract address (VARCHAR 56)event_type- Event type extracted from topics (VARCHAR 100)payload- Full event data (JSONB)ledger_sequence- Blockchain ledger number (BIGINT)transaction_hash- Transaction hash (VARCHAR 64)event_index- Event index within transaction (INTEGER)ledger_closed_at- Blockchain timestampindexed_at- Indexing timestamp(contract_id, transaction_hash, event_index)indexer_state Table
Configuration
Add to
.env:Testing
Run tests:
Deployment Steps
.envnpm run migratenpm run devGET /api/events/indexer/statusTechnical Highlights
ON CONFLICT DO NOTHINGprevents duplicatesDocumentation
Complete documentation provided:
Breaking Changes
None. This is a new feature with no impact on existing functionality.
Checklist
Screenshots/Examples
See
CONTRACT_EVENT_INDEXER_QUICKSTART.mdfor API examples and testing instructions.Related Issues
Closes #78