Skip to content

[Frontend] Optimistic UI State Engine & IndexedDB Offline Stream Cache #1473

Closed

Description

@blurbeast

Overview & Background

In Web3 applications, blockchain transactions and indexer synchronization introduce latency (typically 3 to 10 seconds on Stellar/Soroban). Users who submit transactions (create stream, withdraw, top-up, pause) experience an awkward delay where the UI remains stale or shows blocking loading spinners until the transaction confirms and the backend indexer processes the event. Implementing an Optimistic UI state engine paired with client-side IndexedDB caching delivers instantaneous feedback and fluid user experience.


Detailed Problem Statement

  1. Perceived Latency & UI Freezes:
    • Submitting a withdrawal currently forces the user to wait for ledger inclusion and API polling before the balance updates on screen.
  2. Cold-Start Latency:
    • Every page refresh queries the backend REST endpoints from scratch, causing empty skeletons or layout shifts while data is fetched.
  3. Flaky Network Disconnects:
    • Users on intermittent mobile connections or high-latency networks cannot view their existing streams or draft transactions if offline.

Technical Specification & Architecture

1. IndexedDB Persistence Layer (Dexie.js or idb-keyval)

Implement frontend/src/lib/storage/stream-cache.ts:

  • Schema:
    • cached_streams: Primary store keyed by streamId, indexed by sender, recipient, tokenAddress, updatedAt.
    • optimistic_mutations: Store of pending user actions (CREATE, WITHDRAW, TOP_UP, CANCEL, PAUSE).
  • Cache lifecycle:
    • On app load: Read instantly from IndexedDB to hydrate React Query / Zustand stores in < 15ms.
    • Background sync: Query backend API / SSE / WebSocket in the background and reconcile stale entries.

2. Optimistic Mutation Engine

  • When a user submits an action (e.g. withdraw(streamId, amount)):
    • Generate an ephemeral optimistic patch updating stream.withdrawnAmount += amount and stream.claimableAmount -= amount.
    • Instantly re-render the UI with optimistic state and attach a subtle "Pending Ledger Confirmation" pulse badge.
    • Store the optimistic patch in optimistic_mutations.
  • Reconciliation:
    • When the indexer event arrives via SSE or transaction hash is confirmed by RPC, replace the optimistic state with the canonical on-chain record.
    • If the transaction reverts or fails on-chain, automatically roll back the optimistic patch and display an actionable toast error.

3. Continuous Ticker Synchronization

  • Ensure the real-time claimable balance ticker integrates seamlessly with optimistic mutations without jumping backward or jittering.

Target Files

  • frontend/src/lib/storage/stream-cache.ts
  • frontend/src/context/StreamContext.tsx
  • frontend/src/hooks/useOptimisticStream.ts
  • frontend/src/components/dashboard/StreamCard.tsx
  • frontend/src/__tests__/optimistic-ui.test.tsx

Acceptance Criteria

  • Application loads and renders cached streams from IndexedDB immediately on launch without waiting for network responses.
  • Submitting stream mutations (withdraw, top-up, cancel) immediately updates the UI without blocking spinners.
  • Pending optimistic state displays a non-intrusive status indicator.
  • Canonical indexer updates silently reconcile optimistic values without UI flicker.
  • Simulated transaction failures trigger a clean rollback to prior state and show explanatory error toast.

Activity

  1. Ayomide3271 commented on Sep 27, 2026

    @Ayomide3271

    @Ayomide3271 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hello Maintainer, I’ve taken the time to carefully examine the issue, understand the requirements, and consider the expected outcome. After reviewing everything, I’m confident that I have the relevant knowledge and experience to work on this task effectively. I’m ready to take ownership of the issue, investigate it thoroughly, and work toward a proper and maintainable solution. Kindly assign the issue to me so I can begin. Thank you for your consideration. I look forward to contributing to the project.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Ayomide3271 to this issue.

  2. bright5455 commented on Sep 27, 2026

    @bright5455
    Contributor

    @bright5455 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    hello maintainer

    i have read and understand the issue
    im a backend engineer with five years of eperience,with my level of experience,i believe i can deliver a quality PR
    please kindly assign this task to me

    Thank you

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @bright5455 to this issue.

  3. drips-wave commented on Sep 29, 2026

    @drips-wave

    Congratulations, @bright5455! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

    🧑‍💻 @bright5455: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  4. grantfox-oss commented on Oct 10, 2026

    @grantfox-oss

    🎉 This issue has been marked as completed on GrantFox!

    @bright5455's PR #1585 was approved and merged by @K1NGD4VID.

    🏆 @bright5455: You earned 40 FoxPoints for this contribution! Your current tier: Builder (1,249 total points). Track your full progress on GrantFox.

    👏 Great work, @bright5455! Keep contributing to LabsCrypt.

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