Merge pull request #216 from CosmWasm/132-searchtx-order
Improve cosmwasm searchTx by sentFromOrTo
This commit is contained in:
commit
fbc991e79a
@ -16,25 +16,20 @@ import {
|
||||
wasmdEnabled,
|
||||
} from "./testutils.spec";
|
||||
|
||||
type TestSendTx =
|
||||
| {
|
||||
readonly sender: string;
|
||||
readonly recipient: string;
|
||||
readonly hash: string;
|
||||
readonly height: number;
|
||||
readonly tx: CosmosSdkTx;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
describe("CosmWasmClient.searchTx", () => {
|
||||
let sendSuccessful:
|
||||
| {
|
||||
readonly sender: string;
|
||||
readonly recipient: string;
|
||||
readonly hash: string;
|
||||
readonly height: number;
|
||||
readonly tx: CosmosSdkTx;
|
||||
}
|
||||
| undefined;
|
||||
let sendUnsuccessful:
|
||||
| {
|
||||
readonly sender: string;
|
||||
readonly recipient: string;
|
||||
readonly hash: string;
|
||||
readonly height: number;
|
||||
readonly tx: CosmosSdkTx;
|
||||
}
|
||||
| undefined;
|
||||
let sendSuccessful: TestSendTx;
|
||||
let sendSelfSuccessful: TestSendTx;
|
||||
let sendUnsuccessful: TestSendTx;
|
||||
let postedExecute:
|
||||
| {
|
||||
readonly sender: string;
|
||||
@ -70,6 +65,24 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
const recipient = alice.address0;
|
||||
const transferAmount: Coin = {
|
||||
denom: "ucosm",
|
||||
amount: "2345678",
|
||||
};
|
||||
const result = await client.sendTokens(recipient, [transferAmount]);
|
||||
await sleep(75); // wait until tx is indexed
|
||||
const txDetails = await new RestClient(wasmd.endpoint).txById(result.transactionHash);
|
||||
sendSelfSuccessful = {
|
||||
sender: alice.address0,
|
||||
recipient: recipient,
|
||||
hash: result.transactionHash,
|
||||
height: Number.parseInt(txDetails.height, 10),
|
||||
tx: txDetails.tx,
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
const memo = "Sending more than I can afford";
|
||||
const recipient = makeRandomAddress();
|
||||
@ -268,8 +281,8 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
expect(containsMsgWithSender || containsMsgWithRecipient).toEqual(true);
|
||||
}
|
||||
|
||||
// Check details of most recent result
|
||||
expect(results[results.length - 1]).toEqual(
|
||||
// Check details of most recent result (not sent to self)
|
||||
expect(results[results.length - 2]).toEqual(
|
||||
jasmine.objectContaining({
|
||||
height: sendSuccessful.height,
|
||||
hash: sendSuccessful.hash,
|
||||
@ -305,6 +318,17 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("can search by sender or recipient (sorted and deduplicated)", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(sendSelfSuccessful, "value must be set in beforeAll()");
|
||||
const txhash = sendSelfSuccessful.hash;
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({ sentFromOrTo: sendSelfSuccessful.recipient });
|
||||
|
||||
expect(Array.from(results).sort((tx1, tx2) => tx1.height - tx2.height)).toEqual(results);
|
||||
expect(results.filter((result) => result.hash === txhash).length).toEqual(1);
|
||||
});
|
||||
|
||||
it("can search by recipient and filter by minHeight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(sendSuccessful);
|
||||
|
||||
@ -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))];
|
||||
let mergedTxs: readonly IndexedTx[] = [];
|
||||
/* 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()!;
|
||||
mergedTxs = [...mergedTxs, next];
|
||||
}
|
||||
/* eslint-enable @typescript-eslint/no-non-null-assertion */
|
||||
// At least one of sent/received is empty by now
|
||||
txs = [...mergedTxs, ...sent, ...received];
|
||||
} else if (isSearchByTagsQuery(query)) {
|
||||
const rawQuery = withFilters(query.tags.map((t) => `${t.key}=${t.value}`).join("&"));
|
||||
txs = await this.txsQuery(rawQuery);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user