Skip to content

Make the reconciliation job compare DB vs on-chain truth using escrow-invariants and mapping #229

@greatest0fallt1me

Description

@greatest0fallt1me

Description

app/api/internal/reconciliation/route.ts only tallies stream-status counts; it never compares stored balances against on-chain state, even though lib/onChainClient.ts, lib/dbClient.ts, escrow-invariants.ts, and mapping.ts exist precisely for that (note lib/onChainClient.ts has an intentional released_amount mismatch for stream_2). Implement a real nightly reconciliation that detects drift. This is a backend on-chain reconciliation feature.

Requirements and Context

  • For each stream, fetch on-chain state via lib/onChainClient.ts, DB state via lib/dbClient.ts, map fields using mapping.ts, and assert escrow-invariants.ts (balances never negative, released ≤ total).
  • Report a structured discrepancies[] list (streamId, field, dbValue, onChainValue) in the response and persist a run record via dbClient.updateLastRunStatus.
  • Honor the existing dryRun flag and internal-service auth.
  • Must be secure, tested, and documented
  • Should be efficient and easy to review

Suggested Execution

  1. Fork the repo and create a branch
    git checkout -b feature/reconciliation-drift-detection
  2. Implement changes
    • app/api/internal/reconciliation/route.ts
    • lib/onChainClient.ts, lib/dbClient.ts, mapping.ts, escrow-invariants.ts (wire together)
    • scripts/reconcile-streams.ts — invoke via npm run reconcile
  3. Test and commit
    • npm test -- app/api/internal/reconciliation/route.test.ts
    • Cover edge cases: the seeded stream_2 mismatch, missing on-chain record, dryRun
    • Include test output and notes in the PR

Example commit message

feat: detect DB vs on-chain balance drift in reconciliation

Acceptance Criteria

  • Response includes a discrepancies[] array for mismatched streams
  • Seeded stream_2 mismatch is detected
  • Escrow invariants asserted; negative/over-release flagged
  • Coverage ≥ 90% for the reconciliation route and libs

Guidelines

  • Minimum 90% test coverage including drift and missing-record cases
  • Clear documentation and inline comments
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programapiAPI endpoint/contract workbackendBackend service workdatabaseDatabase schema/migrations
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions