From 0f95a20b4d7b829b128a7f10c8dc4c84aee5e971 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 7 Nov 2023 12:02:07 +0100 Subject: [PATCH] Handle key value pairs in tx search correctly for numbers --- CHANGELOG.md | 7 +++++++ .../cosmwasm-stargate/src/cosmwasmclient.ts | 11 +++++++++-- packages/stargate/src/index.ts | 2 +- packages/stargate/src/search.ts | 17 +++++++++++------ packages/stargate/src/stargateclient.ts | 12 +++++++++--- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a604da1..efa363a5 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 c2e27871..c4b3e14c 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 0e999d79..e424bed7 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 fd49c2a2..a26942e3 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 b48aaacc..b958d990 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."); }