From cb125800bd3c62ebafc3f7649c4bab45b8adac9a Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 13:53:51 +0000 Subject: [PATCH 1/5] 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"; From 33c525ff75078a9048582225bcfcf8d8396c5e68 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 13:59:26 +0000 Subject: [PATCH 2/5] 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 ea39e3ff..e58a19d2 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 b62441fa..8884630f 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 b87b29a5..28c9d43c 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 58de667f..051d36bc 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 bd4f4cb5..66cd6ee1 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 f1e73997..3b86b7c0 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, From 90f2e765f7708e8f9e2b4731abfc3732baa2e692 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 14:07:08 +0000 Subject: [PATCH 3/5] stargate: Separate StargateClient.searchTx and .getTx --- .../src/signingstargateclient.spec.ts | 10 ++-- .../src/stargateclient.searchtx.spec.ts | 51 ++++--------------- packages/stargate/src/stargateclient.ts | 10 ++-- packages/stargate/types/stargateclient.d.ts | 1 + 4 files changed, 24 insertions(+), 48 deletions(-) diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index a15aca9e..482f5e54 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -201,8 +201,9 @@ describe("SigningStargateClient", () => { await sleep(1000); - const searchResult = await client.searchTx({ id: result.transactionHash }); - const tx = Tx.decode(searchResult[0].tx); + const searchResult = await client.getTx(result.transactionHash); + assert(searchResult, "Must find transaction"); + const tx = Tx.decode(searchResult.tx); // From ModifyingDirectSecp256k1HdWallet expect(tx.body!.memo).toEqual("This was modified"); expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm")); @@ -277,8 +278,9 @@ describe("SigningStargateClient", () => { await sleep(1000); - const searchResult = await client.searchTx({ id: result.transactionHash }); - const tx = Tx.decode(searchResult[0].tx); + const searchResult = await client.getTx(result.transactionHash); + assert(searchResult, "Must find transaction"); + const tx = Tx.decode(searchResult.tx); // From ModifyingSecp256k1HdWallet expect(tx.body!.memo).toEqual("This was modified"); expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm")); diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index e22a1434..5b85a577 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -95,7 +95,7 @@ async function sendTokens( }; } -describe("StargateClient.searchTx", () => { +describe("StargateClient.getTx and .searchTx", () => { const registry = new Registry(); let sendUnsuccessful: TestTxSend | undefined; @@ -147,14 +147,13 @@ describe("StargateClient.searchTx", () => { } }); - describe("with SearchByIdQuery", () => { - it("can search successful tx by ID", async () => { + describe("getTx", () => { + it("can get successful tx by ID", async () => { pendingWithoutSimapp(); assert(sendSuccessful, "value must be set in beforeAll()"); const client = await StargateClient.connect(simapp.tendermintUrl); - 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, @@ -164,13 +163,12 @@ describe("StargateClient.searchTx", () => { ); }); - it("can search unsuccessful tx by ID", async () => { + it("can get unsuccessful tx by ID", async () => { pendingWithoutSimapp(); assert(sendUnsuccessful, "value must be set in beforeAll()"); const client = await StargateClient.connect(simapp.tendermintUrl); - 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, @@ -180,39 +178,12 @@ describe("StargateClient.searchTx", () => { ); }); - it("can search by ID (non existent)", async () => { + it("can get by ID (non existent)", async () => { pendingWithoutSimapp(); const client = await StargateClient.connect(simapp.tendermintUrl); 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 () => { - pendingWithoutSimapp(); - assert(sendSuccessful, "value must be set in beforeAll()"); - const client = await StargateClient.connect(simapp.tendermintUrl); - 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/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 073879c6..ca2b1fe0 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -4,7 +4,6 @@ import { Block, Coin, isSearchByHeightQuery, - isSearchByIdQuery, isSearchBySentFromOrToQuery, isSearchByTagsQuery, PubKey, @@ -207,6 +206,11 @@ export class StargateClient { return balances.map(coinFromProto); } + 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; @@ -219,9 +223,7 @@ export class StargateClient { let txs: readonly IndexedTx[]; - if (isSearchByIdQuery(query)) { - txs = await this.txsQuery(`tx.hash='${query.id}'`); - } else if (isSearchByHeightQuery(query)) { + if (isSearchByHeightQuery(query)) { txs = query.height >= minHeight && query.height <= maxHeight ? await this.txsQuery(`tx.height=${query.height}`) diff --git a/packages/stargate/types/stargateclient.d.ts b/packages/stargate/types/stargateclient.d.ts index 3fc30e70..44af081b 100644 --- a/packages/stargate/types/stargateclient.d.ts +++ b/packages/stargate/types/stargateclient.d.ts @@ -72,6 +72,7 @@ export declare class StargateClient { * proofs from such a method. */ getAllBalancesUnverified(address: string): Promise; + getTx(id: string): Promise; searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise; disconnect(): void; broadcastTx(tx: Uint8Array): Promise; From 793bd1ba4c054ccb0dccf36ca66aa62894d86753 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 14:12:14 +0000 Subject: [PATCH 4/5] cosmwasm-stargate: Separate CosmWasmClient.searchTx and .getTx --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 10 ++++++---- .../src/signingcosmwasmclient.spec.ts | 10 ++++++---- packages/cosmwasm-stargate/types/cosmwasmclient.d.ts | 1 + 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 902f40d4..0d36eff7 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -5,7 +5,6 @@ import { Block, Coin, isSearchByHeightQuery, - isSearchByIdQuery, isSearchBySentFromOrToQuery, isSearchByTagsQuery, SearchTxFilter, @@ -126,6 +125,11 @@ export class CosmWasmClient { return balance ? coinFromProto(balance) : null; } + 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; @@ -134,9 +138,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)) { txs = query.height >= minHeight && query.height <= maxHeight ? await this.txsQuery(`tx.height=${query.height}`) diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts index 7f21df86..2bc3b8ce 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -495,8 +495,9 @@ describe("SigningCosmWasmClient", () => { await sleep(1000); - const searchResult = await client.searchTx({ id: result.transactionHash }); - const tx = Tx.decode(searchResult[0].tx); + const searchResult = await client.getTx(result.transactionHash); + assert(searchResult, "Must find transaction"); + const tx = Tx.decode(searchResult.tx); // From ModifyingDirectSecp256k1HdWallet expect(tx.body!.memo).toEqual("This was modified"); expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm")); @@ -571,8 +572,9 @@ describe("SigningCosmWasmClient", () => { await sleep(1000); - const searchResult = await client.searchTx({ id: result.transactionHash }); - const tx = Tx.decode(searchResult[0].tx); + const searchResult = await client.getTx(result.transactionHash); + assert(searchResult, "Must find transaction"); + const tx = Tx.decode(searchResult.tx); // From ModifyingSecp256k1HdWallet expect(tx.body!.memo).toEqual("This was modified"); expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm")); diff --git a/packages/cosmwasm-stargate/types/cosmwasmclient.d.ts b/packages/cosmwasm-stargate/types/cosmwasmclient.d.ts index 9630ed1c..60f63c08 100644 --- a/packages/cosmwasm-stargate/types/cosmwasmclient.d.ts +++ b/packages/cosmwasm-stargate/types/cosmwasmclient.d.ts @@ -29,6 +29,7 @@ export declare class CosmWasmClient { getSequence(address: string): Promise; getBlock(height?: number): Promise; getBalance(address: string, searchDenom: string): Promise; + getTx(id: string): Promise; searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise; disconnect(): void; broadcastTx(tx: Uint8Array): Promise; From 508c13712bdf2faa7f889c7ae33309518af71732 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 15 Dec 2020 14:55:11 +0000 Subject: [PATCH 5/5] cli: Remove SearchByIdQuery import --- packages/cli/src/cli.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 3d9adebb..c0605d88 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -50,7 +50,6 @@ export async function main(originalArgs: readonly string[]): Promise { "CosmWasmClient", "GetSequenceResult", "SearchByHeightQuery", - "SearchByIdQuery", "SearchBySentFromOrToQuery", "SearchByTagsQuery", "SearchTxQuery",