Fix decoding of events with no attributes

This commit is contained in:
Simon Warta 2022-06-29 08:52:00 +02:00
parent e2e9864192
commit 8b5f2b8a4e
3 changed files with 38 additions and 5 deletions

View File

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

View File

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

View File

@ -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) : [],
};
}