Merge pull request #1085 from cosmos/more-then-100-getCodes

getCodes(): Support more than 100 code IDs
This commit is contained in:
Simon Warta 2022-03-15 20:16:40 +01:00 committed by GitHub
commit 2539a2da62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -58,6 +58,7 @@ and this project adheres to
[#1033]: https://github.com/cosmos/cosmjs/issues/1033
[#1053]: https://github.com/cosmos/cosmjs/issues/1053
[#1077]: https://github.com/cosmos/cosmjs/issues/1077
[#1078]: https://github.com/cosmos/cosmjs/issues/1078
[#1079]: https://github.com/cosmos/cosmjs/issues/1079
### Removed

View File

@ -25,7 +25,11 @@ import {
} from "@cosmjs/stargate";
import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import { assert, sleep } from "@cosmjs/utils";
import { CodeInfoResponse, QueryContractsByCodeResponse } from "cosmjs-types/cosmwasm/wasm/v1/query";
import {
CodeInfoResponse,
QueryCodesResponse,
QueryContractsByCodeResponse,
} from "cosmjs-types/cosmwasm/wasm/v1/query";
import { ContractCodeHistoryOperationType } from "cosmjs-types/cosmwasm/wasm/v1/types";
import { JsonObject, setupWasmExtension, WasmExtension } from "./modules";
@ -303,9 +307,25 @@ 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<readonly Code[]> {
const { codeInfos } = await this.forceGetQueryClient().wasm.listCodeInfo();
return (codeInfos || []).map((entry: CodeInfoResponse): Code => {
const allCodes = [];
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");
return {
id: entry.codeId.toNumber(),