Merge pull request #1503 from cosmos/properly-handle-numeric-values

Handle key value pairs in tx search correctly for numbers
This commit is contained in:
Simon Warta 2023-11-07 12:21:11 +01:00 committed by GitHub
commit 9be43b0722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 12 deletions

View File

@ -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

View File

@ -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.");
}

View File

@ -113,7 +113,7 @@ export {
QueryClient,
QueryStoreResponse,
} from "./queryclient";
export { SearchTxQuery } from "./search";
export { isSearchTxQueryArray, SearchPair, SearchTxQuery } from "./search";
export {
createDefaultAminoConverters,
defaultRegistryTypes,

View File

@ -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);
}

View File

@ -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.");
}