Skip to content

feat(backend): implement BridgePayoutEvent webhook listener (#33) - #997

Open
acmvisuals1 wants to merge 1 commit into
Fracverse:masterfrom
acmvisuals1:feat/issue-33-bridge-payout-event-listener
Open

feat(backend): implement BridgePayoutEvent webhook listener (#33)#997
acmvisuals1 wants to merge 1 commit into
Fracverse:masterfrom
acmvisuals1:feat/issue-33-bridge-payout-event-listener

Conversation

@acmvisuals1

@acmvisuals1 acmvisuals1 commented Jul 24, 2026

Copy link
Copy Markdown

Closes #942

Summary

Adds a background daemon that polls the Stellar Horizon API for Soroban contract events with the BridgePay topic, persists them with exactly-once semantics, and fans out bridge.payout webhook dispatches to all active registered endpoints.

Changes

backend/src/bridge_event_listener.rs (new)

  • BridgeListenerConfig::from_env() — reads STELLAR_CONTRACT_ID, STELLAR_HORIZON_URL, BRIDGE_LISTENER_POLL_INTERVAL_SECS; returns None when contract ID is absent so the daemon self-disables
  • BridgeEventListenerService — poll loop with tokio::select! and a watch::Receiver shutdown channel for graceful termination
  • run_once() — full poll → parse → persist → webhook cycle
  • latest_persisted_ledger() — fixed fetch_optional → fetch_one (COALESCE always returns a row; old code skipped every poll cycle silently)
  • fetch_events() — Horizon HTTP poll with correct 19-digit zero-padded cursor format {seq:019}-0000000000
  • persist_event() — INSERT … ON CONFLICT DO NOTHING for exactly-once DB persistence keyed on (contract_id, ledger_sequence, tx_hash, event_index)
  • enqueue_webhook() — fan-out via WebhookDispatcherService
  • extract_str() / extract_i64() — handle plain strings and Horizon wrapper objects ({id:…}, {value:…})
  • 23 inline unit tests

backend/src/api.rs

  • GET /api/bridge/events — paginated query with contract_id, owner_address, from_ledger, to_ledger, page, page_size filters
  • BridgeEventQuery, BridgeEventRow, BridgeEventsResponse types
  • Route added to public_routes in create_router

backend/src/lib.rs

  • pub mod bridge_event_listener
  • pub use BridgeEventListenerService, BridgeListenerConfig

backend/src/main.rs

  • Creates tokio::sync::watch shutdown channel
  • Starts BridgeEventListenerService gated on STELLAR_CONTRACT_ID being set
  • Graceful shutdown tied to Ctrl-C via axum::serve::with_graceful_shutdown

backend/migrations/20260724000000_create_bridge_payout_events.{up,down}.sql

  • bridge_payout_events table with unique index for dedup
  • Indexes on ledger_sequence for efficient pagination

backend/tests/bridge_event_listener_test.rs (new)

  • 29 integration tests: config validation, topic filtering, parse_event fidelity, extract helpers, cursor format, HTTP endpoint routing, method rejection, Horizon page deserialisation

.github/workflows/backend.yml

  • postgres:16 with --health-cmd pg_isready health check
  • DATABASE_URL env var set at job level
  • cargo test --all-features

pr #942

…e#33)

Closes Fracverse#33

## Summary
Adds a background daemon that polls the Stellar Horizon API for Soroban
contract events with the BridgePay topic, persists them with exactly-once
semantics, and fans out bridge.payout webhook dispatches to all active
registered endpoints.

## Changes

### backend/src/bridge_event_listener.rs (new)
- BridgeListenerConfig::from_env() — reads STELLAR_CONTRACT_ID,
  STELLAR_HORIZON_URL, BRIDGE_LISTENER_POLL_INTERVAL_SECS; returns None
  when contract ID is absent so the daemon self-disables
- BridgeEventListenerService — poll loop with tokio::select! and a
  watch::Receiver<bool> shutdown channel for graceful termination
- run_once() — full poll → parse → persist → webhook cycle
- latest_persisted_ledger() — fixed fetch_optional → fetch_one (COALESCE
  always returns a row; old code skipped every poll cycle silently)
- fetch_events() — Horizon HTTP poll with correct 19-digit zero-padded
  cursor format {seq:019}-0000000000
- persist_event() — INSERT … ON CONFLICT DO NOTHING for exactly-once DB
  persistence keyed on (contract_id, ledger_sequence, tx_hash, event_index)
- enqueue_webhook() — fan-out via WebhookDispatcherService
- extract_str() / extract_i64() — handle plain strings and Horizon wrapper
  objects ({id:…}, {value:…})
- 23 inline unit tests

### backend/src/api.rs
- GET /api/bridge/events — paginated query with contract_id, owner_address,
  from_ledger, to_ledger, page, page_size filters
- BridgeEventQuery, BridgeEventRow, BridgeEventsResponse types
- Route added to public_routes in create_router

### backend/src/lib.rs
- pub mod bridge_event_listener
- pub use BridgeEventListenerService, BridgeListenerConfig

### backend/src/main.rs
- Creates tokio::sync::watch shutdown channel
- Starts BridgeEventListenerService gated on STELLAR_CONTRACT_ID being set
- Graceful shutdown tied to Ctrl-C via axum::serve::with_graceful_shutdown

### backend/migrations/20260724000000_create_bridge_payout_events.{up,down}.sql
- bridge_payout_events table with unique index for dedup
- Indexes on ledger_sequence for efficient pagination

### backend/tests/bridge_event_listener_test.rs (new)
- 29 integration tests: config validation, topic filtering, parse_event
  fidelity, extract helpers, cursor format, HTTP endpoint routing,
  method rejection, Horizon page deserialisation

### .github/workflows/backend.yml
- postgres:16 with --health-cmd pg_isready health check
- DATABASE_URL env var set at job level
- cargo test --all-features
@drips-wave

drips-wave Bot commented Jul 24, 2026

Copy link
Copy Markdown

@acmvisuals1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@ONEONUORA

Copy link
Copy Markdown
Contributor

@acmvisuals1
Run cargo fmt --all -- --check

@ONEONUORA

Copy link
Copy Markdown
Contributor

@acmvisuals1
Update on this PR
You will be unassigned at the end of today

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.

[Backend] Issue #33: Implement Webhook Listener for BridgePayoutEvent

2 participants