From d763ceed8c7d5c75a9649364ae7792d220f3df9d Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 9 Dec 2020 15:49:37 +0000 Subject: [PATCH 01/16] stargate: Add logs helper --- packages/stargate/src/logs.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/stargate/src/logs.ts diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts new file mode 100644 index 00000000..2738b412 --- /dev/null +++ b/packages/stargate/src/logs.ts @@ -0,0 +1,11 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { logs } from "@cosmjs/launchpad"; + +export function parseRawLog(input = "[]"): readonly logs.Log[] { + const logsToParse = JSON.parse(input).map(({ events }: { events: readonly unknown[] }, i: number) => ({ + msg_index: i, + events, + log: "", + })); + return logs.parseLogs(logsToParse); +} From cf8c08a3f0294a823f72526add3fb8e45958ec33 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 23 Mar 2021 15:02:46 +0100 Subject: [PATCH 02/16] stargate: Incorporate launchpad logs --- packages/stargate/src/logs.ts | 86 +++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts index 2738b412..91be9109 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/stargate/src/logs.ts @@ -1,11 +1,91 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { logs } from "@cosmjs/launchpad"; +import { isNonNullObject } from "@cosmjs/utils"; -export function parseRawLog(input = "[]"): readonly logs.Log[] { +export interface Attribute { + readonly key: string; + readonly value: string; +} + +export interface Event { + readonly type: string; + readonly attributes: readonly Attribute[]; +} + +export interface Log { + readonly msg_index: number; + readonly log: string; + readonly events: readonly Event[]; +} + +export function parseAttribute(input: unknown): Attribute { + if (!isNonNullObject(input)) throw new Error("Attribute must be a non-null object"); + const { key, value } = input as any; + if (typeof key !== "string" || !key) throw new Error("Attribute's key must be a non-empty string"); + if (typeof value !== "string" && typeof value !== "undefined") { + throw new Error("Attribute's value must be a string or unset"); + } + + return { + key: key, + value: value || "", + }; +} + +export function parseEvent(input: unknown): Event { + if (!isNonNullObject(input)) throw new Error("Event must be a non-null object"); + const { type, attributes } = input as any; + if (typeof type !== "string" || type === "") { + throw new Error(`Event type must be a non-empty string`); + } + if (!Array.isArray(attributes)) throw new Error("Event's attributes must be an array"); + return { + type: type, + attributes: attributes.map(parseAttribute), + }; +} + +export function parseLog(input: unknown): Log { + if (!isNonNullObject(input)) throw new Error("Log must be a non-null object"); + const { msg_index, log, events } = input as any; + if (typeof msg_index !== "number") throw new Error("Log's msg_index must be a number"); + if (typeof log !== "string") throw new Error("Log's log must be a string"); + if (!Array.isArray(events)) throw new Error("Log's events must be an array"); + return { + msg_index: msg_index, + log: log, + events: events.map(parseEvent), + }; +} + +export function parseLogs(input: unknown): readonly Log[] { + if (!Array.isArray(input)) throw new Error("Logs must be an array"); + return input.map(parseLog); +} + +export function parseRawLog(input = "[]"): readonly Log[] { const logsToParse = JSON.parse(input).map(({ events }: { events: readonly unknown[] }, i: number) => ({ msg_index: i, events, log: "", })); - return logs.parseLogs(logsToParse); + return parseLogs(logsToParse); +} + +/** + * Searches in logs for the first event of the given event type and in that event + * for the first first attribute with the given attribute key. + * + * Throws if the attribute was not found. + */ +export function findAttribute(logs: readonly Log[], eventType: string, attrKey: string): Attribute { + const firstLogs = logs.find(() => true); + const out = firstLogs?.events + .find((event) => event.type === eventType) + ?.attributes.find((attr) => attr.key === attrKey); + if (!out) { + throw new Error( + `Could not find attribute '${attrKey}' in first event of type '${eventType}' in first log.`, + ); + } + return out; } From 02b6e38a6a612599cec44f7abde9c9e8beef60d8 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 3 Mar 2022 15:24:10 +0100 Subject: [PATCH 03/16] Organize queries by module --- .../stargate/src/queryclient/utils.spec.ts | 21 ++++++ packages/stargate/src/queryclient/utils.ts | 66 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 packages/stargate/src/queryclient/utils.spec.ts create mode 100644 packages/stargate/src/queryclient/utils.ts diff --git a/packages/stargate/src/queryclient/utils.spec.ts b/packages/stargate/src/queryclient/utils.spec.ts new file mode 100644 index 00000000..703efca4 --- /dev/null +++ b/packages/stargate/src/queryclient/utils.spec.ts @@ -0,0 +1,21 @@ +import { fromHex } from "@cosmjs/encoding"; + +import { decodeCosmosSdkDecFromProto } from "./utils"; + +describe("utils", () => { + describe("decodeCosmosSdkDecFromProto", () => { + it("works for string inputs", () => { + expect(decodeCosmosSdkDecFromProto("0").toString()).toEqual("0"); + expect(decodeCosmosSdkDecFromProto("1").toString()).toEqual("0.000000000000000001"); + expect(decodeCosmosSdkDecFromProto("3000000").toString()).toEqual("0.000000000003"); + expect(decodeCosmosSdkDecFromProto("123456789123456789").toString()).toEqual("0.123456789123456789"); + expect(decodeCosmosSdkDecFromProto("1234567891234567890").toString()).toEqual("1.23456789123456789"); + }); + + it("works for byte inputs", () => { + expect(decodeCosmosSdkDecFromProto(fromHex("313330303033343138373830313631333938")).toString()).toEqual( + "0.130003418780161398", + ); + }); + }); +}); diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/stargate/src/queryclient/utils.ts new file mode 100644 index 00000000..ccab7176 --- /dev/null +++ b/packages/stargate/src/queryclient/utils.ts @@ -0,0 +1,66 @@ +import { fromAscii, fromBech32 } from "@cosmjs/encoding"; +import { Decimal, Uint64 } from "@cosmjs/math"; +import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination"; +import Long from "long"; + +import { QueryClient } from "./queryclient"; + +/** + * Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded. + * This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data. + * The result is typically 20 bytes long but not restricted to that. + */ +export function toAccAddress(address: string): Uint8Array { + return fromBech32(address).data; +} + +/** + * If paginationKey is set, return a `PageRequest` with the given key. + * If paginationKey is unset, return `undefined`. + * + * Use this with a query response's pagination next key to + * request the next page. + */ +export function createPagination(paginationKey?: Uint8Array): PageRequest | undefined { + return paginationKey + ? PageRequest.fromPartial({ + key: paginationKey, + offset: Long.fromNumber(0, true), + limit: Long.fromNumber(0, true), + countTotal: false, + }) + : undefined; +} + +export interface ProtobufRpcClient { + request(service: string, method: string, data: Uint8Array): Promise; +} + +export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient { + return { + request: (service: string, method: string, data: Uint8Array): Promise => { + const path = `/${service}/${method}`; + return base.queryUnverified(path, data); + }, + }; +} + +/** + * Takes a uint64 value as string, number, Long or Uint64 and returns an unsigned Long instance + * of it. + */ +export function longify(value: string | number | Long | Uint64): Long { + const checkedValue = Uint64.fromString(value.toString()); + return Long.fromBytesBE([...checkedValue.toBytesBigEndian()], true); +} + +/** + * Takes a string or binary encoded `github.com/cosmos/cosmos-sdk/types.Dec` from the + * protobuf API and converts it into a `Decimal` with 18 fractional digits. + * + * See https://github.com/cosmos/cosmos-sdk/issues/10863 for more context why this is needed. + */ +export function decodeCosmosSdkDecFromProto(input: string | Uint8Array): Decimal { + const asString = typeof input === "string" ? input : fromAscii(input); + return Decimal.fromAtomics(asString, 18); +} From 265b887bb591f12fcc49f6f6f412ca9a51e90ab9 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 13 Jun 2022 14:00:39 +0200 Subject: [PATCH 04/16] Use Long.UZERO --- packages/stargate/src/queryclient/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/stargate/src/queryclient/utils.ts index ccab7176..fa086e90 100644 --- a/packages/stargate/src/queryclient/utils.ts +++ b/packages/stargate/src/queryclient/utils.ts @@ -25,8 +25,8 @@ export function createPagination(paginationKey?: Uint8Array): PageRequest | unde return paginationKey ? PageRequest.fromPartial({ key: paginationKey, - offset: Long.fromNumber(0, true), - limit: Long.fromNumber(0, true), + offset: Long.UZERO, + limit: Long.UZERO, countTotal: false, }) : undefined; From dd54e007555efea2505e810b5c9e47a0ce885236 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 11 Aug 2022 22:14:51 +0200 Subject: [PATCH 05/16] Simplify PageRequest generation No need to set default values --- packages/stargate/src/queryclient/utils.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/stargate/src/queryclient/utils.ts index fa086e90..a0fb6220 100644 --- a/packages/stargate/src/queryclient/utils.ts +++ b/packages/stargate/src/queryclient/utils.ts @@ -22,14 +22,7 @@ export function toAccAddress(address: string): Uint8Array { * request the next page. */ export function createPagination(paginationKey?: Uint8Array): PageRequest | undefined { - return paginationKey - ? PageRequest.fromPartial({ - key: paginationKey, - offset: Long.UZERO, - limit: Long.UZERO, - countTotal: false, - }) - : undefined; + return paginationKey ? PageRequest.fromPartial({ key: paginationKey }) : undefined; } export interface ProtobufRpcClient { From 6f2e9c9e967347ab33acf9b180f3caa9107b5eb6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 24 Oct 2022 12:48:01 +0200 Subject: [PATCH 06/16] Add structured `Events`s to `IndexTx.events` --- packages/stargate/src/logs.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts index 91be9109..f899c8fe 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/stargate/src/logs.ts @@ -1,15 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { isNonNullObject } from "@cosmjs/utils"; -export interface Attribute { - readonly key: string; - readonly value: string; -} - -export interface Event { - readonly type: string; - readonly attributes: readonly Attribute[]; -} +import { Attribute, Event } from "./events"; export interface Log { readonly msg_index: number; From 726bbb1e7427e0192a2f297683551a7ada7e7834 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 24 Oct 2022 12:48:01 +0200 Subject: [PATCH 07/16] Add structured `Events`s to `IndexTx.events` --- packages/stargate/src/events.ts | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/stargate/src/events.ts diff --git a/packages/stargate/src/events.ts b/packages/stargate/src/events.ts new file mode 100644 index 00000000..b4b70cce --- /dev/null +++ b/packages/stargate/src/events.ts @@ -0,0 +1,46 @@ +import { fromUtf8 } from "@cosmjs/encoding"; +import { tendermint34 } from "@cosmjs/tendermint-rpc"; + +/** + * An event attribute. + * + * This is the same attribute type as tendermint34.Attribute and tendermint35.EventAttribute + * but `key` and `value` are unified to strings. The conversion + * from bytes to string in the Tendermint 0.34 case should be done by performing + * [lossy] UTF-8 decoding. + * + * [lossy]: https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf8_lossy + */ +export interface Attribute { + readonly key: string; + readonly value: string; +} + +/** + * The same event type as tendermint34.Event and tendermint35.Event + * but attribute keys and values are unified to strings. The conversion + * from bytes to string in the Tendermint 0.34 case should be done by performing + * [lossy] UTF-8 decoding. + * + * [lossy]: https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf8_lossy + */ +export interface Event { + readonly type: string; + readonly attributes: readonly Attribute[]; +} + +/** + * Takes a Tendemrint 0.34 event with binary encoded key and value + * and converts it into an `Event` with string attributes. + */ +export function fromTendermint34Event(event: tendermint34.Event): Event { + return { + type: event.type, + attributes: event.attributes.map( + (attr): Attribute => ({ + key: fromUtf8(attr.key, true), + value: fromUtf8(attr.value, true), + }), + ), + }; +} From 81443eae0ef94df02c53db4d62fda12a54e06e4b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 24 Nov 2022 15:27:03 +0100 Subject: [PATCH 08/16] Add QueryClient.queryAbci and deprecate QueryClient.queryUnverified --- packages/stargate/src/queryclient/utils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/stargate/src/queryclient/utils.ts index a0fb6220..4afa3739 100644 --- a/packages/stargate/src/queryclient/utils.ts +++ b/packages/stargate/src/queryclient/utils.ts @@ -31,9 +31,10 @@ export interface ProtobufRpcClient { export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient { return { - request: (service: string, method: string, data: Uint8Array): Promise => { + request: async (service: string, method: string, data: Uint8Array): Promise => { const path = `/${service}/${method}`; - return base.queryUnverified(path, data); + const response = await base.queryAbci(path, data, undefined); + return response.value; }, }; } From e4d1a5d31d4548027294d402ea8c9e5e1e73c0a1 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 2 Mar 2023 14:10:52 +0100 Subject: [PATCH 09/16] Rename `fromTendermint34Event` to `fromTendermintEvent` and let it support both Tendermint 0.34 and 0.37 events as input --- packages/stargate/src/events.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/stargate/src/events.ts b/packages/stargate/src/events.ts index b4b70cce..57f12aaa 100644 --- a/packages/stargate/src/events.ts +++ b/packages/stargate/src/events.ts @@ -1,5 +1,5 @@ import { fromUtf8 } from "@cosmjs/encoding"; -import { tendermint34 } from "@cosmjs/tendermint-rpc"; +import { tendermint34, tendermint37 } from "@cosmjs/tendermint-rpc"; /** * An event attribute. @@ -30,16 +30,16 @@ export interface Event { } /** - * Takes a Tendemrint 0.34 event with binary encoded key and value + * Takes a Tendermint 0.34 or 0.37 event with binary encoded key and value * and converts it into an `Event` with string attributes. */ -export function fromTendermint34Event(event: tendermint34.Event): Event { +export function fromTendermintEvent(event: tendermint34.Event | tendermint37.Event): Event { return { type: event.type, attributes: event.attributes.map( (attr): Attribute => ({ - key: fromUtf8(attr.key, true), - value: fromUtf8(attr.value, true), + key: typeof attr.key == "string" ? attr.key : fromUtf8(attr.key, true), + value: typeof attr.value == "string" ? attr.value : fromUtf8(attr.value, true), }), ), }; From c59295b64b14c152a70f2e17ccb9b7231ca001c3 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 4 Oct 2023 11:07:55 +0300 Subject: [PATCH 10/16] Adapt codebase to cosmjs-types upgrade --- packages/stargate/src/queryclient/utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/stargate/src/queryclient/utils.ts index 4afa3739..6e658f83 100644 --- a/packages/stargate/src/queryclient/utils.ts +++ b/packages/stargate/src/queryclient/utils.ts @@ -1,7 +1,6 @@ import { fromAscii, fromBech32 } from "@cosmjs/encoding"; import { Decimal, Uint64 } from "@cosmjs/math"; import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination"; -import Long from "long"; import { QueryClient } from "./queryclient"; @@ -21,8 +20,8 @@ export function toAccAddress(address: string): Uint8Array { * Use this with a query response's pagination next key to * request the next page. */ -export function createPagination(paginationKey?: Uint8Array): PageRequest | undefined { - return paginationKey ? PageRequest.fromPartial({ key: paginationKey }) : undefined; +export function createPagination(paginationKey?: Uint8Array): PageRequest { + return paginationKey ? PageRequest.fromPartial({ key: paginationKey }) : PageRequest.fromPartial({}); } export interface ProtobufRpcClient { @@ -40,12 +39,12 @@ export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient { } /** - * Takes a uint64 value as string, number, Long or Uint64 and returns an unsigned Long instance + * Takes a uint64 value as string, number, BigInt or Uint64 and returns a BigInt * of it. */ -export function longify(value: string | number | Long | Uint64): Long { +export function longify(value: string | number | Uint64): bigint { const checkedValue = Uint64.fromString(value.toString()); - return Long.fromBytesBE([...checkedValue.toBytesBigEndian()], true); + return BigInt(checkedValue.toString()); } /** From 1a0b74475c1815d00c6b1ca8b2bb4fc4561b34cc Mon Sep 17 00:00:00 2001 From: Pynix Wang Date: Wed, 28 Feb 2024 14:29:19 +0800 Subject: [PATCH 11/16] fix for sdk >= v50 + remove parsedLogs + fix findAttribute + fill logs with [] --- packages/stargate/src/logs.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts index f899c8fe..93003664 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/stargate/src/logs.ts @@ -69,9 +69,8 @@ export function parseRawLog(input = "[]"): readonly Log[] { * * Throws if the attribute was not found. */ -export function findAttribute(logs: readonly Log[], eventType: string, attrKey: string): Attribute { - const firstLogs = logs.find(() => true); - const out = firstLogs?.events +export function findAttribute(events: readonly Event[], eventType: string, attrKey: string): Attribute { + const out = events .find((event) => event.type === eventType) ?.attributes.find((attr) => attr.key === attrKey); if (!out) { From 61284f71f93f80211f6ab7583b087948b4a23655 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 28 Feb 2024 19:01:11 +0100 Subject: [PATCH 12/16] Restore logs.findAttribute. Add event based findAttribute --- packages/stargate/src/logs.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts index 93003664..f899c8fe 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/stargate/src/logs.ts @@ -69,8 +69,9 @@ export function parseRawLog(input = "[]"): readonly Log[] { * * Throws if the attribute was not found. */ -export function findAttribute(events: readonly Event[], eventType: string, attrKey: string): Attribute { - const out = events +export function findAttribute(logs: readonly Log[], eventType: string, attrKey: string): Attribute { + const firstLogs = logs.find(() => true); + const out = firstLogs?.events .find((event) => event.type === eventType) ?.attributes.find((attr) => attr.key === attrKey); if (!out) { From 9d497fd3f6a51608434701e6556f8c266d63916f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 8 Mar 2024 12:26:53 +0100 Subject: [PATCH 13/16] Let `parseRawLog` gracefully handle empty strings --- packages/stargate/src/logs.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/stargate/src/logs.ts b/packages/stargate/src/logs.ts index f899c8fe..1e97d560 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/stargate/src/logs.ts @@ -54,7 +54,10 @@ export function parseLogs(input: unknown): readonly Log[] { return input.map(parseLog); } -export function parseRawLog(input = "[]"): readonly Log[] { +export function parseRawLog(input: string | undefined): readonly Log[] { + // Cosmos SDK >= 0.50 gives us an empty string here. This should be handled like undefined. + if (!input) return []; + const logsToParse = JSON.parse(input).map(({ events }: { events: readonly unknown[] }, i: number) => ({ msg_index: i, events, From 2992043598f91f9b66df91f48240b6340cf0c571 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Mon, 17 Feb 2025 11:42:52 +0800 Subject: [PATCH 14/16] mv stargate files to utils --- packages/{stargate => utils}/src/logs.ts | 8 ++--- .../queryclient => utils/src}/utils.spec.ts | 2 +- .../src/queryclient => utils/src}/utils.ts | 32 ++----------------- 3 files changed, 7 insertions(+), 35 deletions(-) rename packages/{stargate => utils}/src/logs.ts (89%) rename packages/{stargate/src/queryclient => utils/src}/utils.spec.ts (94%) rename packages/{stargate/src/queryclient => utils/src}/utils.ts (50%) diff --git a/packages/stargate/src/logs.ts b/packages/utils/src/logs.ts similarity index 89% rename from packages/stargate/src/logs.ts rename to packages/utils/src/logs.ts index 1e97d560..d8a6983d 100644 --- a/packages/stargate/src/logs.ts +++ b/packages/utils/src/logs.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { isNonNullObject } from "@cosmjs/utils"; +import { isObjectLike } from "@interchainjs/utils"; import { Attribute, Event } from "./events"; @@ -10,7 +10,7 @@ export interface Log { } export function parseAttribute(input: unknown): Attribute { - if (!isNonNullObject(input)) throw new Error("Attribute must be a non-null object"); + if (!isObjectLike(input)) throw new Error("Attribute must be a non-null object"); const { key, value } = input as any; if (typeof key !== "string" || !key) throw new Error("Attribute's key must be a non-empty string"); if (typeof value !== "string" && typeof value !== "undefined") { @@ -24,7 +24,7 @@ export function parseAttribute(input: unknown): Attribute { } export function parseEvent(input: unknown): Event { - if (!isNonNullObject(input)) throw new Error("Event must be a non-null object"); + if (!isObjectLike(input)) throw new Error("Event must be a non-null object"); const { type, attributes } = input as any; if (typeof type !== "string" || type === "") { throw new Error(`Event type must be a non-empty string`); @@ -37,7 +37,7 @@ export function parseEvent(input: unknown): Event { } export function parseLog(input: unknown): Log { - if (!isNonNullObject(input)) throw new Error("Log must be a non-null object"); + if (!isObjectLike(input)) throw new Error("Log must be a non-null object"); const { msg_index, log, events } = input as any; if (typeof msg_index !== "number") throw new Error("Log's msg_index must be a number"); if (typeof log !== "string") throw new Error("Log's log must be a string"); diff --git a/packages/stargate/src/queryclient/utils.spec.ts b/packages/utils/src/utils.spec.ts similarity index 94% rename from packages/stargate/src/queryclient/utils.spec.ts rename to packages/utils/src/utils.spec.ts index 703efca4..45613da8 100644 --- a/packages/stargate/src/queryclient/utils.spec.ts +++ b/packages/utils/src/utils.spec.ts @@ -1,4 +1,4 @@ -import { fromHex } from "@cosmjs/encoding"; +import { fromHex } from "@interchainjs/encoding"; import { decodeCosmosSdkDecFromProto } from "./utils"; diff --git a/packages/stargate/src/queryclient/utils.ts b/packages/utils/src/utils.ts similarity index 50% rename from packages/stargate/src/queryclient/utils.ts rename to packages/utils/src/utils.ts index 6e658f83..597c2143 100644 --- a/packages/stargate/src/queryclient/utils.ts +++ b/packages/utils/src/utils.ts @@ -1,8 +1,5 @@ -import { fromAscii, fromBech32 } from "@cosmjs/encoding"; -import { Decimal, Uint64 } from "@cosmjs/math"; -import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination"; - -import { QueryClient } from "./queryclient"; +import { fromAscii, fromBech32 } from "@interchainjs/encoding"; +import { Decimal, Uint64 } from "@interchainjs/math"; /** * Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded. @@ -13,31 +10,6 @@ export function toAccAddress(address: string): Uint8Array { return fromBech32(address).data; } -/** - * If paginationKey is set, return a `PageRequest` with the given key. - * If paginationKey is unset, return `undefined`. - * - * Use this with a query response's pagination next key to - * request the next page. - */ -export function createPagination(paginationKey?: Uint8Array): PageRequest { - return paginationKey ? PageRequest.fromPartial({ key: paginationKey }) : PageRequest.fromPartial({}); -} - -export interface ProtobufRpcClient { - request(service: string, method: string, data: Uint8Array): Promise; -} - -export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient { - return { - request: async (service: string, method: string, data: Uint8Array): Promise => { - const path = `/${service}/${method}`; - const response = await base.queryAbci(path, data, undefined); - return response.value; - }, - }; -} - /** * Takes a uint64 value as string, number, BigInt or Uint64 and returns a BigInt * of it. From 2f9450f48f7781cd38cbea4e753cc45751ec0686 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Mon, 17 Feb 2025 11:54:22 +0800 Subject: [PATCH 15/16] fix deps for logs --- packages/{stargate => utils}/src/events.ts | 21 +-------------------- packages/utils/src/index.ts | 3 +++ packages/utils/src/logs.ts | 2 +- 3 files changed, 5 insertions(+), 21 deletions(-) rename packages/{stargate => utils}/src/events.ts (59%) diff --git a/packages/stargate/src/events.ts b/packages/utils/src/events.ts similarity index 59% rename from packages/stargate/src/events.ts rename to packages/utils/src/events.ts index 57f12aaa..3ca2de52 100644 --- a/packages/stargate/src/events.ts +++ b/packages/utils/src/events.ts @@ -1,6 +1,3 @@ -import { fromUtf8 } from "@cosmjs/encoding"; -import { tendermint34, tendermint37 } from "@cosmjs/tendermint-rpc"; - /** * An event attribute. * @@ -27,20 +24,4 @@ export interface Attribute { export interface Event { readonly type: string; readonly attributes: readonly Attribute[]; -} - -/** - * Takes a Tendermint 0.34 or 0.37 event with binary encoded key and value - * and converts it into an `Event` with string attributes. - */ -export function fromTendermintEvent(event: tendermint34.Event | tendermint37.Event): Event { - return { - type: event.type, - attributes: event.attributes.map( - (attr): Attribute => ({ - key: typeof attr.key == "string" ? attr.key : fromUtf8(attr.key, true), - value: typeof attr.value == "string" ? attr.value : fromUtf8(attr.value, true), - }), - ), - }; -} +} \ No newline at end of file diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index d6037275..1505ea7c 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -8,3 +8,6 @@ export * from "./arrays"; export * from "./typechecks"; export * from "./chain"; export * from "./rpc"; +export * from "./utils"; +export * from "./logs"; +export * from "./events"; \ No newline at end of file diff --git a/packages/utils/src/logs.ts b/packages/utils/src/logs.ts index d8a6983d..8f84d446 100644 --- a/packages/utils/src/logs.ts +++ b/packages/utils/src/logs.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { isObjectLike } from "@interchainjs/utils"; +import { isObjectLike } from "./typechecks"; import { Attribute, Event } from "./events"; From 06a62f17cafee9fa32dc1f154a54d3682dd8ce42 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Mon, 17 Feb 2025 12:44:02 +0800 Subject: [PATCH 16/16] fix deps --- packages/encoding/package.json | 1 + packages/encoding/src/index.ts | 1 + packages/{utils => encoding}/src/utils.spec.ts | 2 +- packages/{utils => encoding}/src/utils.ts | 3 ++- packages/utils/package.json | 2 ++ packages/utils/src/index.ts | 1 - yarn.lock | 12 ++++++++++++ 7 files changed, 19 insertions(+), 3 deletions(-) rename packages/{utils => encoding}/src/utils.spec.ts (94%) rename packages/{utils => encoding}/src/utils.ts (93%) diff --git a/packages/encoding/package.json b/packages/encoding/package.json index 92b41678..9106c71e 100644 --- a/packages/encoding/package.json +++ b/packages/encoding/package.json @@ -27,6 +27,7 @@ "lint": "eslint . --fix" }, "dependencies": { + "@interchainjs/math": "1.9.11", "base64-js": "^1.3.0", "bech32": "^1.1.4", "readonly-date": "^1.0.0" diff --git a/packages/encoding/src/index.ts b/packages/encoding/src/index.ts index a2dc4771..bd66880f 100644 --- a/packages/encoding/src/index.ts +++ b/packages/encoding/src/index.ts @@ -4,3 +4,4 @@ export { fromBech32, normalizeBech32, toBech32 } from "./bech32"; export { fromHex, toHex } from "./hex"; export { fromRfc3339, toRfc3339 } from "./rfc3339"; export { fromUtf8, toUtf8 } from "./utf8"; +export { toAccAddress, longify, decodeCosmosSdkDecFromProto } from "./utils"; \ No newline at end of file diff --git a/packages/utils/src/utils.spec.ts b/packages/encoding/src/utils.spec.ts similarity index 94% rename from packages/utils/src/utils.spec.ts rename to packages/encoding/src/utils.spec.ts index 45613da8..5db66c1f 100644 --- a/packages/utils/src/utils.spec.ts +++ b/packages/encoding/src/utils.spec.ts @@ -1,4 +1,4 @@ -import { fromHex } from "@interchainjs/encoding"; +import { fromHex } from "./hex"; import { decodeCosmosSdkDecFromProto } from "./utils"; diff --git a/packages/utils/src/utils.ts b/packages/encoding/src/utils.ts similarity index 93% rename from packages/utils/src/utils.ts rename to packages/encoding/src/utils.ts index 597c2143..694c5cd9 100644 --- a/packages/utils/src/utils.ts +++ b/packages/encoding/src/utils.ts @@ -1,4 +1,5 @@ -import { fromAscii, fromBech32 } from "@interchainjs/encoding"; +import { fromAscii } from "./ascii"; +import { fromBech32 } from "./bech32"; import { Decimal, Uint64 } from "@interchainjs/math"; /** diff --git a/packages/utils/package.json b/packages/utils/package.json index 203771a9..78e9cbc8 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -25,6 +25,8 @@ "lint": "eslint . --fix" }, "dependencies": { + "@chain-registry/v2": "1.71.71", + "@chain-registry/v2-types": "0.53.72", "@interchainjs/types": "1.9.11", "bech32": "^2.0.0", "decimal.js": "^10.4.3" diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 1505ea7c..b083becd 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -8,6 +8,5 @@ export * from "./arrays"; export * from "./typechecks"; export * from "./chain"; export * from "./rpc"; -export * from "./utils"; export * from "./logs"; export * from "./events"; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 9221a291..238edf21 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1116,6 +1116,18 @@ resolved "https://registry.yarnpkg.com/@chain-registry/v2-types/-/v2-types-0.53.68.tgz#83173c3e79c7c89a382c4017e15db71970038847" integrity sha512-MCK9RKJ67VYWCJ0HtnDYJIottx4paoo6wKMbH3s6xWFzLVwufJWi0iKMoppVOIXWk+yr+h9W3puaLJPnYNez9A== +"@chain-registry/v2-types@0.53.72", "@chain-registry/v2-types@^0.53.40": + version "0.53.72" + resolved "https://registry.yarnpkg.com/@chain-registry/v2-types/-/v2-types-0.53.72.tgz#3621cc1e94cacb430c657c2af63d4825950b880f" + integrity sha512-HIbDFK0R1aZTbXdTN7FOZI+z3lHt4ZQWRYDctUk3IwvGxOC04gYCa45SfsHx4YpqALXa0hu9Acj5fUSp4mnZ5w== + +"@chain-registry/v2@1.71.71": + version "1.71.71" + resolved "https://registry.yarnpkg.com/@chain-registry/v2/-/v2-1.71.71.tgz#648eab79a487a2680c77b85fab96d5c5d5fb3eea" + integrity sha512-JdzJHRduw58io8ZOKy7mnAt9oFJqWSa5Bn5srWXG5326ztUCe1MLd9yZMMCos57mc4UFrOJm8Gr9V7lKtEZvjQ== + dependencies: + "@chain-registry/v2-types" "^0.53.40" + "@chain-registry/v2@^1.65.6": version "1.71.121" resolved "https://registry.yarnpkg.com/@chain-registry/v2/-/v2-1.71.121.tgz#38baa88c2d6eb59e8996a0bc2b754b2f492468d9"