This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fluent api for creating and sending extrinsics (#181)
- Loading branch information
1 parent
7fde27c
commit b114a23
Showing
10 changed files
with
101 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters