Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add contract event effect
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico committed Dec 19, 2022
1 parent 1669134 commit 91a9a90
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
28 changes: 28 additions & 0 deletions effects/contracts/events.ts
Original file line number Diff line number Diff line change
@@ -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.$<ContractMetadata>,
events: Z.$<ExtrinsicEvent[]>,
) {
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))
})
}
1 change: 1 addition & 0 deletions effects/contracts/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./call.ts"
export * from "./events.ts"
export * from "./instantiate.ts"
10 changes: 6 additions & 4 deletions effects/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ export function events<Extrinsic extends SignedExtrinsic, FinalizedHash extends
}, k1_)
const events = entryRead(client)("System", "Events", [], finalizedHash)
.access("value")
.as<{
event?: Record<string, any>
phase: { value: number }
}[]>()
.as<ExtrinsicEvent[]>()
return Z
.ls(idx, events)
.next(([idx, events]) => {
Expand All @@ -36,3 +33,8 @@ export function events<Extrinsic extends SignedExtrinsic, FinalizedHash extends
})
}, k2_)
}

export type ExtrinsicEvent = {
event?: Record<string, any>
phase: { value: number }
}
26 changes: 4 additions & 22 deletions examples/smart_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,11 @@ const contractAddress = U.throwIfError(
)

class Contract<Client extends C.Z.Effect<C.rpc.Client>> {
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<Args extends any[]>(
sender: C.MultiAddress,
Expand Down Expand Up @@ -126,15 +114,9 @@ class Contract<Client extends C.Z.Effect<C.rpc.Client>> {
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
Expand Down

0 comments on commit 91a9a90

Please sign in to comment.