Skip to content

Commit

Permalink
Add structured Eventss to IndexTx.events
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 24, 2022
1 parent 68f3dee commit 98d6377
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to

- @cosmjs/encoding: Add `lossy` parameter to `fromUtf8` allowing the use of a
replacement charater instead of throwing.
- @cosmjs/stargate: Add structured `Events`s to `IndexTx.events`.

## [0.29.2] - 2022-10-13

Expand Down
2 changes: 2 additions & 0 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Expand Up @@ -10,6 +10,7 @@ import {
BroadcastTxError,
Coin,
DeliverTxResponse,
fromTendermint34Event,
IndexedTx,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
Expand Down Expand Up @@ -462,6 +463,7 @@ export class CosmWasmClient {
height: tx.height,
hash: toHex(tx.hash).toUpperCase(),
code: tx.result.code,
events: tx.result.events.map(fromTendermint34Event),
rawLog: tx.result.log || "",
tx: tx.tx,
gasUsed: tx.result.gasUsed,
Expand Down
46 changes: 46 additions & 0 deletions 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),
}),
),
};
}
1 change: 1 addition & 0 deletions packages/stargate/src/index.ts
@@ -1,5 +1,6 @@
export { Account, accountFromAny, AccountParser } from "./accounts";
export { AminoConverter, AminoConverters, AminoTypes } from "./aminotypes";
export { Attribute, Event, fromTendermint34Event } from "./events";
export { calculateFee, GasPrice } from "./fee";
export * as logs from "./logs";
export {
Expand Down
10 changes: 1 addition & 9 deletions 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;
Expand Down
10 changes: 10 additions & 0 deletions packages/stargate/src/stargateclient.ts
Expand Up @@ -10,6 +10,7 @@ import { QueryDelegatorDelegationsResponse } from "cosmjs-types/cosmos/staking/v
import { DelegationResponse } from "cosmjs-types/cosmos/staking/v1beta1/staking";

import { Account, accountFromAny, AccountParser } from "./accounts";
import { Event, fromTendermint34Event } from "./events";
import {
AuthExtension,
BankExtension,
Expand Down Expand Up @@ -64,6 +65,14 @@ export interface IndexedTx {
readonly hash: string;
/** Transaction execution error code. 0 on success. */
readonly code: number;
readonly events: readonly Event[];
/**
* A string-based log document.
*
* This currently seems to merge attributes of multiple events into one event per type
* (https://github.com/tendermint/tendermint/issues/9595). You might want to use the `events`
* field instead.
*/
readonly rawLog: string;
/**
* Raw transaction bytes stored in Tendermint.
Expand Down Expand Up @@ -453,6 +462,7 @@ export class StargateClient {
height: tx.height,
hash: toHex(tx.hash).toUpperCase(),
code: tx.result.code,
events: tx.result.events.map(fromTendermint34Event),
rawLog: tx.result.log || "",
tx: tx.tx,
gasUsed: tx.result.gasUsed,
Expand Down

0 comments on commit 98d6377

Please sign in to comment.