From 52837a2a7513c9f94bc8b929e7dc5825013e7f12 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 09:59:30 +0100 Subject: [PATCH 1/5] getCodes(): Support more than 100 code IDs --- CHANGELOG.md | 1 + .../cosmwasm-stargate/src/cosmwasmclient.ts | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7e910bec..a1f55c820a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to 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/cosmwasm-stargate: getCodes() automatically loops through all pagination pages now. [#927]: https://github.com/cosmos/cosmjs/issues/927 [#955]: https://github.com/cosmos/cosmjs/issues/955 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index c1a39ed253..15cec983d5 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, QueryCodesResponse } from "cosmjs-types/cosmwasm/wasm/v1/query"; import { ContractCodeHistoryOperationType } from "cosmjs-types/cosmwasm/wasm/v1/types"; import { JsonObject, setupWasmExtension, WasmExtension } from "./modules"; @@ -303,9 +303,30 @@ export class CosmWasmClient { ); } + /** + * getCodes() returns all codes 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 getCodes(): Promise { - const { codeInfos } = await this.forceGetQueryClient().wasm.listCodeInfo(); - return (codeInfos || []).map((entry: CodeInfoResponse): Code => { + const allCodes = []; + + try { + let startAtKey: Uint8Array | undefined = undefined; + do { + const { codeInfos, pagination }: QueryCodesResponse = + await this.forceGetQueryClient().wasm.listCodeInfo(startAtKey); + const loadedCodes = codeInfos || []; + loadedCodes.reverse(); + allCodes.unshift(...loadedCodes); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0); + } catch (_e: any) { + throw new Error(_e); + } + + return (allCodes || []).map((entry: CodeInfoResponse): Code => { assert(entry.creator && entry.codeId && entry.dataHash, "entry incomplete"); return { id: entry.codeId.toNumber(), From a89ca5fa0ebfb63282ecadb170616b88f955d2e6 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:08:11 +0100 Subject: [PATCH 2/5] Link issue to changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1f55c820a..1191f03faf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,7 +45,7 @@ and this project adheres to 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/cosmwasm-stargate: getCodes() automatically loops through all pagination pages now. +- @cosmjs/cosmwasm-stargate: getCodes() automatically loops through all pagination pages now. ([#1078]) [#927]: https://github.com/cosmos/cosmjs/issues/927 [#955]: https://github.com/cosmos/cosmjs/issues/955 @@ -57,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 +[#1078]: https://github.com/cosmos/cosmjs/issues/1078 [#1079]: https://github.com/cosmos/cosmjs/issues/1079 ### Removed From 612efece8f31e064b670991f382f1d3fbc1792cc Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:34:29 +0100 Subject: [PATCH 3/5] Fix tests --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 15cec983d5..25117fc8bd 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -318,7 +318,6 @@ export class CosmWasmClient { const { codeInfos, pagination }: QueryCodesResponse = await this.forceGetQueryClient().wasm.listCodeInfo(startAtKey); const loadedCodes = codeInfos || []; - loadedCodes.reverse(); allCodes.unshift(...loadedCodes); startAtKey = pagination?.nextKey; } while (startAtKey?.length !== 0); From fab3fcc31fcfe83042763627be77899a2d70f41c Mon Sep 17 00:00:00 2001 From: Milan Steiner <69144826+msteiner96@users.noreply.github.com> Date: Mon, 14 Mar 2022 15:21:59 +0100 Subject: [PATCH 4/5] Update packages/cosmwasm-stargate/src/cosmwasmclient.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 25117fc8bd..e9edec3bb1 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -325,7 +325,7 @@ export class CosmWasmClient { throw new Error(_e); } - return (allCodes || []).map((entry: CodeInfoResponse): Code => { + return allCodes.map((entry: CodeInfoResponse): Code => { assert(entry.creator && entry.codeId && entry.dataHash, "entry incomplete"); return { id: entry.codeId.toNumber(), From 1239f31161d8769f361828ce4f499e5636a59e70 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Tue, 15 Mar 2022 16:30:18 +0100 Subject: [PATCH 5/5] getCodes() doesn't need a try/catch --- .../cosmwasm-stargate/src/cosmwasmclient.ts | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index e9edec3bb1..4e32d8de80 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -312,18 +312,14 @@ export class CosmWasmClient { public async getCodes(): Promise { const allCodes = []; - try { - let startAtKey: Uint8Array | undefined = undefined; - do { - const { codeInfos, pagination }: QueryCodesResponse = - await this.forceGetQueryClient().wasm.listCodeInfo(startAtKey); - const loadedCodes = codeInfos || []; - allCodes.unshift(...loadedCodes); - startAtKey = pagination?.nextKey; - } while (startAtKey?.length !== 0); - } catch (_e: any) { - throw new Error(_e); - } + let startAtKey: Uint8Array | undefined = undefined; + do { + const { codeInfos, pagination }: QueryCodesResponse = + await this.forceGetQueryClient().wasm.listCodeInfo(startAtKey); + const loadedCodes = codeInfos || []; + allCodes.push(...loadedCodes); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0); return allCodes.map((entry: CodeInfoResponse): Code => { assert(entry.creator && entry.codeId && entry.dataHash, "entry incomplete");