Adapt handling of logs/attributes/events for wasm
This commit is contained in:
parent
542939a63e
commit
9f44e296cd
@ -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,
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
4
packages/sdk/types/logs.d.ts
vendored
4
packages/sdk/types/logs.d.ts
vendored
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user