Skip to content

fix: keep the recipient's claimable balance unwithdrawn after a cancel (#542) - #665

Closed
PINYOPATTANAWASANPORN wants to merge 1 commit into
TricklePay:mainfrom
PINYOPATTANAWASANPORN:fix/issue-542
Closed

PINYOPATTANAWASANPORN wants to merge 1 commit into
TricklePay:mainfrom
PINYOPATTANAWASANPORN:fix/issue-542

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown

Summary of Changes

Fixes #542. When a stream is cancelled, applyCancellation in src/repositories/streams.ts now only freezes totalAmount to the vested balance at cancellation (stored.withdrawn + recipientAmount) and sets endTime to cancelledAt, without updating withdrawn. The recipient's vested-but-unwithdrawn tokens remain claimable, accurately reflecting the smart contract's behavior.

Root Cause / Technical Context

Previously, applyCancellation set both totalAmount and withdrawn to frozenTotal, operating under the assumption that cancellation settled the recipient's balance immediately. In reality, the Soroban contract only refunds unvested tokens to the sender, leaving the recipient's balance (recipient_amount) claimable. Prematurely marking withdrawn = frozenTotal zeroed out the derived withdrawable field, and caused subsequent withdrawals to increment withdrawn past totalAmount.

Verification & Testing

  • Unit tests added in tests/repositories/streams.test.ts covering:
    • Freezing totalAmount without setting withdrawn in the update payload.
    • Returning missing when the stream does not exist.
    • Returning duplicate when updateMany matches 0 rows due to an event race.
  • Acceptance criteria verified:
    • Comments in applyCancellation, docs/database-schema.md, and prisma/schema.prisma updated to state "still claimable" instead of "paid".
    • docs/derived-figures.md updated to reflect mathematical parity.
  • Passed all verification gates:
    • npm run typecheck: Exit Code 0 (tsc --noEmit && tsc -p tsconfig.test.json).
    • npm test: 49/49 test files passed, 583/583 tests passed.
    • npm run build: Exit Code 0.
  • Normalized all modified files to LF line endings.

Impact & Compatibility

  • Breaking changes: None.
  • DB migrations required: None.

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.

Stop marking the recipient's claimable balance as withdrawn when a stream is cancelled

2 participants