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 9864ce66..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"; @@ -375,8 +376,24 @@ 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); + + 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); 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);