diff --git a/packages/sdk/src/logs.ts b/packages/sdk/src/logs.ts index 79d7b915..496fe2ca 100644 --- a/packages/sdk/src/logs.ts +++ b/packages/sdk/src/logs.ts @@ -1,13 +1,22 @@ /* eslint-disable @typescript-eslint/camelcase */ import { isNonNullObject } from "@iov/encoding"; +const supportedEventTypes: readonly string[] = ["message", "transfer", "wasm"]; + +export type SupportedEventType = "message" | "transfer" | "wasm"; + +export function isSupportedEventType(data: any): data is SupportedEventType { + if (typeof data !== "string") return false; + return supportedEventTypes.includes(data); +} + export interface Attribute { readonly key: string; readonly value: string; } export interface Event { - readonly type: "message" | "transfer"; + readonly type: SupportedEventType; readonly attributes: readonly Attribute[]; } @@ -34,7 +43,9 @@ 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"); const { type, attributes } = input as any; - if (type !== "message" && type !== "transfer") throw new Error("Event must be of type message or transfer"); + if (!isSupportedEventType(type)) { + throw new Error(`Event type must be one of ${supportedEventTypes.join(", ")}; got ${type}`); + } if (!Array.isArray(attributes)) throw new Error("Event's attributes must be an array"); return { type: type, diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index b6c05d02..5aa6884a 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -581,9 +581,15 @@ describe("RestClient", () => { { const result = await executeContract(client, pen, contractAddress); expect(result.code).toBeFalsy(); - // console.log("Raw log:", result.raw_log); - const [firstLog] = parseLogs(result.logs); - expect(firstLog.log).toEqual(`released funds to ${beneficiaryAddress}`); + // console.log("Raw log:", result.logs); + const logs = parseLogs(result.logs); + const wasmEvent = logs.find(() => true)?.events.find(e => e.type === "wasm"); + assert(wasmEvent, "Event of type wasm expected"); + expect(wasmEvent.attributes).toContain({ key: "action", value: "release" }); + expect(wasmEvent.attributes).toContain({ + key: "destination", + value: beneficiaryAddress, + }); // Verify token transfer from contract to beneficiary const beneficiaryBalance = (await client.authAccounts(beneficiaryAddress)).result.value.coins; diff --git a/packages/sdk/src/signingcosmwasmclient.spec.ts b/packages/sdk/src/signingcosmwasmclient.spec.ts index defd9806..6a4208ea 100644 --- a/packages/sdk/src/signingcosmwasmclient.spec.ts +++ b/packages/sdk/src/signingcosmwasmclient.spec.ts @@ -144,8 +144,13 @@ describe("SigningCosmWasmClient", () => { // execute const result = await client.execute(contractAddress, { release: {} }, undefined); - const [firstLog] = result.logs; - expect(firstLog.log).toEqual(`released funds to ${beneficiaryAddress}`); + const wasmEvent = result.logs.find(() => true)?.events.find(e => e.type === "wasm"); + assert(wasmEvent, "Event of type wasm expected"); + expect(wasmEvent.attributes).toContain({ key: "action", value: "release" }); + expect(wasmEvent.attributes).toContain({ + key: "destination", + value: beneficiaryAddress, + }); // Verify token transfer from contract to beneficiary const rest = new RestClient(httpUrl); diff --git a/packages/sdk/types/logs.d.ts b/packages/sdk/types/logs.d.ts index 9a9e92c7..a84e8276 100644 --- a/packages/sdk/types/logs.d.ts +++ b/packages/sdk/types/logs.d.ts @@ -1,9 +1,11 @@ +export declare type SupportedEventType = "message" | "transfer" | "wasm"; +export declare function isSupportedEventType(data: any): data is SupportedEventType; export interface Attribute { readonly key: string; readonly value: string; } export interface Event { - readonly type: "message" | "transfer"; + readonly type: SupportedEventType; readonly attributes: readonly Attribute[]; } export interface Log {