import "../_dnt.polyfills.js"; import { is, Rune, ValueRune } from "../rune/mod.js" import { Chain } from "./ChainRune.js" import { CodecRune } from "./CodecRune.js" import { EventsRune, isSystemExtrinsicFailedEvent, SystemExtrinsicFailedEvent, } from "./EventsRune.js" export class ExtrinsicEventsRune extends EventsRune { // TODO: // handleFailed unhandleFailed() { const dispatchError = this .into(ValueRune) .map((events: any) => events.find(isSystemExtrinsicFailedEvent) as SystemExtrinsicFailedEvent | undefined ) .unhandle(is(undefined)) .access("event", "value", "dispatchError") .map((dispatchError) => { // TODO: fix if ((dispatchError as any).type === "Module") return (dispatchError as any).value return new ExtrinsicError(dispatchError as any) }) .unhandle(is(ExtrinsicError)) return Rune .tuple([ this.chain.metadata.into(ValueRune).access("pallets"), dispatchError.access("index"), ]) .map(([pallets, id]) => Object.values(pallets).find((pallet) => pallet.id === id)!) .access("types", "error") .unhandle(is(undefined)) .into(CodecRune) .decoded(dispatchError.access("error")) .map((data) => new ExtrinsicError((typeof data === "string" ? { type: data } : data) as any)) .unhandle(is(ExtrinsicError)) .rehandle(is(undefined), () => this /* TODO: type-level exclusions? */) } } export class ExtrinsicError extends Error { override readonly name = "ExtrinsicError" constructor(data: D) { super(data.type) } }