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

chore: add batch example #354

Merged
merged 1 commit into from
Nov 2, 2022
Merged
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
66 changes: 66 additions & 0 deletions examples/batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as C from "../mod.ts";
import * as T from "../test_util/mod.ts";
import * as U from "../util/mod.ts";

const recipients = [T.bob, T.charlie, T.dave, T.eve];
const balances = C.ls(
...recipients.map(({ publicKey }) => {
return C.entryRead(T.westend, "System", "Account", [publicKey])
.access("value")
.access("data")
.access("free")
.as<bigint>();
}),
);

const extrinsic = new C.Extrinsic(T.westend, {
sender: {
type: "Id",
value: T.alice.publicKey,
},
palletName: "Utility",
methodName: "batch_all",
args: {
calls: recipients.map(({ publicKey }) => ({
type: "Balances",
value: {
type: "transfer",
dest: {
type: "Id",
value: publicKey,
},
value: 12345n,
},
})),
},
})
.signed((message) => ({
type: "Sr25519",
value: T.alice.sign(message),
}))
.watch((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();
}
}
};
});

const initialBalances = U.throwIfError(await C.run(balances));
U.throwIfError(await C.run(extrinsic));
const balancesAfterExtrinsicFinalized = U.throwIfError(await C.run(balances));

console.log({
initialBalances,
balancesAfterExtrinsicFinalized,
});