Skip to content

fix(withdraw): gate balance increment on event INSERT rowcount to pre… - #1410

Open
Martinsbab wants to merge 7 commits into
LabsCrypt:mainfrom
Martinsbab:fix/issue-1216-idempotent-withdraw
Open

Martinsbab wants to merge 7 commits into
LabsCrypt:mainfrom
Martinsbab:fix/issue-1216-idempotent-withdraw

Conversation

@Martinsbab

Copy link
Copy Markdown

Summary

Fixes an accounting bug where duplicate or retried withdrawal requests could increment withdrawnAmount more than once for the same claimable withdrawal.

Problem

The withdrawal flow uses a deterministic transaction hash for simulated withdrawals. The StreamEvent upsert correctly handles duplicate events by treating an existing event as a no-op.

However, the adjacent prisma.stream.update was not protected by the same idempotency check. As a result, retrying the same withdrawal request could increment withdrawnAmount repeatedly even though the withdrawal event had already been recorded.

Changes

  • Made the withdrawal accounting update idempotent.
  • Ensured duplicate withdrawal requests cannot double-count withdrawnAmount.
  • Kept the event recording and balance update consistent.
  • Added integration coverage for repeated withdrawal requests.
  • Verified the behavior against the existing withdrawal flow and database patterns.

Testing

  • Added/updated integration tests covering duplicate withdrawal requests.
  • Verified that calling withdraw twice for the same claimable window increases withdrawnAmount only once.
  • Ran the relevant project validation checks.

Issue

Closes #1216

Martinsbab and others added 7 commits August 30, 2026 23:06
…vent double-counting

The withdraw handler atomically incremented withdrawnAmount on every request,
regardless of whether the corresponding StreamEvent already existed. Because
sorobanWithdraw returns a deterministic txHash ('simulated-withdraw-' +
streamId), retries and concurrent duplicate requests would each re-increment
the balance — a real fund-accounting bug.

Move the StreamEvent creation inside the $transaction using a conditional
INSERT with WHERE NOT EXISTS + RETURNING. The INSERT's rowcount (1 = new,
0 = duplicate) gates the balance UPDATE, guaranteeing exactly-once accounting
per claimable window. This is race-condition-safe because the unique
constraint on (transactionHash, eventType) prevents duplicate events at the
database level, and the rowcount is evaluated within the same transaction.

Updates existing tests in eventRace.test.ts and withdraw.handler.test.ts,
and adds two new integration tests proving idempotent withdrawal behavior.

Closes LabsCrypt#1216

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
…elope

Restore the withdraw handler idempotency tests to match the
constitent error envelope produced by sendApiError (code + message)
and the PR's 'gates balance increment on event INSERT rowcount'
integration test (recover a missing firstClaimed declaration and
replace the response-amount double-counting assertion, which can
never be less than the full claimable amount, with an assertion
that the DB balance is unchanged on a duplicate).

Closes LabsCrypt#1216 LabsCrypt#1410

Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
The Prisma schema defined IndexerDeadLetterEvent twice (lines 71 and 164),
causing  to fail with P1012 ('model already exists'). The
second definition (with eventType/txHash/ledgerSequence/payload) supersedes
the first, so the duplicate is removed while keeping the complete model
so the Prisma client and npm run build work.

This unblocks the Backend CI and Backend Docker Image CI build steps.

Co-Authored-By: Codebuff <noreply@codebuff.com>
The simulation rewrite (LabsCrypt#1847) removed the balance-writing SQL but left the
test's raw-SQL mock referencing a deleted `insertedHashes` set, and dropped
the `nowSec` declaration that a later assertion still uses. Both broke
`tsc` in Backend CI.

Remove the unreachable INSERT/UPDATE mock (the route never runs raw SQL, and
the test asserts that), declare `nowSec` once per test, and set the claimable
mock explicitly in the three tests that need it. `vi.clearAllMocks()` does
not clear return values, so the 409 case's `actionable: false` was leaking
into later tests and turning expected 200s into 409s.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>

This branch has not been deployed

No deployments
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.

[Audit] Duplicate/retried withdraw requests double-count withdrawnAmount

2 participants