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

feat: event extraction util #353

Merged
merged 9 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions effects/$extrinsic.ts

This file was deleted.

16 changes: 0 additions & 16 deletions effects/$storageKey.ts

This file was deleted.

32 changes: 0 additions & 32 deletions effects/blockRead.ts

This file was deleted.

14 changes: 9 additions & 5 deletions effects/blockWatch.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import * as Z from "../deps/zones.ts";
import * as M from "../frame_metadata/mod.ts";
import * as known from "../known/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { blockRead } from "./blockRead.ts";
import { chain } from "./rpc_known.ts";

const k0_ = Symbol();

export function blockWatch<Client extends Z.$<rpc.Client>>(client: Client) {
return <
Listener extends Z.$<U.Listener<known.SignedBlock<M.Extrinsic>, rpc.ClientSubscribeContext>>,
Listener extends Z.$<U.Listener<known.SignedBlock, BlockWatchListenerContext>>,
>(listener: Listener) => {
const listenerMapped = Z
.ls(listener, Z.env)
.next(([listener, env]) => {
return async function(this: rpc.ClientSubscribeContext, header: known.Header) {
const blockHash = chain.getBlockHash(client)(header.number);
const block = await blockRead(client)(blockHash).bind(env)();
const block = await chain.getBlock(client)(blockHash).bind(env)();
// TODO: return error with `this.stop` once implemented
if (block instanceof Error) throw block;
listener.apply(this, [block]);
listener.apply({ ...this, env }, [block]);
};
}, k0_);
const subscriptionId = chain.subscribeNewHeads(client)([], listenerMapped);
Expand All @@ -28,3 +27,8 @@ export function blockWatch<Client extends Z.$<rpc.Client>>(client: Client) {
.zoned("BlockWatch");
};
}

// TODO: generalize creating watch effects + accessing context + halting with a value
export interface BlockWatchListenerContext extends rpc.ClientSubscribeContext {
env: Z.Env;
}
11 changes: 0 additions & 11 deletions effects/codec.ts

This file was deleted.

10 changes: 4 additions & 6 deletions effects/const.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { codec } from "./codec.ts";
import { deriveCodec } from "./deriveCodec.ts";
import { constMetadata, metadata, palletMetadata } from "./metadata.ts";
import * as e$ from "./scale.ts";
import * as scale from "./scale.ts";

export function const_<Client extends Z.$<rpc.Client>>(client: Client) {
return <
Expand All @@ -17,13 +15,13 @@ export function const_<Client extends Z.$<rpc.Client>>(client: Client) {
...[blockHash]: [...Rest]
) => {
const metadata_ = metadata(client)(blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const deriveCodec_ = scale.deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const constMetadata_ = constMetadata(palletMetadata_, constName);
const entryValueTypeI = constMetadata_.access("ty").access("id");
const constValue = constMetadata_.access("value");
const $const = codec(deriveCodec_, entryValueTypeI);
return e$.scaleDecoded($const, constValue, "value").zoned("Const");
const $const = scale.codec(deriveCodec_, entryValueTypeI);
return scale.scaleDecoded($const, constValue, "value").zoned("Const");
};
}
Object.defineProperty(const_, "name", {
Expand Down
8 changes: 0 additions & 8 deletions effects/deriveCodec.ts

This file was deleted.

15 changes: 6 additions & 9 deletions effects/entryRead.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { $storageKey } from "./$storageKey.ts";
import { codec } from "./codec.ts";
import { deriveCodec } from "./deriveCodec.ts";
import { entryMetadata, metadata, palletMetadata } from "./metadata.ts";
import { state } from "./rpc_known.ts";
import * as e$ from "./scale.ts";
import * as scale from "./scale.ts";

export function entryRead<Client extends Z.$<rpc.Client>>(client: Client) {
return <
Expand All @@ -21,15 +18,15 @@ export function entryRead<Client extends Z.$<rpc.Client>>(client: Client) {
...[blockHash]: [...Rest]
) => {
const metadata_ = metadata(client)(blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const deriveCodec_ = scale.deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const entryMetadata_ = entryMetadata(palletMetadata_, entryName);
const $storageKey_ = $storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const storageKey = e$.scaleEncoded($storageKey_, Z.ls(...keys)).next(U.hex.encode);
const $storageKey_ = scale.$storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const storageKey = scale.scaleEncoded($storageKey_, Z.ls(...keys)).next(U.hex.encode);
const storageBytesHex = state.getStorage(client)(storageKey, blockHash);
const storageBytes = storageBytesHex.next(U.hex.decode);
const entryValueTypeI = entryMetadata_.access("value");
const $entry = codec(deriveCodec_, entryValueTypeI);
return e$.scaleDecoded($entry, storageBytes, "value").zoned("EntryRead");
const $entry = scale.codec(deriveCodec_, entryValueTypeI);
return scale.scaleDecoded($entry, storageBytes, "value").zoned("EntryRead");
};
}
13 changes: 5 additions & 8 deletions effects/entryWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import * as Z from "../deps/zones.ts";
import * as known from "../known/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { $storageKey } from "./$storageKey.ts";
import { codec } from "./codec.ts";
import { deriveCodec } from "./deriveCodec.ts";
import { entryMetadata, metadata, palletMetadata } from "./metadata.ts";
import { state } from "./rpc_known.ts";
import * as e$ from "./scale.ts";
import * as scale from "./scale.ts";

export type WatchEntryEvent = [key?: unknown, value?: unknown];

Expand All @@ -25,13 +22,13 @@ export function entryWatch<Client extends Z.$<rpc.Client>>(client: Client) {
listener: U.Listener<WatchEntryEvent[], rpc.ClientSubscribeContext>,
) => {
const metadata_ = metadata(client)();
const deriveCodec_ = deriveCodec(metadata_);
const deriveCodec_ = scale.deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const entryMetadata_ = entryMetadata(palletMetadata_, entryName);
const $storageKey_ = $storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const $storageKey_ = scale.$storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const entryValueTypeI = entryMetadata_.access("value");
const $entry = codec(deriveCodec_, entryValueTypeI);
const storageKeys = e$
const $entry = scale.codec(deriveCodec_, entryValueTypeI);
const storageKeys = scale
.scaleEncoded($storageKey_, keys.length ? [keys] : [])
.next(U.hex.encode)
.next(U.tuple);
Expand Down
35 changes: 35 additions & 0 deletions effects/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Z from "../deps/zones.ts";
import * as known from "../known/mod.ts";
import { entryRead } from "./entryRead.ts";
import { SignedExtrinsic } from "./extrinsic.ts";
import { chain } from "./rpc_known.ts";

const k1_ = Symbol();
const k2_ = Symbol();

// TODO: attach this to `Extrinsic`?
export function events<Extrinsic extends SignedExtrinsic, FinalizedHash extends Z.$<known.Hash>>(
extrinsic: Extrinsic,
finalizedHash: FinalizedHash,
) {
const client = extrinsic.client as Extrinsic["client"];
const extrinsics = chain
.getBlock(client)(finalizedHash)
.access("block")
.access("extrinsics");
const idx = Z
.ls(extrinsics, extrinsic.extrinsicHex as Extrinsic["extrinsicHex"])
.next(([extrinsics, extrinsicHex]) => {
return extrinsics.indexOf(extrinsicHex);
}, k1_);
const events = entryRead(client)("System", "Events", [], finalizedHash)
.access("value")
.as<{ phase: { value: number } }[]>();
return Z
.ls(idx, events)
.next(([idx, events]) => {
return events.filter((event) => {
return event.phase.value === idx;
});
}, k2_);
}
60 changes: 42 additions & 18 deletions effects/extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import * as known from "../known/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as ss58 from "../ss58/mod.ts";
import * as U from "../util/mod.ts";
import { $extrinsic } from "./$extrinsic.ts";
import { const as const_ } from "./const.ts";
import { deriveCodec } from "./deriveCodec.ts";
import { metadata } from "./metadata.ts";
import { author, chain, system } from "./rpc_known.ts";
import * as e$ from "./scale.ts";
import * as scale from "./scale.ts";

const k0_ = Symbol();

Expand All @@ -35,8 +33,8 @@ export function extrinsic<Client extends Z.$<rpc.Client>>(client: Client) {
}

export class Extrinsic<
Client extends Z.$<rpc.Client>,
Props extends Z.Rec$<ExtrinsicProps>,
Client extends Z.$<rpc.Client> = Z.$<rpc.Client>,
Props extends Z.Rec$<ExtrinsicProps> = Z.Rec$<ExtrinsicProps>,
> {
constructor(
readonly client: Client,
Expand All @@ -49,14 +47,16 @@ export class Extrinsic<
}

export class SignedExtrinsic<
Client extends Z.$<rpc.Client>,
Props extends Z.Rec$<ExtrinsicProps>,
Sign extends Z.$<M.Signer>,
Client extends Z.$<rpc.Client> = Z.$<rpc.Client>,
Props extends Z.Rec$<ExtrinsicProps> = Z.Rec$<ExtrinsicProps>,
Sign extends Z.$<M.Signer> = Z.$<M.Signer>,
> {
client;
props;
sign;
extrinsic;
extrinsicBytes;
extrinsicHex;
extrinsicDecoded;

constructor(
client: Client,
Expand All @@ -67,12 +67,10 @@ export class SignedExtrinsic<
this.props = props as Z.Rec$Access<Props>;
this.sign = sign as Sign;

const metadata_ = metadata(this.client)();
const deriveCodec_ = deriveCodec(metadata_);
const addrPrefix = const_(this.client)("System", "SS58Prefix")
.access("value")
.as<number>();
const $extrinsic_ = $extrinsic(deriveCodec_, metadata_, this.sign, addrPrefix);
const $extrinsic_ = $extrinsic(this.client, this.sign);
const versions = const_(this.client)("System", "Version")
.access("value");
const specVersion = versions
Expand Down Expand Up @@ -114,24 +112,50 @@ export class SignedExtrinsic<
args: this.props.args,
signature,
});
this.extrinsic = e$
.scaleEncoded($extrinsic_, $extrinsicProps, true)
.next(U.hex.encode);
this.extrinsicBytes = scale.scaleEncoded($extrinsic_, $extrinsicProps, true);
this.extrinsicHex = this.extrinsicBytes.next(U.hex.encodePrefixed);
this.extrinsicDecoded = scale.scaleDecoded($extrinsic_, this.extrinsicBytes, "extrinsic");
}

watch<Listener extends Z.$<U.Listener<known.TransactionStatus, rpc.ClientSubscribeContext>>>(
listener: Listener,
) {
const subscriptionId = author.submitAndWatchExtrinsic(this.client)(
[this.extrinsic],
[this.extrinsicHex],
listener,
);
return author.unwatchExtrinsic(this.client)(subscriptionId)
.zoned("ExtrinsicWatch");
}

get sent() {
return author.submitExtrinsic(this.client)(this.extrinsic)
.zoned("ExtrinsicSent");
return author.submitExtrinsic(this.client)(this.extrinsicHex);
}
}

// TODO: attach to extrinsics sent.finalized result once zones-level method addition implemented
export function extrinsicsDecoded<
Client extends Z.$<rpc.Client>,
Hexes extends Z.$<known.Hex[]>,
>(
client: Client,
hexes: Hexes,
) {
return Z
.ls($extrinsic(client), hexes)
.next(([$extrinsic, hexes]) => {
return hexes.map((hex) => $extrinsic.decode(U.hex.decode(hex)));
});
}

function $extrinsic<
Client extends Z.$<rpc.Client> = Z.$<rpc.Client>,
Rest extends [sign?: Z.$<M.Signer>] = [sign?: Z.$<M.Signer>],
>(client: Client, ...[sign]: Rest) {
const metadata_ = metadata(client)();
const deriveCodec_ = scale.deriveCodec(metadata_);
const addrPrefix = const_(client)("System", "SS58Prefix")
.access("value")
.as<number>();
return scale.$extrinsic(deriveCodec_, metadata_, sign!, addrPrefix);
}
Loading