Skip to content

Feature/cancel stream endpoint 413#444

Merged
ogazboiz merged 1 commit intoLabsCrypt:mainfrom
Omoboi-dev:feature/cancel-stream-endpoint-413
Apr 30, 2026
Merged

Feature/cancel stream endpoint 413#444
ogazboiz merged 1 commit intoLabsCrypt:mainfrom
Omoboi-dev:feature/cancel-stream-endpoint-413

Conversation

@Omoboi-dev
Copy link
Copy Markdown
Contributor

closes #413

This PR implements the POST /v1/streams/:streamId/cancel endpoint, enabling users to cancel active payment streams directly from the backend. The implementation includes the full workflow from request validation and authorization to on-chain execution via the Soroban RPC and subsequent database state synchronization.

Key Changes

1. Soroban Service Enhancement (backend/src/services/sorobanService.ts)

  • submitContractCall: Introduced a new internal helper to handle the complete lifecycle of a Soroban write transaction:
    • Fetches latest account sequence from RPC.
    • Builds the transaction and performs a simulation to retrieve necessary resource footprints and fees.
    • Assembles the transaction with simulation results using rpc.assembleTransaction.
    • Signs the transaction using the backend's secret key.
  • cancelStream: Exposed a method to invoke the cancel_stream function on the smart contract for a specific streamId.

2. Controller Implementation (backend/src/controllers/stream/cancel.ts)

  • cancelStreamHandler: A dedicated controller for the cancellation logic:
    • Fetches stream data from the database to verify ownership.
    • Authorization: Ensures the req.user.publicKey (from JWT/SIWX) matches the stream's sender.
    • State Validation: Prevents cancellation of streams that are already inactive or fully completed (Status 409).
    • Triggers the on-chain contract call and captures the resulting txHash.

3. Repository Layer (backend/src/repositories/stream.repository.ts)

  • Introduced a repository layer for cleaner data access.
  • updateStatus: A helper function to synchronize the database isActive flag once an on-chain operation succeeds.

4. Routing (backend/src/routes/v1/stream.routes.ts)

  • Registered the POST /:streamId/cancel route.
  • Secured the endpoint with authMiddleware to ensure only authenticated users can trigger cancellations.
  • Updated OpenAPI/Swagger documentation for the new endpoint.

5. Testing (backend/tests/integration/streams/cancel.test.ts)

  • Added comprehensive integration tests covering:
    • Success: Valid sender can cancel and receive a txHash.
    • Unauthorized: Different user attempting to cancel (403).
    • Not Found: Attempting to cancel a non-existent stream (404).
    • Conflict: Attempting to cancel an already inactive stream (409).

Acceptance Criteria Verification

  • POST /api/v1/streams/:streamId/cancel route is registered and documented.
  • Validates session via authMiddleware.
  • Confirms caller is the stream sender via DB check.
  • Invokes sorobanService.cancelStream() on-chain.
  • Returns txHash in the JSON response.
  • Updates DB stream record (isActive: false) on success.
  • Handles 404 (not found), 403 (forbidden), and 409 (conflict) errors.
  • Integration tests pass with 100% coverage of the new logic.

Environment Requirements

  • SOROBAN_SECRET_KEY: Must be set in the backend environment to enable transaction signing for on-chain writes.

@ogazboiz
Copy link
Copy Markdown
Contributor

hey, main was failing CI from broken auth imports + frontend parse errors. fixed and pushed to main now. please rebase to pick up the fixes:

git fetch upstream
git rebase upstream/main
git push --force-with-lease

if there's a conflict, resolve it locally and we'll review once CI is green.

@Omoboi-dev Omoboi-dev force-pushed the feature/cancel-stream-endpoint-413 branch from a483503 to dd745ee Compare April 30, 2026 12:34
@ogazboiz ogazboiz merged commit 96cb907 into LabsCrypt:main Apr 30, 2026
8 of 9 checks passed
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.

[Backend] Wire cancel stream endpoint to Soroban contract

2 participants