From 8b5f2b8a4e875562ae8562de487c9c4cd75cd180 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 29 Jun 2022 08:52:00 +0200 Subject: [PATCH] Fix decoding of events with no attributes --- CHANGELOG.md | 6 ++++ .../tendermint34/adaptor/responses.spec.ts | 30 +++++++++++++++++-- .../src/tendermint34/adaptor/responses.ts | 7 +++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e809dfdefa..e34e8dc38f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to ## [Unreleased] +### Fixed + +- @cosmjs/tendermint-rpc: Fix decoding events without attributes ([#1198]). + +[#1198]: https://github.com/cosmos/cosmjs/pull/1198 + ## [0.28.9] - 2022-06-21 This version replaces the 0.28.8 release which was erroneously tagged as 0.26.8 diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts index d00a31209d..0ad26490ab 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts @@ -1,9 +1,35 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { fromBase64, fromHex } from "@cosmjs/encoding"; +import { fromBase64, fromHex, toUtf8 } from "@cosmjs/encoding"; -import { decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; +import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; describe("Adaptor Responses", () => { + describe("decodeEvent", () => { + it("works with attributes", () => { + // from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF + const event = decodeEvent({ + type: "coin_spent", + attributes: [ + { key: "c3BlbmRlcg==", value: "dGdyYWRlMWpzN2V6cm01NWZxZ3h1M3A2MmQ5eG42cGF0amt1Mno3bmU1ZHZn" }, + { key: "YW1vdW50", value: "NjAwMDAwMDAwMHV0Z2Q=" }, + ], + }); + expect(event.type).toEqual("coin_spent"); + expect(event.attributes).toEqual([ + { key: toUtf8("spender"), value: toUtf8("tgrade1js7ezrm55fqgxu3p62d9xn6patjku2z7ne5dvg") }, + { key: toUtf8("amount"), value: toUtf8("6000000000utgd") }, + ]); + }); + + it("works with no attribute", () => { + const event = decodeEvent({ + type: "cosmos.module.EmittedEvent", + }); + expect(event.type).toEqual("cosmos.module.EmittedEvent"); + expect(event.attributes).toEqual([]); + }); + }); + describe("decodeValidatorGenesis", () => { it("works for genesis format", () => { // from https://raw.githubusercontent.com/cosmos/mainnet/master/genesis.json diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts index f4ac865fa3..e7f1eefc33 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts @@ -111,13 +111,14 @@ function decodeAttributes(attributes: readonly RpcAttribute[]): responses.Attrib interface RpcEvent { readonly type: string; - readonly attributes: readonly RpcAttribute[]; + /** Can be omitted (see https://github.com/cosmos/cosmjs/pull/1198) */ + readonly attributes?: readonly RpcAttribute[]; } -function decodeEvent(event: RpcEvent): responses.Event { +export function decodeEvent(event: RpcEvent): responses.Event { return { type: event.type, - attributes: decodeAttributes(event.attributes), + attributes: event.attributes ? decodeAttributes(event.attributes) : [], }; }