From 8fa74e616e66c7dc4fca0a8bc0e6627217687010 Mon Sep 17 00:00:00 2001 From: phips28 Date: Sat, 6 Jan 2024 21:36:07 +0100 Subject: [PATCH] fix(stargate/allBalances): use pagination if there are more than 100 balances --- packages/stargate/src/modules/bank/queries.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/stargate/src/modules/bank/queries.ts b/packages/stargate/src/modules/bank/queries.ts index 4ac41c010c..76256c2407 100644 --- a/packages/stargate/src/modules/bank/queries.ts +++ b/packages/stargate/src/modules/bank/queries.ts @@ -3,6 +3,7 @@ import { assert } from "@cosmjs/utils"; import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank"; import { QueryAllBalancesRequest, + QueryAllBalancesResponse, QueryClientImpl, QueryDenomsMetadataRequest, QueryTotalSupplyResponse, @@ -36,10 +37,22 @@ export function setupBankExtension(base: QueryClient): BankExtension { return balance; }, allBalances: async (address: string) => { - const { balances } = await queryService.AllBalances( - QueryAllBalancesRequest.fromPartial({ address: address }), - ); - return balances; + const allBalances = []; + let paginationKey: Uint8Array | undefined = undefined; + do { + const { balances, pagination }: QueryAllBalancesResponse = await queryService.AllBalances( + QueryAllBalancesRequest.fromPartial({ + address: address, + pagination: createPagination(paginationKey), + }), + ); + + const loadedBalances = balances || []; + allBalances.push(...loadedBalances); + paginationKey = pagination?.nextKey; + } while (paginationKey !== undefined && paginationKey.length !== 0); + + return allBalances; }, totalSupply: async (paginationKey?: Uint8Array) => { const response = await queryService.TotalSupply({