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

feat: rework rpc typings #233

Merged
merged 9 commits into from
Oct 22, 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
15 changes: 1 addition & 14 deletions config/mod.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import * as rpc from "../rpc/mod.ts";

export class Config<
DiscoveryValue = any,
RpcCallMethods extends rpc.ProviderMethods = rpc.ProviderMethods,
RpcSubscriptionMethods extends rpc.ProviderMethods = rpc.ProviderMethods,
RpcErrorDetails extends rpc.ErrorDetails = rpc.ErrorDetails,
> {
// TODO: get rid of this gunk
RpcMethods!: RpcCallMethods & RpcSubscriptionMethods;
RpcCallMethods!: RpcCallMethods;
RpcSubscriptionMethods!: RpcSubscriptionMethods;
RpcErrorDetails!: RpcErrorDetails;

export class Config<DiscoveryValue = any> {
#discoveryValue?: DiscoveryValue | Promise<DiscoveryValue>;

constructor(
Expand Down
8 changes: 4 additions & 4 deletions effect/BlockRead.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as known from "../known/mod.ts";
import * as U from "../util/mod.ts";
import { $extrinsic } from "./core/$extrinsic.ts";
import { deriveCodec } from "./core/deriveCodec.ts";
import { Metadata } from "./Metadata.ts";
import { RpcCall } from "./RpcCall.ts";

export class BlockRead<Rest extends [blockHash?: Z.$<U.HashHexString | undefined>]> extends Z.Name {
export class BlockRead<Rest extends [blockHash?: Z.$<U.HexHash | undefined>]> extends Z.Name {
root;

constructor(
config: known.rpc.Config<string, "state_getMetadata" | "chain_getBlock">,
config: Config,
...[blockHash]: [...Rest]
) {
super();
Expand All @@ -23,7 +23,7 @@ export class BlockRead<Rest extends [blockHash?: Z.$<U.HashHexString | undefined
justifications,
block: {
header,
extrinsics: extrinsics.map((extrinsic) => {
extrinsics: extrinsics.map((extrinsic: U.Hex) => {
return $extrinsic_.decode(U.hex.decode(extrinsic));
}),
},
Expand Down
12 changes: 4 additions & 8 deletions effect/BlockWatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import { Extrinsic } from "../frame_metadata/mod.ts";
import * as known from "../known/mod.ts";
import * as U from "../util/mod.ts";
import { BlockRead } from "./BlockRead.ts";
Expand All @@ -11,12 +11,8 @@ export class BlockWatch extends Z.Name {
root;

constructor(
config: known.rpc.Config<
string,
"state_getMetadata" | "chain_getBlockHash" | "chain_getBlock" | "chain_unsubscribeNewHead",
"chain_subscribeNewHeads"
>,
createWatchHandler: U.CreateWatchHandler<known.types.Block<Extrinsic>>,
config: Config,
createWatchHandler: U.CreateWatchHandler<known.SignedBlock>,
) {
super();
this.root = new RpcSubscription(
Expand All @@ -26,7 +22,7 @@ export class BlockWatch extends Z.Name {
function subscribeNewHeadsHandler(stop) {
const watchHandler = createWatchHandler(stop);
return async (result) => {
const blockNum = result.params.result.number;
const blockNum: number = result.params.result.number;
const blockHash = Z.sel(new RpcCall(config, "chain_getBlockHash", [blockNum]), "result");
// TODO: use derived util from Zones
const block = U.throwIfError(await run(new BlockRead(config, blockHash)));
Expand Down
6 changes: 3 additions & 3 deletions effect/EntryRead.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as known from "../known/mod.ts";
import * as U from "../util/mod.ts";
import { $storageKey } from "./core/$storageKey.ts";
import { codec } from "./core/codec.ts";
Expand All @@ -13,12 +13,12 @@ export class EntryRead<
PalletName extends Z.$<string>,
EntryName extends Z.$<string>,
Keys extends unknown[],
Rest extends [blockHash?: Z.$<U.HashHexString | undefined>],
Rest extends [blockHash?: Z.$<U.HexHash | undefined>],
> extends Z.Name {
root;

constructor(
config: known.rpc.Config<string, "state_getMetadata" | "state_getStorage">,
config: Config,
palletName: PalletName,
entryName: EntryName,
keys: [...Keys],
Expand Down
14 changes: 4 additions & 10 deletions effect/EntryWatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../config/mod.ts";
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 "./core/$storageKey.ts";
Expand All @@ -10,13 +10,7 @@ import { entryMetadata, Metadata, palletMetadata } from "./Metadata.ts";
import { RpcCall } from "./RpcCall.ts";
import { RpcSubscription } from "./RpcSubscription.ts";

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

type Config = known.rpc.Config<
string,
"state_getMetadata" | "state_unsubscribeStorage",
"state_subscribeStorage"
>;
export type WatchEntryEvent = [key?: U.Hex, value?: unknown];

export class EntryWatch<
PalletName extends Z.$<string>,
Expand Down Expand Up @@ -49,8 +43,8 @@ export class EntryWatch<
const watchInit = Z.call($entry, function entryWatchInit($entry) {
return U.mapCreateWatchHandler(
createWatchHandler,
(message: rpc.NotifMessage<Config, "state_subscribeStorage">) => {
return message.params.result.changes.map(([key, val]) => {
(message: rpc.NotifMessage) => {
return message.params.result.changes.map(([key, val]: any) => {
return <WatchEntryEvent> [key, val ? $entry.decode(U.hex.decode(val)) : undefined];
});
},
Expand Down
24 changes: 5 additions & 19 deletions effect/ExtrinsicSentWatch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unimplemented } from "../deps/std/testing/asserts.ts";
import * as Z from "../deps/zones.ts";
import * as M from "../frame_metadata/mod.ts";
import * as known from "../known/mod.ts";
import { Config } from "../mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as ss58 from "../ss58/mod.ts";
import * as U from "../util/mod.ts";
Expand All @@ -12,31 +12,17 @@ import { Metadata } from "./Metadata.ts";
import { RpcCall } from "./RpcCall.ts";
import { RpcSubscription } from "./RpcSubscription.ts";

export { type Config as SendAndWatchExtrinsicConfig };
type Config = known.rpc.Config<
string,
| "state_getMetadata"
| "state_getRuntimeVersion"
| "chain_getBlockHash"
| "system_accountNextIndex"
| "system_chain"
| "author_unwatchExtrinsic",
"author_submitAndWatchExtrinsic"
>;

export interface SendAndWatchExtrinsicProps {
sender: M.MultiAddress;
palletName: string;
methodName: string;
args: Record<string, unknown>;
checkpoint?: U.HashHexString;
checkpoint?: U.HexHash;
mortality?: [period: bigint, phase: bigint];
nonce?: string;
tip?: bigint;
sign: M.SignExtrinsic;
createWatchHandler: U.CreateWatchHandler<
rpc.NotifMessage<Config, "author_submitAndWatchExtrinsic">
>;
createWatchHandler: U.CreateWatchHandler<rpc.NotifMessage>;
}

export class ExtrinsicSentWatch<Props extends Z.Rec$<SendAndWatchExtrinsicProps>> extends Z.Name {
Expand All @@ -63,7 +49,7 @@ export class ExtrinsicSentWatch<Props extends Z.Rec$<SendAndWatchExtrinsicProps>
unimplemented();
}
}
})() as U.AccountIdString;
})();
});
const accountNextIndex = new RpcCall(config, "system_accountNextIndex", [senderSs58]);
const genesisHash = hexDecode(Z.sel(new RpcCall(config, "chain_getBlockHash", [0]), "result"));
Expand Down Expand Up @@ -119,7 +105,7 @@ export class ExtrinsicSentWatch<Props extends Z.Rec$<SendAndWatchExtrinsicProps>
additional: [specVersion, transactionVersion, checkpoint, genesisHash],
},
});
return U.hex.encode(extrinsicBytes) as U.HexString;
return U.hex.encode(extrinsicBytes);
},
);
this.root = new RpcSubscription(
Expand Down
8 changes: 4 additions & 4 deletions effect/KeyPageRead.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as known from "../known/mod.ts";
import * as U from "../util/mod.ts";
import { $key } from "./core/$key.ts";
import { $storageKey } from "./core/$storageKey.ts";
Expand All @@ -12,12 +12,12 @@ export class KeyPageRead<
PalletName extends Z.$<string>,
EntryName extends Z.$<string>,
Count extends Z.$<number>,
Rest extends [start?: unknown[] | undefined, blockHash?: Z.$<U.HashHexString | undefined>],
Rest extends [start?: unknown[] | undefined, blockHash?: Z.$<U.HexHash | undefined>],
> extends Z.Name {
root;

constructor(
config: known.rpc.Config<string, "state_getMetadata" | "state_getKeysPaged">,
config: Config,
palletName: PalletName,
entryName: EntryName,
count: Count,
Expand Down Expand Up @@ -50,7 +50,7 @@ export class KeyPageRead<
const keysDecoded = Z.call(
Z.ls($key_, keysEncoded),
function keysDecodedImpl([$key, keysEncoded]) {
return keysEncoded.map((keyEncoded) => {
return keysEncoded.map((keyEncoded: U.Hex) => {
return $key.decode(U.hex.decode(keyEncoded));
});
},
Expand Down
6 changes: 3 additions & 3 deletions effect/Metadata.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as Z from "../deps/zones.ts";
import * as M from "../frame_metadata/mod.ts";
import * as known from "../known/mod.ts";
import { Config } from "../mod.ts";
import * as U from "../util/mod.ts";
import { RpcCall } from "./RpcCall.ts";

export class Metadata<Rest extends [blockHash?: Z.$<U.HashHexString | undefined>]> extends Z.Name {
export class Metadata<Rest extends [blockHash?: Z.$<U.HexHash | undefined>]> extends Z.Name {
root;

constructor(config: known.rpc.Config<string, "state_getMetadata">, ...[blockHash]: [...Rest]) {
constructor(config: Config, ...[blockHash]: [...Rest]) {
super();
this.root = Z.call(
new RpcCall(config, "state_getMetadata", [blockHash]),
Expand Down
10 changes: 4 additions & 6 deletions effect/RpcCall.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import { RpcError } from "./common.ts";
import { rpcClient } from "./core/rpcClient.ts";

export class RpcCall<
Methods extends rpc.ProviderMethods,
MethodName extends Z.$<Extract<keyof Methods, string>>,
Params extends Z.Ls$<Parameters<Methods[Z.T<MethodName>]>>,
MethodName extends Z.$<string>,
Params extends Z.Ls$<unknown[]>,
> extends Z.Name {
root;

constructor(
config: Config<string, Methods>,
config: Config,
methodName: MethodName,
params: [...Params],
) {
Expand All @@ -24,7 +22,7 @@ export class RpcCall<
async function rpcCallImpl([[client, methodName, ...params], rc]) {
const result = await client.call(
methodName,
params as Parameters<(Methods & rpc.ProviderMethods)[Z.T<MethodName>]>,
params,
);
if (result.error) {
return new RpcError({
Expand Down
29 changes: 13 additions & 16 deletions effect/RpcSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import { rpcClient } from "./core/rpcClient.ts";
import { run } from "./run.ts";

export class RpcSubscription<
Config_ extends Config,
MethodName extends Extract<keyof Config_["RpcSubscriptionMethods"], string>,
MethodName_ extends Z.$<MethodName>,
Params extends Parameters<Config_["RpcSubscriptionMethods"][Z.T<MethodName_>]>,
Params_ extends Z.Ls$<Params>,
CreateListenerCb extends Z.$<U.CreateWatchHandler<rpc.NotifMessage<Config_, MethodName>>>,
MethodName extends Z.$<string>,
Params extends Z.Ls$<unknown[]>,
CreateListenerCb extends Z.$<U.CreateWatchHandler<rpc.NotifMessage>>,
> extends Z.Name {
root;

constructor(
config: Config_,
methodName: MethodName_,
params: [...Params_],
config: Config,
methodName: MethodName,
params: [...Params],
createListenerCb: CreateListenerCb,
cleanup?: (initOk: rpc.OkMessage<Config_, MethodName>) => Z.EffectLike,
cleanup?: (initOk: rpc.OkMessage) => Z.EffectLike,
) {
super();
const client = rpcClient(config);
Expand All @@ -30,10 +27,10 @@ export class RpcSubscription<
Z.ls(deps, Z.rc(client, deps)),
async function rpcSubscriptionImpl([[client, methodName, createListenerCb, ...params], rc]) {
const result = await client.subscribe(
methodName as MethodName,
params as Parameters<Config_["RpcSubscriptionMethods"][MethodName]>,
createListenerCb as any,
cleanup ? (x) => run(cleanup(x.result), undefined!) : undefined,
methodName,
params,
createListenerCb,
cleanup ? (x) => run(cleanup(x), undefined!) : undefined,
);
if (result?.error) {
return new RpcError({
Expand All @@ -56,8 +53,8 @@ export class RpcSubscription<
}

// TODO: handle elsewhere
export class RpcSubscriptionError<Config_ extends Config> extends U.ErrorCtor("RpcSubscription") {
constructor(readonly error: rpc.ErrMessage<Config_>["error"]) {
export class RpcSubscriptionError extends U.ErrorCtor("RpcSubscription") {
constructor(readonly error: rpc.ErrMessage["error"]) {
super();
}
}
13 changes: 4 additions & 9 deletions effect/common.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { Config } from "../config/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";

// TODO: handle this elsewhere
export class RpcError<
Config_ extends Config,
MethodName extends keyof Config_["RpcMethods"],
Params extends Parameters<Config_["RpcMethods"][MethodName]>,
> extends U.ErrorCtor("RpcCall") {
export class RpcError extends U.ErrorCtor("RpcCall") {
code;
attempt;

constructor(
{ code, message, attempt }: rpc.ErrMessage<Config_>["error"] & {
{ code, message, attempt }: rpc.ErrMessage["error"] & {
attempt: {
methodName: MethodName;
params: Params;
methodName: string;
params: unknown[];
};
},
) {
Expand Down
2 changes: 1 addition & 1 deletion effect/core/decoded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as U from "../../util/mod.ts";

export function decoded<
Codec extends Z.$<$.Codec<unknown>>,
Encoded extends Z.$<U.HexString>,
Encoded extends Z.$<U.Hex>,
Key extends Z.$<string>,
>(
codec: Codec,
Expand Down
2 changes: 1 addition & 1 deletion effect/core/storageKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const storageKey = Z.call.fac((
$storageKey: $.Codec<unknown[]>,
...keys: unknown[]
) => {
return U.hex.encode($storageKey.encode(keys)) as U.HexString;
return U.hex.encode($storageKey.encode(keys)) as U.Hex;
});
2 changes: 1 addition & 1 deletion effect/extrinsic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Deno.test({
methodName: "propose",
args: {
proposal_hash: U.hex.decode(
"0x123450000000000000000000000000000000000000000000000000000000000",
"0x123450000000000000000000000000000000000000000000000000000000000" as U.Hex,
),
value: 2000000000000n,
},
Expand Down
4 changes: 2 additions & 2 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as $ from "../deps/scale.ts";
import { assert } from "../deps/std/testing/asserts.ts";
import * as H from "../hashers/mod.ts";
import * as ss58 from "../ss58/mod.ts";
import { hex } from "../util/mod.ts";
import { Hex, hex } from "../util/mod.ts";
import { $null, DeriveCodec } from "./Codec.ts";
import { Metadata } from "./Metadata.ts";

Expand Down Expand Up @@ -116,7 +116,7 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const signer = props.sign;
buffer.writeAsync(0, async (buffer) => {
const { signature } = await signer.signPayload(payload);
buffer.insertArray(hex.decode(signature));
buffer.insertArray(hex.decode(signature as Hex));
});
buffer.insertArray(extraEncoded);
buffer.insertArray(callEncoded);
Expand Down
Loading