diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a604da1ba..efa363a5e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index c2e278712c..c4b3e14c5c 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -12,6 +12,7 @@ import { DeliverTxResponse, fromTendermintEvent, IndexedTx, + isSearchTxQueryArray, QueryClient, SearchTxQuery, SequenceResponse, @@ -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."); } diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index 0e999d79af..e424bed7c2 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -113,7 +113,7 @@ export { QueryClient, QueryStoreResponse, } from "./queryclient"; -export { SearchTxQuery } from "./search"; +export { isSearchTxQueryArray, SearchPair, SearchTxQuery } from "./search"; export { createDefaultAminoConverters, defaultRegistryTypes, diff --git a/packages/stargate/src/search.ts b/packages/stargate/src/search.ts index fd49c2a2f4..a26942e312 100644 --- a/packages/stargate/src/search.ts +++ b/packages/stargate/src/search.ts @@ -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); +} diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index b48aaacc7b..b958d990a0 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -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; @@ -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."); }