diff --git a/effects/contracts/events.ts b/effects/contracts/events.ts new file mode 100644 index 000000000..8b39a5833 --- /dev/null +++ b/effects/contracts/events.ts @@ -0,0 +1,28 @@ +import * as $ from "../../deps/scale.ts" +import * as Z from "../../deps/zones.ts" +import { ContractMetadata, DeriveCodec } from "../../frame_metadata/mod.ts" +import { ExtrinsicEvent } from "../events.ts" + +export function events( + contractMetadata: Z.$, + events: Z.$, +) { + const $events = Z.ls(contractMetadata).next(([contractMetadata]) => { + return $.taggedUnion( + "type", + contractMetadata.V3.spec.events + .map((e) => [ + e.label, + [ + "value", + $.tuple(...e.args.map((a) => DeriveCodec(contractMetadata.V3.types)(a.type.type))), + ], + ]), + ) + }) + return Z.ls(events, $events).next(([events, $events]) => { + return events + .filter((e) => e.event?.type === "Contracts" && e.event?.value?.type === "ContractEmitted") + .map((e) => $events.decode(e.event?.value.data)) + }) +} diff --git a/effects/contracts/mod.ts b/effects/contracts/mod.ts index a4a0a9be5..af8dae12e 100644 --- a/effects/contracts/mod.ts +++ b/effects/contracts/mod.ts @@ -1,2 +1,3 @@ export * from "./call.ts" +export * from "./events.ts" export * from "./instantiate.ts" diff --git a/effects/events.ts b/effects/events.ts index 59661b662..a08e44088 100644 --- a/effects/events.ts +++ b/effects/events.ts @@ -24,10 +24,7 @@ export function events - phase: { value: number } - }[]>() + .as() return Z .ls(idx, events) .next(([idx, events]) => { @@ -36,3 +33,8 @@ export function events + phase: { value: number } +} diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts index b8a299d09..bbf6a9293 100644 --- a/examples/smart_contract.ts +++ b/examples/smart_contract.ts @@ -68,23 +68,11 @@ const contractAddress = U.throwIfError( ) class Contract> { - readonly $events - constructor( readonly client: Client, readonly contractMetadata: C.M.ContractMetadata, readonly contractAddress: Uint8Array, - ) { - const deriveCodec = C.M.DeriveCodec(contractMetadata.V3.types) - this.$events = C.$.taggedUnion( - "type", - contractMetadata.V3.spec.events - .map((e) => [ - e.label, - ["value", C.$.tuple(...e.args.map((a) => deriveCodec(a.type.type)))], - ]), - ) - } + ) {} call( sender: C.MultiAddress, @@ -126,15 +114,9 @@ class Contract> { return } ) - return C.Z.ls(finalizedIn, C.events(tx, finalizedIn)) - .next(([finalizedIn, events]) => { - const contractEvents: any[] = events - .filter((e) => - e.event?.type === "Contracts" && e.event?.value?.type === "ContractEmitted" - ) - .map((e) => this.$events.decode(e.event?.value.data)) - return [finalizedIn, events, contractEvents] - }) + const events = C.events(tx, finalizedIn) + const contractEvents = C.contracts.events(this.contractMetadata, events) + return C.Z.ls(finalizedIn, events, contractEvents) } // TODO: codegen each contract message as a method