Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle key value pairs in tx search correctly for numbers #1503

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ and this project adheres to

[#1489]: https://github.com/cosmos/cosmjs/pull/1489

### Fixed

- @cosmjs/stargate: Handle key value pairs in tx search correctly if the value
is a numeric value. ([#1462])

[#1462] : https://github.com/cosmos/cosmjs/issues/1462

### Changed

- all: Upgrade cosmjs-types to 0.9.0. This makes a few fields non-optional. It
Expand Down
11 changes: 9 additions & 2 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DeliverTxResponse,
fromTendermintEvent,
IndexedTx,
isSearchTxQueryArray,
QueryClient,
SearchTxQuery,
SequenceResponse,
Expand Down Expand Up @@ -216,8 +217,14 @@ export class CosmWasmClient {
let rawQuery: string;
if (typeof query === "string") {
rawQuery = query;
} else if (Array.isArray(query)) {
rawQuery = query.map((t) => `${t.key}='${t.value}'`).join(" AND ");
} else if (isSearchTxQueryArray(query)) {
rawQuery = query
.map((t) => {
// numeric values must not have quotes https://github.com/cosmos/cosmjs/issues/1462
if (typeof t.value === "string") return `${t.key}='${t.value}'`;
else return `${t.key}=${t.value}`;
})
.join(" AND ");
} else {
throw new Error("Got unsupported query type. See CosmJS 0.31 CHANGELOG for API breaking changes here.");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/stargate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export {
QueryClient,
QueryStoreResponse,
} from "./queryclient";
export { SearchTxQuery } from "./search";
export { isSearchTxQueryArray, SearchPair, SearchTxQuery } from "./search";
export {
createDefaultAminoConverters,
defaultRegistryTypes,
Expand Down
17 changes: 11 additions & 6 deletions packages/stargate/src/search.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/** A key value pair for searching transactions */
export interface SearchPair {
readonly key: string;
readonly value: string | number | bigint;
}

/**
* This query type allows you to pass arbitrary key/value pairs to the backend.
*/
export type SearchTxQuery =
| string
| ReadonlyArray<{
readonly key: string;
readonly value: string;
}>;
export type SearchTxQuery = string | readonly SearchPair[];

export function isSearchTxQueryArray(query: SearchTxQuery): query is readonly SearchPair[] {
return Array.isArray(query);
}
12 changes: 9 additions & 3 deletions packages/stargate/src/stargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TxExtension,
} from "./modules";
import { QueryClient } from "./queryclient";
import { SearchTxQuery } from "./search";
import { isSearchTxQueryArray, SearchTxQuery } from "./search";

export class TimeoutError extends Error {
public readonly txId: string;
Expand Down Expand Up @@ -386,8 +386,14 @@ export class StargateClient {
let rawQuery: string;
if (typeof query === "string") {
rawQuery = query;
} else if (Array.isArray(query)) {
rawQuery = query.map((t) => `${t.key}='${t.value}'`).join(" AND ");
} else if (isSearchTxQueryArray(query)) {
rawQuery = query
.map((t) => {
// numeric values must not have quotes https://github.com/cosmos/cosmjs/issues/1462
if (typeof t.value === "string") return `${t.key}='${t.value}'`;
else return `${t.key}=${t.value}`;
})
.join(" AND ");
} else {
throw new Error("Got unsupported query type. See CosmJS 0.31 CHANGELOG for API breaking changes here.");
}
Expand Down