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

Commit

Permalink
feat: fluent api for creating and sending extrinsics (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Aug 9, 2022
1 parent 7fde27c commit b114a23
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 142 deletions.
16 changes: 8 additions & 8 deletions effect/examples/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ const root = C.sendAndWatchExtrinsic({
};
},
createWatchHandler(stop) {
return (message) => {
if (typeof message.params.result === "string") {
console.log("Extrinsic", message.params.result);
return (event) => {
if (typeof event.params.result === "string") {
console.log("Extrinsic", event.params.result);
} else {
if (message.params.result.inBlock) {
console.log("Extrinsic in block", message.params.result.inBlock);
} else if (message.params.result.finalized) {
console.log("Extrinsic finalized as of", message.params.result.finalized);
if (event.params.result.inBlock) {
console.log("Extrinsic in block", event.params.result.inBlock);
} else if (event.params.result.finalized) {
console.log("Extrinsic finalized as of", event.params.result.finalized);
stop();
} else {
console.log("Misc", message.params.result);
console.log("Misc", event.params.result);
stop();
}
}
Expand Down
1 change: 1 addition & 0 deletions effect/std/submitAndWatchExtrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as U from "../../util/mod.ts";
import * as a from "../atoms/mod.ts";
import * as sys from "../sys/mod.ts";

export { type Config as SendAndWatchExtrinsicConfig };
type Config = knownRpc.Config<
string,
| "state_getMetadata"
Expand Down
47 changes: 47 additions & 0 deletions examples/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as C from "../mod.ts";
import * as t from "../test-util/mod.ts";
import * as U from "../util/mod.ts";

const config = await t.config();

const root = C
.chain(config)
.pallet("Balances")
.extrinsic("transfer")
.call({
value: 12345n,
dest: {
type: "Id",
value: t.bob.publicKey,
},
})
.signed({
type: "Id",
value: t.alice.publicKey,
}, (message) => {
return {
type: "Sr25519",
value: t.alice.sign(message),
};
})
.sendAndWatch((stop) => {
return (event) => {
if (typeof event.params.result === "string") {
console.log("Extrinsic", event.params.result);
} else {
if (event.params.result.inBlock) {
console.log("Extrinsic in block", event.params.result.inBlock);
} else if (event.params.result.finalized) {
console.log("Extrinsic finalized as of", event.params.result.finalized);
stop();
} else {
console.log("Misc", event.params.result);
stop();
}
}
};
});

U.throwIfError(await root.run());

config.close();
57 changes: 0 additions & 57 deletions fluent/Address.ts

This file was deleted.

41 changes: 0 additions & 41 deletions fluent/Addresses.ts

This file was deleted.

25 changes: 14 additions & 11 deletions fluent/Call.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { Address } from "./Address.ts";
import { Addresses } from "./Addresses.ts";
import * as M from "../frame_metadata/mod.ts";
import { NodeBase } from "./common.ts";
import { Extrinsic } from "./Extrinsic.ts";
import { Signed } from "./Signed.ts";

// TODO: constraint `Props` according to metadata
export class Call<
E extends Extrinsic = Extrinsic,
Props extends Record<string, unknown> = Record<string, unknown>,
Extra extends unknown[] = any[],
Additional extends unknown = any,
Args extends Record<string, unknown> = Record<string, any>,
> extends NodeBase<"Call"> {
chain;

constructor(
readonly extrinsic: E,
readonly props: Props,
readonly extra?: Extra,
readonly additional?: Additional,
readonly args: Args,
readonly options?: CallOptions,
) {
super();
this.chain = extrinsic.chain;
}

signed<From extends Address<Addresses<this["chain"]>> = Address<Addresses<this["chain"]>>>(
from: From,
sign: (message: Uint8Array) => Promise<Uint8Array>,
signed(
from: M.MultiAddress,
sign: M.SignExtrinsic,
): Signed<this> {
return new Signed(this, from, sign);
}

declare send: () => any;
}

export interface CallOptions {
checkpoint?: string;
mortality?: [period: bigint, phase: bigint];
tip?: bigint;
nonce?: number;
}
3 changes: 0 additions & 3 deletions fluent/Chain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Config } from "../config/mod.ts";
import { HashHexString } from "../util/mod.ts";
import { Addresses } from "./Addresses.ts";
import { Block } from "./Block.ts";
import { NodeBase } from "./common.ts";
import { Header } from "./Header.ts";
Expand All @@ -27,8 +26,6 @@ export class Chain<B extends Config = Config> extends NodeBase<"Chain"> {
return new Pallet(this, name);
}

address: Addresses<this> = new Addresses(this);

head(): Header<this> {
return new Header(this);
}
Expand Down
18 changes: 6 additions & 12 deletions fluent/Extrinsic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Call } from "./Call.ts";
import { Call, CallOptions } from "./Call.ts";
import { NodeBase } from "./common.ts";
import { Pallet } from "./Pallet.ts";

Expand All @@ -17,16 +17,10 @@ export class Extrinsic<
this.chain = pallet.chain;
}

// TODO: constraint
call<
Props extends Record<string, unknown>,
Extra extends unknown[],
Additional extends unknown,
>(
props: Props,
extra?: Extra,
additional?: Additional,
): Call<this, Props> {
return new Call(this, props, extra, additional);
call<Args extends Record<string, unknown>>(
args: Args,
callOptions?: CallOptions,
): Call<this, Args> {
return new Call(this, args, callOptions);
}
}
34 changes: 25 additions & 9 deletions fluent/Signed.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { Address } from "./Address.ts";
import { Addresses } from "./Addresses.ts";
import {
sendAndWatchExtrinsic,
SendAndWatchExtrinsicConfig,
} from "../effect/std/submitAndWatchExtrinsic.ts";
import * as M from "../frame_metadata/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { Call } from "./Call.ts";
import { NodeBase } from "./common.ts";

export class Signed<
C extends Call = Call,
From extends Address<Addresses<C["chain"]>> = Address<Addresses<C["chain"]>>,
> extends NodeBase<"Signed"> {
export class Signed<C extends Call = Call> extends NodeBase<"Signed"> {
chain;

constructor(
readonly call: C,
readonly from: From,
readonly sign: (message: Uint8Array) => Promise<Uint8Array>,
readonly from: M.MultiAddress,
readonly sign: M.SignExtrinsic,
) {
super();
this.chain = call.chain;
}

declare send: () => any;
sendAndWatch(
createWatchHandler: U.CreateWatchHandler<
rpc.NotifMessage<SendAndWatchExtrinsicConfig, "author_submitAndWatchExtrinsic">
>,
) {
return sendAndWatchExtrinsic({
config: this.chain.config as any,
palletName: this.call.extrinsic.pallet.name,
methodName: this.call.extrinsic.name,
args: this.call.args,
sender: this.from,
sign: this.sign,
createWatchHandler,
});
}
}
1 change: 0 additions & 1 deletion fluent/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./Address.ts";
export * from "./Block.ts";
export * from "./Call.ts";
export * from "./Chain.ts";
Expand Down

0 comments on commit b114a23

Please sign in to comment.