Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stargate/allBalances): use pagination if there are more than 100 balances #1538

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/stargate/src/modules/bank/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { assert } from "@cosmjs/utils";
import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank";
import {
QueryAllBalancesRequest,
QueryAllBalancesResponse,
QueryClientImpl,
QueryDenomsMetadataRequest,
QueryTotalSupplyResponse,
Expand Down Expand Up @@ -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({
Expand Down