Add CosmWasmClient.getContractsByCreator

This commit is contained in:
Simon Warta
2023-05-25 16:53:00 +02:00
parent 3c55cb1fdd
commit a284d94740
4 changed files with 51 additions and 0 deletions
+3
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
@@ -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();
@@ -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<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);
return allContracts;
}
/**
* Throws an error if no contract was found at the address
*/
@@ -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);