Skip to content

feat: create StellarService to centralize Horizon client β€” eliminate duplication across modulesΒ #117

Description

@Josue19-08

πŸ”– Title

Create `StellarService` β€” centralize Horizon client instantiation currently duplicated across modules


πŸ“„ Description

`src/blockchain/stellar/` exists in the architecture but contains only a `.gitkeep`. The Stellar Horizon client is instantiated independently in at least two places, with diverging retry strategies and no shared error normalization:

  • `src/modules/transactions/transactions.service.ts` β€” creates its own `StellarSdk.Horizon.Server` instance inline
  • `src/jobs/transaction-status-checker/transaction-status-checker.processor.ts` β€” creates a second independent instance with its own inline retry loop using different timing constants

This duplication means:

  • Horizon URL and network passphrase are read from env vars in two different places
  • Retry/back-off logic differs between the two β€” a bug fix in one doesn't fix the other
  • No single place to add observability hooks, circuit breaking, or connection pooling
  • Unit tests must mock `StellarSdk` globally instead of injecting a service

βœ… Tasks to complete

  • Create `src/blockchain/stellar/stellar.service.ts` β€” wraps `Horizon.Server` with: configurable base URL from `ConfigService`, unified exponential back-off retry (max 3 attempts), normalized error types (`StellarNetworkError`, `TransactionNotFoundError`), and timeout per request
  • Create `src/blockchain/stellar/stellar.module.ts` β€” exports `StellarService` as a global module
  • Refactor `src/modules/transactions/transactions.service.ts` β€” inject `StellarService`; remove inline `Horizon.Server` instantiation
  • Refactor `src/jobs/transaction-status-checker/transaction-status-checker.processor.ts` β€” inject `StellarService`; remove duplicated inline retry loop
  • Update `src/jobs/transaction-status-checker/transaction-status-checker.module.ts` β€” import `StellarModule`
  • Update `src/modules/transactions/transactions.module.ts` β€” import `StellarModule`
  • Add unit tests for `StellarService` in `test/unit/blockchain/stellar/stellar.service.spec.ts` covering: successful fetch, retry on transient error, failure after max retries, transaction not found

πŸ“š Documentation/context for AI

(This link never should removed)
https://github.com/TrustUp-app/TrustUp-API/tree/main/docs

Relevant files:

  • `src/blockchain/stellar/` (currently empty β€” create here)
  • `src/modules/transactions/transactions.service.ts`
  • `src/jobs/transaction-status-checker/transaction-status-checker.processor.ts`
  • `src/jobs/transaction-status-checker/transaction-status-checker.module.ts`
  • `src/modules/transactions/transactions.module.ts`

πŸ—’οΈ Additional notes

  • Follow the same NestJS module pattern used by `SupabaseModule` (`src/database/supabase/`) β€” make it a `@Global()` module so all consumers just import it once
  • Normalized error types should extend `Error` and carry the original Horizon error for logging
  • The retry strategy should use exponential back-off: 500ms β†’ 1000ms β†’ 2000ms with jitter
  • Network passphrase (`STELLAR_NETWORK_PASSPHRASE`) must be added to `src/config/env.ts` validation if not already present

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions