Skip to content

feat: implement event subscription and real-time event listener#78

Open
Kheshie1 wants to merge 1 commit intosorosave-protocol:mainfrom
Kheshie1:feat/event-subscription
Open

feat: implement event subscription and real-time event listener#78
Kheshie1 wants to merge 1 commit intosorosave-protocol:mainfrom
Kheshie1:feat/event-subscription

Conversation

@Kheshie1
Copy link

Summary

Implements event subscription and real-time event listener as described in #33.

/claim #33

Changes

New: src/events.tsSoroSaveEventListener

  • Polls Soroban RPC getEvents endpoint for contract events at configurable interval (default 5s)
  • Parses raw events into typed objects: ContributionEvent, PayoutEvent, GroupCreatedEvent, MemberJoinedEvent
  • onEvent(eventType, callback) — subscribe to specific event types or '*' for all
  • unsubscribe(id) / unsubscribeAll() — cleanup methods
  • Cursor persistence via latestCursor / setCursor() so restarts resume without replay
  • Re-entrance protection and graceful error handling

Updated: src/types.ts

  • Added SoroSaveEventType enum (contribution, payout, group_created, member_joined)
  • Added typed event interfaces with - Added typed event interfaces with - Added typed event interfaces with - Added typed event interfaces with src/client.ts
  • Added onEvent() convenience method that auto- Added onEvent() convenience method that auto- Added onEvent() convenbscriptions remain
  • Added - Added - Added - Added - Addvanced configuration

Updated: src/index.ts

  • Exports all new types and the SoroSaveEventListener class

New: t### New: t### New: t### New: t### New: t### New: t### New: `t### New: rsor persistence, event parsing for all 4 types, edge cases (malformed XDR, missing values), and topic name aliasing

Usage

import { SoroSaveClient, SoroSaveEventType } from '@sorosave/sdk';

const client = new SoroSaveClient(config);

// Subscribe to contributions
const subId = client.onEvent(SoroSaveEventType.Contribution, (event) => {
  console.log(`${event.member} contributed ${event.amount} to group ${event.groupId}`);
});

// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: cleanu// Later: aveEventType } from '@sorosave/sdk';

const listener = new SoroSaveEventListener({
  rpcUrl: 'https://soroban-testnet.stellar.org',
  contractId: 'CABC...',
  networkPassphrase: Networks.TESTNET,
  pollIntervalMs: 3000,
});

listener.onEvent('*', (event) => conlistener.onEvent('*', (event) => conlistener.onEventor for restart resilience
const cursor = listener.latestCursor;
// On restart: listener.setCursor(cursor);

Tests

All 24 tests pass (21 new + 3 existing):

✓ tests/batch.test.ts (2)
✓ tests/events.test.ts (21)
✓ tests/in✓ tests/in✓ tests/in✓ tests/uthored-By: Oz <oz-agent@warp.dev>

Adds SoroSaveEventListener class that polls Soroban RPC getEvents
endpoint for contract events and dispatches typed callbacks.

- Add onEvent(eventType, callback) method with subscription IDs
- Support event types: contribution, payout, group_created, member_joined
- Poll Horizon at configurable interval with cursor persistence
- Add unsubscribe() and unsubscribeAll() cleanup methods
- Parse event data into typed ContributionEvent, PayoutEvent, etc.
- Integrate with SoroSaveClient via onEvent/unsubscribeEvent convenience methods
- 21 new tests covering parsing, subscriptions, edge cases

Closes sorosave-protocol#33

Co-Authored-By: Oz <oz-agent@warp.dev>
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.

1 participant