From 33c525ff75078a9048582225bcfcf8d8396c5e68 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 13:59:26 +0000 Subject: [PATCH] cosmwasm: Separate CosmWasmClient.searchTx and .getTx --- CHANGELOG.md | 2 + .../src/cosmwasmclient.searchtx.spec.ts | 52 ++++--------------- packages/cosmwasm/src/cosmwasmclient.ts | 19 +++---- packages/cosmwasm/src/index.ts | 1 - packages/cosmwasm/types/cosmwasmclient.d.ts | 7 +-- packages/cosmwasm/types/index.d.ts | 1 - 6 files changed, 22 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea39e3ff81..e58a19d262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ @cosmjs/launchpad instead. - @cosmjs/cosmwasm: Export `JsonObject`, `ChangeAdminResult` and `WasmData` types as well as `isValidBuilder` and `parseWasmData` functions. +- @cosmjs/cosmwasm: Add `CosmWasmClient.getTx` method for searching by ID and + remove such functionality from `CosmWasmClient.searchTx`. - @cosmjs/cosmwasm-stargate: Add new package for CosmWasm Stargate support. - @cosmjs/launchpad: Add `Secp256k1Wallet` to manage a single raw secp256k1 keypair. diff --git a/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts index b62441fa55..8884630fdc 100644 --- a/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts @@ -44,7 +44,7 @@ interface TestTxExecute { readonly tx: WrappedStdTx; } -describe("CosmWasmClient.searchTx", () => { +describe("CosmWasmClient.getTx and .searchTx", () => { let sendSuccessful: TestTxSend | undefined; let sendSelfSuccessful: TestTxSend | undefined; let sendUnsuccessful: TestTxSend | undefined; @@ -147,15 +147,14 @@ describe("CosmWasmClient.searchTx", () => { } }); - describe("with SearchByIdQuery", () => { - it("can search successful tx by ID", async () => { + describe("getTx", () => { + it("can get successful tx by ID", async () => { pendingWithoutLaunchpad(); pendingWithoutErc20(); assert(sendSuccessful, "value must be set in beforeAll()"); const client = new CosmWasmClient(launchpad.endpoint); - const result = await client.searchTx({ id: sendSuccessful.hash }); - expect(result.length).toEqual(1); - expect(result[0]).toEqual( + const result = await client.getTx(sendSuccessful.hash); + expect(result).toEqual( jasmine.objectContaining({ height: sendSuccessful.height, hash: sendSuccessful.hash, @@ -165,14 +164,13 @@ describe("CosmWasmClient.searchTx", () => { ); }); - it("can search unsuccessful tx by ID", async () => { + it("can get unsuccessful tx by ID", async () => { pendingWithoutLaunchpad(); pendingWithoutErc20(); assert(sendUnsuccessful, "value must be set in beforeAll()"); const client = new CosmWasmClient(launchpad.endpoint); - const result = await client.searchTx({ id: sendUnsuccessful.hash }); - expect(result.length).toEqual(1); - expect(result[0]).toEqual( + const result = await client.getTx(sendUnsuccessful.hash); + expect(result).toEqual( jasmine.objectContaining({ height: sendUnsuccessful.height, hash: sendUnsuccessful.hash, @@ -182,41 +180,13 @@ describe("CosmWasmClient.searchTx", () => { ); }); - it("can search by ID (non existent)", async () => { + it("can get by ID (non existent)", async () => { pendingWithoutLaunchpad(); pendingWithoutErc20(); const client = new CosmWasmClient(launchpad.endpoint); const nonExistentId = "0000000000000000000000000000000000000000000000000000000000000000"; - const result = await client.searchTx({ id: nonExistentId }); - expect(result.length).toEqual(0); - }); - - it("can search by ID and filter by minHeight", async () => { - pendingWithoutLaunchpad(); - pendingWithoutErc20(); - assert(sendSuccessful); - const client = new CosmWasmClient(launchpad.endpoint); - const query = { id: sendSuccessful.hash }; - - { - const result = await client.searchTx(query, { minHeight: 0 }); - expect(result.length).toEqual(1); - } - - { - const result = await client.searchTx(query, { minHeight: sendSuccessful.height - 1 }); - expect(result.length).toEqual(1); - } - - { - const result = await client.searchTx(query, { minHeight: sendSuccessful.height }); - expect(result.length).toEqual(1); - } - - { - const result = await client.searchTx(query, { minHeight: sendSuccessful.height + 1 }); - expect(result.length).toEqual(0); - } + const result = await client.getTx(nonExistentId); + expect(result).toBeNull(); }); }); diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index b87b29a545..28c9d43c1f 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -54,15 +54,7 @@ export interface SearchByTagsQuery { readonly tags: ReadonlyArray<{ readonly key: string; readonly value: string }>; } -export type SearchTxQuery = - | SearchByIdQuery - | SearchByHeightQuery - | SearchBySentFromOrToQuery - | SearchByTagsQuery; - -function isSearchByIdQuery(query: SearchTxQuery): query is SearchByIdQuery { - return (query as SearchByIdQuery).id !== undefined; -} +export type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery; function isSearchByHeightQuery(query: SearchTxQuery): query is SearchByHeightQuery { return (query as SearchByHeightQuery).height !== undefined; @@ -264,6 +256,11 @@ export class CosmWasmClient { }; } + public async getTx(id: string): Promise { + const results = await this.txsQuery(`tx.hash=${id}`); + return results[0] ?? null; + } + public async searchTx(query: SearchTxQuery, filter: SearchTxFilter = {}): Promise { const minHeight = filter.minHeight || 0; const maxHeight = filter.maxHeight || Number.MAX_SAFE_INTEGER; @@ -275,9 +272,7 @@ export class CosmWasmClient { } let txs: readonly IndexedTx[]; - if (isSearchByIdQuery(query)) { - txs = await this.txsQuery(`tx.hash=${query.id}`); - } else if (isSearchByHeightQuery(query)) { + if (isSearchByHeightQuery(query)) { // optional optimization to avoid network request if (query.height < minHeight || query.height > maxHeight) { txs = []; diff --git a/packages/cosmwasm/src/index.ts b/packages/cosmwasm/src/index.ts index 58de667f86..051d36bc9e 100644 --- a/packages/cosmwasm/src/index.ts +++ b/packages/cosmwasm/src/index.ts @@ -14,7 +14,6 @@ export { CosmWasmClient, GetSequenceResult, SearchByHeightQuery, - SearchByIdQuery, SearchBySentFromOrToQuery, SearchByTagsQuery, SearchTxQuery, diff --git a/packages/cosmwasm/types/cosmwasmclient.d.ts b/packages/cosmwasm/types/cosmwasmclient.d.ts index bd4f4cb591..66cd6ee159 100644 --- a/packages/cosmwasm/types/cosmwasmclient.d.ts +++ b/packages/cosmwasm/types/cosmwasmclient.d.ts @@ -42,11 +42,7 @@ export interface SearchByTagsQuery { readonly value: string; }>; } -export declare type SearchTxQuery = - | SearchByIdQuery - | SearchByHeightQuery - | SearchBySentFromOrToQuery - | SearchByTagsQuery; +export declare type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery; export interface SearchTxFilter { readonly minHeight?: number; readonly maxHeight?: number; @@ -148,6 +144,7 @@ export declare class CosmWasmClient { * @param height The height of the block. If undefined, the latest height is used. */ getBlock(height?: number): Promise; + getTx(id: string): Promise; searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise; broadcastTx(tx: StdTx): Promise; getCodes(): Promise; diff --git a/packages/cosmwasm/types/index.d.ts b/packages/cosmwasm/types/index.d.ts index f1e7399745..3b86b7c0de 100644 --- a/packages/cosmwasm/types/index.d.ts +++ b/packages/cosmwasm/types/index.d.ts @@ -13,7 +13,6 @@ export { CosmWasmClient, GetSequenceResult, SearchByHeightQuery, - SearchByIdQuery, SearchBySentFromOrToQuery, SearchByTagsQuery, SearchTxQuery,