feat(soroban-client): batch ledger entry fetch helper (#222) - #273
Merged
manoahLinks merged 2 commits intoApr 27, 2026
Merged
Conversation
Adds `batchGetLedgerEntries` to the tokenbound SDK. It wraps the RPC's `getLedgerEntries` so callers can read many keys in one workflow: - Chunks input keys over the RPC's per-call cap (`DEFAULT_BATCH_CHUNK_SIZE = 100`). - Runs chunks with bounded concurrency (`DEFAULT_BATCH_CONCURRENCY = 4`). - Aligns the response back to input order. Missing keys (silently omitted by the RPC) become `null` at their original index, so callers can distinguish "not on-chain" from "chunk RPC failed." - Tolerates partial failures: a single chunk's RPC error is captured in the `errors` array along with the input indexes it covered, rather than losing every other chunk's data. - Aggregates `latestLedger` (max across successful chunks) and `found` / `missing` / `failed` counters for monitoring. The helper takes a structural `LedgerEntriesFetcher` (the shape of `rpc.Server`) and uses type-only imports of `@stellar/stellar-sdk` so it remains tree-shakeable and testable without the SDK's runtime (which currently breaks under jsdom without a TextEncoder polyfill). Tests: 12 unit tests covering empty input, single chunk, multi-chunk, missing-key alignment, partial chunk failure, latest-ledger aggregation, all-chunks-fail, duplicate-key handling, stranger-key tolerance, concurrency limit, validation of chunkSize/concurrency, and default chunk size behaviour. Docs: `sdk/README.md` gains a "Batch ledger-entry fetch" section. Closes #222
|
@menawar Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
batchGetLedgerEntriesto the tokenbound SDK, a helper for fetching many ledger entries in one workflow with partial-failure handling.The Soroban RPC's
getLedgerEntries:That makes it awkward to consume directly in any read path that wants to fan-out across many keys (account state for a list of TBAs, contract data for a paginated list of events, etc.). This helper wraps the workflow so callers hand it a flat
xdr.LedgerKey[]and get back an aligned, observable result.Behaviour
chunkSize(defaultDEFAULT_BATCH_CHUNK_SIZE = 100). Override per call if the RPC is configured differently.concurrency(defaultDEFAULT_BATCH_CONCURRENCY = 4).entries[i]corresponds to inputkeys[i]:LedgerEntryResultif the entry was returned.nullif the chunk succeeded but no entry came back for that key (i.e. not present on-chain).undefinedif the chunk that contained that key failed (seeerrors).result.errorsalong with the input indexes they covered. Sibling chunks complete and contribute their entries.latestLedgeris the max across successful chunks;found/missing/failedgive a quick status read suitable for telemetry / UI badges.keyIdis overridable; defaults to the canonicalkey.toXDR("base64").The helper takes a structural
LedgerEntriesFetcher(the shape ofrpc.Server), so it composes with the existingsdk.rpcServerand is trivial to mock. All@stellar/stellar-sdkimports are type-only, so the module is tree-shakeable and the tests run cleanly under jsdom (the existing repo tests break there because the SDK runtime needs aTextEncoderpolyfill).Changes
soroban-client/sdk/src/batchLedgerEntries.tswithbatchGetLedgerEntries,BatchLedgerEntriesOptions,BatchLedgerEntriesResult,BatchLedgerChunkError,LedgerEntriesFetcher, andDEFAULT_BATCH_CHUNK_SIZE/DEFAULT_BATCH_CONCURRENCYconstants.sdk/src/index.ts.__tests__/lib/batchLedgerEntries.test.ts(12 unit tests).sdk/README.mdgains a "Batch ledger-entry fetch" section.Testing
The 12 tests cover:
nullat the right index.errorsrecords affected indexes.latestLedgeris the max across successful chunks.latestLedger = 0, every index isfailed.concurrencyis actually bounded (in-flight count never exceeds it).chunkSize/concurrencyvalidation rejects 0 and non-finite values.chunkSizeproduces the expected number of RPC calls.Full
npm testshows 18 passing tests (was 6 on main); the same 3 unrelated, pre-existing test-suite-load failures remain — none caused by this PR. Pre-existing TS error count is unchanged at 9.Notes / known limitations
main: three test suites fail to load onmain(tokenbound-sdk.test.ts,Hero.test.tsx,Footer.test.tsx) andnpx tsc --noEmitreports 9 pre-existing errors inapp/[locale]/create-event/page.tsxandlib/soroban.ts. None are caused or worsened by this PR.Closes #222