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

Commit

Permalink
feat: decoding extrinsics within blocks (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Jun 27, 2022
1 parent d800787 commit fdf9a56
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 3 additions & 1 deletion _deps/scale.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "https://deno.land/x/scale@v0.2.1/mod.ts";
export * from "https://raw.githubusercontent.com/paritytech/parity-scale-codec-ts/7b40cffaf930bf945b09b39a6a4196509d6cd262/mod.ts";
// TODO: replace line above with line below (with new version) upon release with bit sequence codec
// export * from "https://deno.land/x/scale@v0.2.1/mod.ts";
5 changes: 5 additions & 0 deletions core/Block.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { read } from "../runtime/mod.ts";
import { HashHexString } from "../util/mod.ts";
import { Chain } from "./Chain.ts";
import { NodeBase } from "./common.ts";
Expand All @@ -9,4 +10,8 @@ export class Block<C extends Chain = Chain> extends NodeBase<"Block"> {
) {
super("Block");
}

read(): Promise<unknown> {
return read(this);
}
}
5 changes: 5 additions & 0 deletions examples/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { polkadot } from "../known/mod.ts";

const result = await polkadot.block().read();

console.log({ result });
2 changes: 1 addition & 1 deletion frame_metadata/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function DeriveCodec(metadata: M.Metadata): DeriveCodec {
return $.compact;
},
BitSequence: () => {
return $.never as unknown as $.Codec<any>;
return $.bitSequence;
},
visit: (i) => {
if (cache[i]) {
Expand Down
6 changes: 3 additions & 3 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface ExtrinsicCodecProps {
sign: (value: Uint8Array) => Signature;
}

export function $extrinsic(args: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const { metadata, sign, deriveCodec, hashers: { Blake2_256 } } = args;
export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const { metadata, sign, deriveCodec, hashers: { Blake2_256 } } = props;
const { signedExtensions } = metadata.extrinsic;
const $sig = deriveCodec(findExtrinsicTypeParam("Signature")!);
const $address = deriveCodec(findExtrinsicTypeParam("Address")!);
Expand Down Expand Up @@ -97,7 +97,7 @@ export function $extrinsic(args: ExtrinsicCodecProps): $.Codec<Extrinsic> {
});

return $.createCodec({
_metadata: [$extrinsic, args],
_metadata: [$extrinsic, props],
_staticSize: $.nCompact._staticSize,
_encode(buffer, extrinsic) {
const encoded = $baseExtrinsic.encode(extrinsic);
Expand Down
28 changes: 27 additions & 1 deletion runtime/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as M from "../frame_metadata/mod.ts";
import * as U from "../util/mod.ts";
import { globalContext } from "./Context.ts";

export type ReadTarget = C.Entry | C.KeyPage | C.Metadata | C.Header;
export type ReadTarget = C.Entry | C.KeyPage | C.Metadata | C.Header | C.Block;

export async function read<Target extends ReadTarget = ReadTarget>(
target: Target,
Expand Down Expand Up @@ -78,5 +78,31 @@ export async function read<Target extends ReadTarget = ReadTarget>(
}
throw new Error(); // TODO
}
case "Block": {
const raw = await chain.rpcClient.call("chain_getBlock", [target.hash]);
if (raw.result) {
const { block, justifications } = raw.result;
const { extrinsics, header } = block;
const $extrinsic = M.$extrinsic({
deriveCodec: group.deriveCodec,
hashers: await Hashers(),
metadata: group.metadata,
sign: undefined!,
});
await chain.release();
return {
justifications,
block: {
header,
extrinsics: extrinsics.map((extrinsic) => {
const trimmed = extrinsic.substring(2);
const bytes = U.hex.decode(trimmed);
return $extrinsic.decode(bytes);
}),
},
};
}
throw new Error();
}
}
}

0 comments on commit fdf9a56

Please sign in to comment.