From aff82e1f40fc26d6a6602804dd37b5ae58df54ce Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 10 Jun 2020 12:45:34 +0100 Subject: [PATCH] Improve cosmwasm searchTx by sentFromOrTo --- packages/cosmwasm/src/cosmwasmclient.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index 45f88de7..e6cac469 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -286,11 +286,26 @@ export class CosmWasmClient { // We cannot get both in one request (see https://github.com/cosmos/gaia/issues/75) const sentQuery = withFilters(`message.module=bank&message.sender=${query.sentFromOrTo}`); const receivedQuery = withFilters(`message.module=bank&transfer.recipient=${query.sentFromOrTo}`); - const sent = await this.txsQuery(sentQuery); - const received = await this.txsQuery(receivedQuery); + const [sent, received] = (await Promise.all([ + this.txsQuery(sentQuery), + this.txsQuery(receivedQuery), + ])) as [IndexedTx[], IndexedTx[]]; - const sentHashes = sent.map((t) => t.hash); - txs = [...sent, ...received.filter((t) => !sentHashes.includes(t.hash))]; + txs = []; + /* eslint-disable @typescript-eslint/no-non-null-assertion */ + // sent/received are presorted + while (sent.length && received.length) { + const next = + sent[0].hash === received[0].hash + ? sent.shift()! && received.shift()! + : sent[0].height <= received[0].height + ? sent.shift()! + : received.shift()!; + txs = [...txs, next]; + } + /* eslint-enable @typescript-eslint/no-non-null-assertion */ + // At least one of sent/received is empty by now + txs = [...txs, ...sent, ...received]; } else if (isSearchByTagsQuery(query)) { const rawQuery = withFilters(query.tags.map((t) => `${t.key}=${t.value}`).join("&")); txs = await this.txsQuery(rawQuery);