Merge branch 'main' into more-then-100-getCodes

This commit is contained in:
Milan Steiner
2022-03-15 19:27:46 +01:00
committed by GitHub
3 changed files with 89 additions and 6 deletions
@@ -25,7 +25,11 @@ import {
} from "@cosmjs/stargate";
import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import { assert, sleep } from "@cosmjs/utils";
import { CodeInfoResponse, QueryCodesResponse } 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";
@@ -350,10 +354,24 @@ export class CosmWasmClient {
return codeDetails;
}
/**
* getContracts() returns all contract instances for one code 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<readonly string[]> {
// TODO: handle pagination - accept as arg or auto-loop
const { contracts } = await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId);
return contracts;
const allContracts = [];
let startAtKey: Uint8Array | undefined = undefined;
do {
const { contracts, pagination }: QueryContractsByCodeResponse =
await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey);
const loadedContracts = contracts || [];
allContracts.push(...loadedContracts);
startAtKey = pagination?.nextKey;
} while (startAtKey?.length !== 0 && startAtKey !== undefined);
return allContracts;
}
/**