From cb125800bd3c62ebafc3f7649c4bab45b8adac9a Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 13:53:51 +0000 Subject: [PATCH] launchpad: Separate CosmosClient.searchTx and .getTx --- CHANGELOG.md | 2 + .../src/cosmosclient.searchtx.spec.ts | 51 ++++--------------- packages/launchpad/src/cosmosclient.ts | 23 +++------ packages/launchpad/src/index.ts | 2 - packages/launchpad/types/cosmosclient.d.ts | 11 +--- packages/launchpad/types/index.d.ts | 2 - 6 files changed, 22 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a71f8e6..ea39e3ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ - @cosmjs/launchpad: `OfflineSigner` type’s `sign` method renamed `signAmino` and `SignResponse` type renamed `AminoSignResponse`. - @cosmjs/launchpad: `Secp256k1HdWallet.sign` method renamed `signAmino`. +- @cosmjs/launchpad: Add `CosmosClient.getTx` method for searching by ID and + remove such functionality from `CosmosClient.searchTx`. - @cosmjs/launchpad-ledger: `LedgerSigner.sign` method renamed `signAmino`. - @cosmjs/proto-signing: Add new package for handling transaction signing with protobuf encoding. diff --git a/packages/launchpad/src/cosmosclient.searchtx.spec.ts b/packages/launchpad/src/cosmosclient.searchtx.spec.ts index 36445f5c..c195e87b 100644 --- a/packages/launchpad/src/cosmosclient.searchtx.spec.ts +++ b/packages/launchpad/src/cosmosclient.searchtx.spec.ts @@ -26,7 +26,7 @@ interface TestTxSend { readonly tx: WrappedStdTx; } -describe("CosmosClient.searchTx", () => { +describe("CosmosClient.getTx and .searchTx", () => { let sendUnsuccessful: TestTxSend | undefined; let sendSuccessful: TestTxSend | undefined; @@ -91,14 +91,13 @@ describe("CosmosClient.searchTx", () => { } }); - describe("with SearchByIdQuery", () => { - it("can search successful tx by ID", async () => { + describe("getTx", () => { + it("can get successful tx by ID", async () => { pendingWithoutLaunchpad(); assert(sendSuccessful, "value must be set in beforeAll()"); const client = new CosmosClient(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, @@ -108,13 +107,12 @@ describe("CosmosClient.searchTx", () => { ); }); - it("can search unsuccessful tx by ID", async () => { + it("can get unsuccessful tx by ID", async () => { pendingWithoutLaunchpad(); assert(sendUnsuccessful, "value must be set in beforeAll()"); const client = new CosmosClient(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, @@ -124,39 +122,12 @@ describe("CosmosClient.searchTx", () => { ); }); - it("can search by ID (non existent)", async () => { + it("can get by ID (non existent)", async () => { pendingWithoutLaunchpad(); const client = new CosmosClient(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(); - assert(sendSuccessful); - const client = new CosmosClient(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/launchpad/src/cosmosclient.ts b/packages/launchpad/src/cosmosclient.ts index 8a087332..79194bc9 100644 --- a/packages/launchpad/src/cosmosclient.ts +++ b/packages/launchpad/src/cosmosclient.ts @@ -66,10 +66,6 @@ export function assertIsBroadcastTxSuccess(result: BroadcastTxResult): asserts r } } -export interface SearchByIdQuery { - readonly id: string; -} - export interface SearchByHeightQuery { readonly height: number; } @@ -86,15 +82,7 @@ export interface SearchByTagsQuery { readonly tags: ReadonlyArray<{ readonly key: string; readonly value: string }>; } -export type SearchTxQuery = - | SearchByIdQuery - | SearchByHeightQuery - | SearchBySentFromOrToQuery - | SearchByTagsQuery; - -export function isSearchByIdQuery(query: SearchTxQuery): query is SearchByIdQuery { - return (query as SearchByIdQuery).id !== undefined; -} +export type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery; export function isSearchByHeightQuery(query: SearchTxQuery): query is SearchByHeightQuery { return (query as SearchByHeightQuery).height !== undefined; @@ -269,6 +257,11 @@ export class CosmosClient { }; } + 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; @@ -280,9 +273,7 @@ export class CosmosClient { } 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/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 2b264517..644eb610 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -18,13 +18,11 @@ export { BroadcastTxResult, BroadcastTxSuccess, SearchByHeightQuery, - SearchByIdQuery, SearchBySentFromOrToQuery, SearchByTagsQuery, SearchTxQuery, SearchTxFilter, isSearchByHeightQuery, - isSearchByIdQuery, isSearchBySentFromOrToQuery, isSearchByTagsQuery, } from "./cosmosclient"; diff --git a/packages/launchpad/types/cosmosclient.d.ts b/packages/launchpad/types/cosmosclient.d.ts index 12bb6f2d..919502ce 100644 --- a/packages/launchpad/types/cosmosclient.d.ts +++ b/packages/launchpad/types/cosmosclient.d.ts @@ -38,9 +38,6 @@ export declare function isBroadcastTxSuccess(result: BroadcastTxResult): result export declare function assertIsBroadcastTxSuccess( result: BroadcastTxResult, ): asserts result is BroadcastTxSuccess; -export interface SearchByIdQuery { - readonly id: string; -} export interface SearchByHeightQuery { readonly height: number; } @@ -57,12 +54,7 @@ export interface SearchByTagsQuery { readonly value: string; }>; } -export declare type SearchTxQuery = - | SearchByIdQuery - | SearchByHeightQuery - | SearchBySentFromOrToQuery - | SearchByTagsQuery; -export declare function isSearchByIdQuery(query: SearchTxQuery): query is SearchByIdQuery; +export declare type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery; export declare function isSearchByHeightQuery(query: SearchTxQuery): query is SearchByHeightQuery; export declare function isSearchBySentFromOrToQuery(query: SearchTxQuery): query is SearchBySentFromOrToQuery; export declare function isSearchByTagsQuery(query: SearchTxQuery): query is SearchByTagsQuery; @@ -144,6 +136,7 @@ export declare class CosmosClient { * @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; private txsQuery; diff --git a/packages/launchpad/types/index.d.ts b/packages/launchpad/types/index.d.ts index 8b312aae..1c8bc2ea 100644 --- a/packages/launchpad/types/index.d.ts +++ b/packages/launchpad/types/index.d.ts @@ -16,13 +16,11 @@ export { BroadcastTxResult, BroadcastTxSuccess, SearchByHeightQuery, - SearchByIdQuery, SearchBySentFromOrToQuery, SearchByTagsQuery, SearchTxQuery, SearchTxFilter, isSearchByHeightQuery, - isSearchByIdQuery, isSearchBySentFromOrToQuery, isSearchByTagsQuery, } from "./cosmosclient";