Skip to content

Commit

Permalink
Merge pull request #1204 from cosmos/fix-decoding-of-omitted-attribut…
Browse files Browse the repository at this point in the history
…es-0.28

Fix decoding of events with no attributes
  • Loading branch information
webmaster128 committed Jun 29, 2022
2 parents e2e9864 + 8b5f2b8 commit 0cf6830
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : [],
};
}

Expand Down

0 comments on commit 0cf6830

Please sign in to comment.