From bf25bc6409b840acbebaaf7a39129f07c9ecc845 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:06:47 +0100 Subject: [PATCH] getContracts(): Support more than 100 per code ID --- CHANGELOG.md | 4 ++- .../cosmwasm-stargate/src/cosmwasmclient.ts | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7e910b..9c2dd21a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ and this project adheres to dependencies. This should also reduce the bundle size as only the English wordlist is shipped. ([#966]) - @cosmjs/cli: Rename binary `cosmwasm-cli` to `cosmjs-cli` ([#1033]). -- @cosmjs/stargate & @cosmjs/cosmwasm-stargate: Removed default types from AminoTypes. ([1079]) +- @cosmjs/stargate & @cosmjs/cosmwasm-stargate: Removed default types from AminoTypes. ([#1079]) +- @cosmjs/cosmwasm-stargate: getCodes() automatically loops through all pagination pages now. ([#1077]) [#927]: https://github.com/cosmos/cosmjs/issues/927 [#955]: https://github.com/cosmos/cosmjs/issues/955 @@ -56,6 +57,7 @@ and this project adheres to [#1026]: https://github.com/cosmos/cosmjs/issues/1026 [#1033]: https://github.com/cosmos/cosmjs/issues/1033 [#1053]: https://github.com/cosmos/cosmjs/issues/1053 +[#1077]: https://github.com/cosmos/cosmjs/issues/1077 [#1079]: https://github.com/cosmos/cosmjs/issues/1079 ### Removed diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index c1a39ed2..b1da085d 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -25,7 +25,7 @@ import { } from "@cosmjs/stargate"; import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc"; import { assert, sleep } from "@cosmjs/utils"; -import { CodeInfoResponse } from "cosmjs-types/cosmwasm/wasm/v1/query"; +import { CodeInfoResponse, QueryContractsByCodeResponse } from "cosmjs-types/cosmwasm/wasm/v1/query"; import { ContractCodeHistoryOperationType } from "cosmjs-types/cosmwasm/wasm/v1/types"; import { JsonObject, setupWasmExtension, WasmExtension } from "./modules"; @@ -334,10 +334,30 @@ export class CosmWasmClient { return codeDetails; } + /** + * getContracts() returns all contracts and is just looping through all pagination pages. + * + * This is potentially inefficient and advanced apps should consider creating + * their own query client to handle pagination together with the app's screens. + */ public async getContracts(codeId: number): Promise { - // TODO: handle pagination - accept as arg or auto-loop - const { contracts } = await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId); - return contracts; + const allContracts = []; + + try { + let startAtKey: Uint8Array | undefined = undefined; + do { + const { contracts, pagination }: QueryContractsByCodeResponse = + await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); + const loadedContracts = contracts || []; + loadedContracts.reverse(); + allContracts.unshift(...loadedContracts); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0); + } catch (_e: any) { + throw new Error(_e); + } + + return allContracts; } /**