Skip to content

fix: correct the type errors introduced in #1290 (#1184 #1185 #1186) - #1291

Closed
SagaciousBoss wants to merge 2 commits into
Smartdevs17:mainfrom
SagaciousBoss:fix/payments-payout-reliability-1183-1186
Closed

SagaciousBoss wants to merge 2 commits into
Smartdevs17:mainfrom
SagaciousBoss:fix/payments-payout-reliability-1183-1186

Conversation

@SagaciousBoss

Copy link
Copy Markdown
Contributor

Follow-up to #1290, which merged with four type errors that only the Docker build job (tsc) caught — the backend CI jobs run eslint and vitest, neither of which type-checks.

What was wrong

  • backend/src/services/fiat-crypto.ts — const conversions = Map<string, ConversionRecord>() was missing new. That also made Array.from(conversions.values()) an unknown[], which broke the element types in listConversions.
  • backend/src/services/payouts/payout-engine.ts — evaluatePayouts called the async materialiseBatch without awaiting it, so it pushed a Promise into a PayoutBatch[], and the per-schedule try/catch could not observe a failure.
  • backend/src/routes/payouts.ts — req.params.batchId is typed string | string[] under this Express version. The handlers now resolve the id once through requireId and reuse the validated string, which also removes the repeated param lookup.

Two smaller robustness changes while in the same code:

  • The FX rate lookup and the payout asset-scale lookup use Object.prototype.hasOwnProperty.call rather than comparing the result to undefined, which keeps them honest if a rate is ever legitimately falsy.
  • The settlement amount parse moved into parseSettlementAmount, which collapses every failure mode onto the single INVALID_SETTLEMENT_AMOUNT sentinel instead of relying on definite-assignment analysis across a try/catch.

Scope

Three files, no behaviour change beyond fixing the above. Backend (Node.js 20/22) on #1290 passed, so the test suite already covered this code; this commit makes tsc agree.

Verification

npm run test --workspace backend and npm run lint --workspace backend. The repository-wide tsc build has a large number of pre-existing errors unrelated to this change (missing modules, ungenerated Prisma client, websocket.ts, tax-engine.ts, featureFlagRegistry.ts and others); this commit removes the only ones introduced by #1290.

Closes #1184
Closes #1185
Closes #1186

SagaciousBoss and others added 2 commits October 17, 2026 11:04
…artdevs17#1183 Smartdevs17#1184 Smartdevs17#1185 Smartdevs17#1186)

Payout reliability (Smartdevs17#1185)
- PayoutEngine had no execution path: auto-approved and approved batches were
  stamped completed without anything being sent, executing was unreachable, and
  there was no failure or retry path.
- Add a guarded pending -> executing -> completed|failed transition table; a batch
  is only completed when the rail returns a transaction hash.
- Add an injected executor that receives a stable idempotencyKey on every attempt.
- Add exponential backoff with jitter, capped by maxAttempts. Exhausted batches keep
  their reservation and are released only by an approver, so an unattended retry
  cannot spin against a failing rail.
- Enforce daily/weekly/monthly cadence and key calendar batches by period bucket.
- Reject thresholdAmount of 0 instead of falling through to pay anything.
- Emit payout.created/pending_review/executing/completed/failed/delayed events.
- Isolate one merchant's misconfiguration from the rest of the evaluation pass.
- Add /api/v1/payouts, mounted in index.ts.

Payment correctness (Smartdevs17#1184)
- getCurrentRate returned 1.0 for unquoted pairs, pricing GBP->BTC at par. Unquoted
  pairs are now an error.
- Money was derived with binary floating point; add services/money.ts (bigint minor
  units, half-away-from-zero) and route conversions through it.
- lockRate accepted NaN/negative/infinite amounts.
- processFiatRefund recorded failed, reported success without refunding when no
  payment intent existed, and was not idempotent.
- Revenue pool recipient ratios were validated individually but not in aggregate, so a
  pool could distribute more than 100 percent; recordDistribution was not idempotent
  on txHash; conversion reads returned live internal references.

Payment security (Smartdevs17#1186)
- Every /api/v1/payouts route, including reads, requires an authenticated approver
  principal, checked before any lookup so batch existence cannot be probed.
- Reject reuse of an Idempotency-Key with a different payload (409
  idempotency_key_reuse_mismatch) instead of replaying the earlier response, and
  bound the response cache.
- Enforce asset and amount allowlists in the engine as well as the route, and add
  route-scoped rate limits (120/min, 30/min for approval decisions).

Documentation (Smartdevs17#1183)
- Add docs/PAYOUT_RELIABILITY.md, docs/PAYMENT_CORRECTNESS.md and
  docs/security/payment-security.md, index them, and cross-link PAYOUT_APPROVALS.md.
…rtdevs17#1290

Four defects introduced in Smartdevs17#1290 that the backend lint+test jobs do not catch
because they only run eslint and vitest, neither of which type-checks:

- fiat-crypto.ts: const conversions = Map<string, ConversionRecord>() was missing

ew, which also made Array.from(conversions.values()) an unknown[] and broke
  the element types in listConversions.
- payout-engine.ts: evaluatePayouts called the async materialiseBatch without
  awaiting it, so it pushed a Promise into the returned PayoutBatch[] and the
  try/catch could not observe a per-schedule failure.
- routes/payouts.ts: 
eq.params.batchId is typed string | string[] under this
  Express version. The handlers now resolve the id once through requireId and reuse
  the validated string, which also removes the repeated param lookup.
- Also replaced the === undefined rate/scale lookups with hasOwnProperty checks
  and moved the settlement amount parse into a helper, so neither can trip the
  type checker or trip over a definite-assignment analysis.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 17, 2026

Copy link
Copy Markdown

@SagaciousBoss is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@SagaciousBoss

Copy link
Copy Markdown
Contributor Author

Superseded by a clean branch off main; #1290 is already merged so this branch duplicated it and conflicted.

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.

Strengthen payment security (iteration 11) Improve payout reliability (iteration 11) Fix payment correctness bugs (iteration 10)

1 participant