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;