mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
getCodes(): Support more than 100 code IDs
This commit is contained in:
parent
0fe5c0c6f6
commit
52837a2a75
@ -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
|
||||
|
@ -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<readonly Code[]> {
|
||||
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(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user