Summary
useTransactions currently does a one-shot Horizon fetch. Horizon supports Server-Sent Events (SSE) for streaming new transactions in real time. This hook should wrap that capability.
Implementation Steps
- Create
src/hooks/useHorizonSSE.ts.
- Signature:
useHorizonSSE(publicKey: string | null, network: StellarNetwork, onRecord: (tx: TransactionRecord) => void): { close: () => void }.
- Use
Horizon.Server.transactions().forAccount(publicKey).cursor('now').stream({ onmessage, onerror }) from @stellar/stellar-sdk.
- Call
close() in the useEffect cleanup to prevent memory leaks.
- Map the raw Horizon record to
TransactionRecord using the same mapping logic as useTransactions.
- Export from
src/index.ts.
- Write a unit test using
vi.mock('@stellar/stellar-sdk') to simulate the stream emitting one record and verify onRecord is called.
Definition of Done
Summary
useTransactionscurrently does a one-shot Horizon fetch. Horizon supports Server-Sent Events (SSE) for streaming new transactions in real time. This hook should wrap that capability.Implementation Steps
src/hooks/useHorizonSSE.ts.useHorizonSSE(publicKey: string | null, network: StellarNetwork, onRecord: (tx: TransactionRecord) => void): { close: () => void }.Horizon.Server.transactions().forAccount(publicKey).cursor('now').stream({ onmessage, onerror })from@stellar/stellar-sdk.close()in theuseEffectcleanup to prevent memory leaks.TransactionRecordusing the same mapping logic asuseTransactions.src/index.ts.vi.mock('@stellar/stellar-sdk')to simulate the stream emitting one record and verifyonRecordis called.Definition of Done
onRecordcalled with correctly typedTransactionRecord.