Skip to content

[Backend] Transaction Simulation & Pre-Flight Fee Estimation API for Client-Side Signing #1443

Description

@blurbeast

Overview & Background

In accordance with Issue #1274 and the security audit in docs/audits/1274-keeper-key-blast-radius.md, FlowFi is deprecating centralized backend keeper key signing in favor of client-side signing (Freighter, Lobstr, xBull). However, building and signing Soroban transactions directly in browser extensions requires running a Soroban RPC transaction simulation (simulateTransaction) to obtain ledger footprint keys, resource limits (CPU/memory instructions), and the minimum base fee.


Detailed Problem Statement

  1. Client Complexity & Duplicate RPC Calls:
    • Forcing every frontend client to implement low-level Soroban transaction simulation results in bloated bundle sizes and inconsistent fee calculations.
  2. Frequent Simulation Failures:
    • Client-side simulation errors often lack human-readable error messages when contracts revert or balances are insufficient.
  3. No Centralized Fee Buffer Policy:
    • Clients need an authoritative backend endpoint to compute simulation footprints with appropriate safety fee buffers.

Technical Specification & Architecture

1. Simulation API Endpoint

POST /api/v1/streams/simulate

Request Body:

{
  "action": "create" | "withdraw" | "cancel" | "top_up" | "batch_withdraw",
  "senderPublicKey": "G...",
  "params": {
    "streamId": "123",
    "recipient": "G...",
    "amount": "1000000000",
    "duration": 2592000
  }
}

Response Body:

{
  "success": true,
  "data": {
    "unsignedXdr": "AAAAAgAAA...",
    "minResourceFee": "15000",
    "recommendedFee": "20000",
    "cpuInstructions": 1420500,
    "memoryBytes": 524000,
    "expiresAtLedger": 482910,
    "simulatedReturn": "100000000"
  }
}

2. Implementation in sorobanService.ts

  • Assembles the contract invocation XDR.
  • Invokes server.simulateTransaction(tx) via the RPC pool.
  • Validates simulation result: if failed, extracts the specific Soroban error code and translates to a typed ApiError with user-friendly remediation hints.
  • Adds recommended 15% resource fee buffer to prevent out-of-gas errors.

Target Files

  • backend/src/routes/v1/stream.routes.ts
  • backend/src/controllers/stream.controller.ts
  • backend/src/services/sorobanService.ts
  • backend/tests/soroban.service.test.ts

Acceptance Criteria

  • Endpoint correctly simulates create, withdraw, cancel, and top_up contract calls.
  • Returns valid base64-encoded unsigned XDR consumable by Freighter wallet.
  • Reverted simulations return appropriate HTTP 400 with decoded contract error messages.
  • Unit tests mock both successful and failed Soroban RPC simulation responses.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions