Merge pull request #1432 from cosmos/add-getContractsByCreator

Add CosmWasmClient.getContractsByCreator
This commit is contained in:
Simon Warta 2023-05-25 17:31:06 +02:00 committed by GitHub
commit b351961414
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 2 deletions

View File

@ -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

View File

@ -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();

View File

@ -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<string[]> {
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);

View File

@ -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<QueryContractsByCodeResponse>;
/**
* Returns a list of contract addresses created by the given creator.
*/
readonly listContractsByCreator: (
creator: string,
paginationKey?: Uint8Array,
) => Promise<QueryContractsByCreatorResponse>;
/**
* 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);