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
- Fork the repo and create a branch
git checkout -b feature/reconciliation-drift-detection
- 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
- 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
Guidelines
- Minimum 90% test coverage including drift and missing-record cases
- Clear documentation and inline comments
- Timeframe: 96 hours
Description
app/api/internal/reconciliation/route.tsonly tallies stream-status counts; it never compares stored balances against on-chain state, even thoughlib/onChainClient.ts,lib/dbClient.ts,escrow-invariants.ts, andmapping.tsexist precisely for that (notelib/onChainClient.tshas an intentionalreleased_amountmismatch forstream_2). Implement a real nightly reconciliation that detects drift. This is a backend on-chain reconciliation feature.Requirements and Context
lib/onChainClient.ts, DB state vialib/dbClient.ts, map fields usingmapping.ts, and assertescrow-invariants.ts(balances never negative, released ≤ total).discrepancies[]list (streamId, field, dbValue, onChainValue) in the response and persist a run record viadbClient.updateLastRunStatus.dryRunflag and internal-service auth.Suggested Execution
app/api/internal/reconciliation/route.tslib/onChainClient.ts,lib/dbClient.ts,mapping.ts,escrow-invariants.ts(wire together)scripts/reconcile-streams.ts— invoke vianpm run reconcilenpm test -- app/api/internal/reconciliation/route.test.tsstream_2mismatch, missing on-chain record, dryRunExample commit message
Acceptance Criteria
discrepancies[]array for mismatched streamsstream_2mismatch is detectedGuidelines