Skip to content

Add TTL-based caching to TrustFlowClient.getBalance to reduce redundant Horizon calls #142

Description

@meshackyaro

Description

This issue aims to address the implementation of TTL-Cached getBalance.

The core objective is to: Add opt-in TTL-based caching to TrustFlowClient.getBalance (src/client.ts), using the SDK's existing (currently unused) SimpleCache from src/utils/cache.ts, so repeated balance checks for the same address within a short window don't each trigger a fresh server.loadAccount(address) Horizon round-trip. TrustFlowClient.getBalance(address) currently calls this.server.loadAccount(address) on every single invocation with no caching layer — a UI polling balance every few seconds (exactly the pattern src/hooks/useBalance.ts sets up via its useEffect) generates a fresh Horizon request every time even if the balance hasn't changed. src/utils/cache.ts's SimpleCache<K, V> already implements exactly the TTL-expiry primitive this would need, but currently has no real call site anywhere in the codebase.

By completing this feature, we will ensure that the TrustFlow protocol maintains its high standards for security, usability, and decentralized logic. This issue requires careful attention to the SDK Wrapper architecture and adherence to the existing project conventions.

Component

SDK

Difficulty

🟡 Medium

Tasks

  • Review existing Typescript classes (TrustFlowClient, SimpleCache)
  • Implement SDK method for: TTL-based caching on TrustFlowClient.getBalance using SimpleCache, with a sensible default TTL and a way to force a fresh fetch (e.g. a { skipCache?: boolean } option)
  • Ensure Tsup successfully exports to ESM and CJS
  • Update typedoc comments for the new feature
  • Write Jest tests validating cache hits, cache expiry (fake timers), and the force-refresh option
  • Run npm run build to verify standard bundling

Acceptance Criteria

  • Feature accurately implements the objective above
  • A second getBalance call for the same address within the TTL window does not trigger a second Horizon request
  • Any PR that introduces TypeScript errors is automatically blocked
  • Code is properly reviewed and approved by codeowners

Estimated Time

2-3 days

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions