From 3c55cb1fdd10eac2d86fbc28a41b1b71c2318476 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 25 May 2023 16:49:27 +0200 Subject: [PATCH 1/2] Remove unnecessary falsy handling --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 9864ce66..056e0a6a 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -375,8 +375,7 @@ export class CosmWasmClient { do { const { contracts, pagination }: QueryContractsByCodeResponse = await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); - const loadedContracts = contracts || []; - allContracts.push(...loadedContracts); + allContracts.push(...contracts); startAtKey = pagination?.nextKey; } while (startAtKey?.length !== 0 && startAtKey !== undefined); From a284d9474086e0b40bb08f7058f0ac743ff465a9 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 25 May 2023 16:53:00 +0200 Subject: [PATCH 2/2] Add CosmWasmClient.getContractsByCreator --- CHANGELOG.md | 3 +++ .../src/cosmwasmclient.spec.ts | 15 +++++++++++++++ .../cosmwasm-stargate/src/cosmwasmclient.ts | 18 ++++++++++++++++++ .../src/modules/wasm/queries.ts | 15 +++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e60e2b11..319c75d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,9 +16,12 @@ and this project adheres to ### Added - @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.instantiate2` ([#1407]). +- @cosmjs/cosmwasm-stargate: Add `CosmWasmClient.getContractsByCreator` + ([#1266]). - @cosmjs/stargate: `IndexedTx` and `DeliverTxResponse` now have a `msgResponses` field ([#1305]). +[#1266]: https://github.com/cosmos/cosmjs/issues/1266 [#1305]: https://github.com/cosmos/cosmjs/issues/1305 [#1407]: https://github.com/cosmos/cosmjs/pull/1407 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index 6f75b3de..cdb6f81d 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -289,6 +289,21 @@ describe("CosmWasmClient", () => { }); }); + describe("getContractsByCreator", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = await CosmWasmClient.connect(wasmd.endpoint); + const result = await client.getContractsByCreator(alice.address0); + const expectedAddresses = deployedHackatom.instances.map((info) => info.address); + + // Test first 3 instances we get from scripts/wasmd/init.sh. There may me more than that in the result. + expect(result.length).toBeGreaterThanOrEqual(3); + expect(result[0]).toEqual(expectedAddresses[0]); + expect(result[1]).toEqual(expectedAddresses[1]); + expect(result[2]).toEqual(expectedAddresses[2]); + }); + }); + describe("getContract", () => { it("works for instance without admin", async () => { pendingWithoutWasmd(); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 056e0a6a..fa3af3f9 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -34,6 +34,7 @@ import { CodeInfoResponse, QueryCodesResponse, QueryContractsByCodeResponse, + QueryContractsByCreatorResponse, } from "cosmjs-types/cosmwasm/wasm/v1/query"; import { ContractCodeHistoryOperationType } from "cosmjs-types/cosmwasm/wasm/v1/types"; @@ -382,6 +383,23 @@ export class CosmWasmClient { return allContracts; } + /** + * Returns a list of contract addresses created by the given creator. + * This just loops through all pagination pages. + */ + public async getContractsByCreator(creator: string): Promise { + const allContracts = []; + let startAtKey: Uint8Array | undefined = undefined; + do { + const { contractAddresses, pagination }: QueryContractsByCreatorResponse = + await this.forceGetQueryClient().wasm.listContractsByCreator(creator, startAtKey); + allContracts.push(...contractAddresses); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0 && startAtKey !== undefined); + + return allContracts; + } + /** * Throws an error if no contract was found at the address */ diff --git a/packages/cosmwasm-stargate/src/modules/wasm/queries.ts b/packages/cosmwasm-stargate/src/modules/wasm/queries.ts index aef8a493..35bca8af 100644 --- a/packages/cosmwasm-stargate/src/modules/wasm/queries.ts +++ b/packages/cosmwasm-stargate/src/modules/wasm/queries.ts @@ -8,6 +8,7 @@ import { QueryContractHistoryResponse, QueryContractInfoResponse, QueryContractsByCodeResponse, + QueryContractsByCreatorResponse, QueryRawContractStateResponse, } from "cosmjs-types/cosmwasm/wasm/v1/query"; import Long from "long"; @@ -33,6 +34,13 @@ export interface WasmExtension { id: number, paginationKey?: Uint8Array, ) => Promise; + /** + * Returns a list of contract addresses created by the given creator. + */ + readonly listContractsByCreator: ( + creator: string, + paginationKey?: Uint8Array, + ) => Promise; /** * Returns null when contract was not found at this address. */ @@ -90,6 +98,13 @@ export function setupWasmExtension(base: QueryClient): WasmExtension { }; return queryService.ContractsByCode(request); }, + listContractsByCreator: async (creator: string, paginationKey?: Uint8Array) => { + const request = { + creatorAddress: creator, + pagination: createPagination(paginationKey), + }; + return queryService.ContractsByCreator(request); + }, getContractInfo: async (address: string) => { const request = { address: address }; return queryService.ContractInfo(request);