-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.ts
179 lines (162 loc) · 5.72 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import "npm:@polkadot/api-augment";
import { ApiPromise, WsProvider } from "npm:@polkadot/api";
import { GearApi } from "npm:@gear-js/api";
import { Client } from "https://deno.land/x/subshell@0.2.45-4/client/mod.ts";
// import { VerboseSigner } from "https://deno.land/x/subshell@0.2.45-4/signer/mod.ts";
import { stringToU8a } from "npm:@polkadot/util";
const GEAR = !!Deno.env.get("GEAR");
const DEFAULT_PROVIDER = GEAR
? "wss://rpc.vara.network"
: "wss://rpc.polkadot.io";
const SESSION_ID = Deno.env.get("SESSION_ID") ?? "";
const PROVIDER = Deno.env.get("PROVIDER") ?? DEFAULT_PROVIDER;
const TYPES = JSON.parse(Deno.env.get("TYPES") ?? "null");
const HUB = Deno.env.get("HUB") ?? `ws://localhost:8000`;
function progInfo() {
/* print program info from package.json */
const info: { [key: string]: string } = {
// "⚙️ v8 version ": Deno.version.v8,
// "🇹 TypeScript version ": Deno.version.typescript,
"🦕 Deno": Deno.version.deno,
"📗 Wiki": GEAR
? "https://github.com/btwiuse/gear.sh/wiki"
: "https://wiki.subshell.xyz",
"🙋 Issues": GEAR
? "https://github.com/btwiuse/gear.sh/issues"
: "https://github.com/btwiuse/subshell/issues",
// "⛓️ RPC Pprvider": PROVIDER,
// "🪄 Custom types": JSON.stringify(TYPES) != '{}' ? 'Yes' : 'None',
// "📖 Runtime api reference": 'https://substrate.rs',
};
Object.keys(info)
.forEach((k) => {
const prop = k;
const value = info[k];
console.log(`%c${prop} %c${value}`, "color: blue", "color: green");
});
}
Deno.addSignalListener("SIGINT", () => {
console.log("interrupted!");
});
progInfo();
console.log();
// deno-lint-ignore no-explicit-any
function info(msg: any = "", prefix = " ") {
console.log(`%c${prefix} %c${msg}`, "color: yellow", "color: white");
}
info(`RPC Provider: ${PROVIDER}`, "🔗");
const customTypes = JSON.stringify(TYPES) != "{}" ? "Yes" : "None";
info(`Custom types: ${customTypes}`);
info("api is initializing. Please hold on...");
async function sleep(ms = 2000) {
await new Promise((resolve) => setTimeout(resolve, ms));
}
const wsProvider = new WsProvider(
PROVIDER,
);
let api;
if (GEAR) {
api = await GearApi.create({ provider: wsProvider, types: TYPES });
} else {
api = await ApiPromise.create({ provider: wsProvider, types: TYPES });
}
interface ISubshell {
extension?: Client;
showExamples(): Promise<void>;
}
const Subshell: ISubshell = {
showExamples: async () => {
info();
info(`Hello and welcome! Here are some Subshell basic examples to try:`);
info();
info("get chain name", "💁");
info("> api.runtimeChain.toHuman()");
info(api.runtimeChain.toHuman());
info();
await sleep();
info("get ss58 prefix", "💁");
info("> api.consts.system.ss58Prefix.toHuman()");
info(api.consts.system.ss58Prefix.toHuman());
info();
await sleep();
info("get current block", "💁");
info("> (await api.query.system.number()).toNumber()");
// deno-lint-ignore no-explicit-any
info((await api.query.system.number() as any).toNumber());
info();
await sleep();
info("get runtime metadata version", "💁");
info("> api.runtimeMetadata.version");
info(api.runtimeMetadata.version);
info();
await sleep();
info("get accounts in Polkadot.js extension wallet", "💁");
info("> await Subshell.extension?.web3Accounts()");
info(
(await Subshell.extension?.web3Accounts()) ||
" No injected account was found. ¯_(ツ)_/¯ \n You may want to install the Polkadot.js wallet extension and setup the keyring first.",
);
info();
await sleep();
info("sign a message with account.", "💁");
info(
"(You should see a signing request if you have the wallet extension installed and enabled. Feel free to click cancel)",
);
info("> await api.sign(ALICE, {data: 'Hello, Subshell!'})");
info(`(skipped. Try it by replacing ALICE with your own address)`);
info();
await sleep();
info("verify signature", "💁");
info(`> import { signatureVerify } from "@polkadot/util-crypto"`);
info(`> signatureVerify('Hello, Subshell!', Signature, ALICE)`);
info(
`(skipped. Try it by replacing Signature with output of previous example)`,
);
info();
await sleep();
info("sign and send a transaction.", "💁");
info(
"(You should see a signing request if you have the wallet extension installed and enabled. Feel free to click cancel)",
);
info(
"> await api.tx.system.remarkWithEvent('Hello, Subshell!').signAndSend(BOB)",
);
info(`(skipped. Try it by replacing BOB with your own address)`);
info();
await sleep();
info("For more information, visit https://wiki.subshell.xyz");
},
};
if (SESSION_ID) {
Subshell.extension = new Client(HUB, SESSION_ID);
}
info(`api has been injected into the global object.`);
if (Subshell.extension) {
info(`Connecting to the Polkadot.js browser wallet extension...`);
const accounts = await Subshell.extension?.web3Accounts();
if (accounts.length > 0) {
api.setSigner(Subshell.extension);
info(`Polkadot.js extension signer bridge has been established.`, "✨");
} else {
info(`Polkadot.js extension is not properly setup.`, `🔴`);
info(
`Please install, enable, and add to it at least one keypair to explore all features of Subshell.`,
);
}
}
info();
info(
`Now you can start exploring the Polkadot.js api in this Deno repl environment! For example:`,
"💡",
);
info();
info(`check if the api is indeed connected:`, "💁");
info();
info(` > api.isConnected`);
info(` ${api.isConnected}`);
info();
info(
`If it is your first time using Subshell, click the ❔ icon at the top-right corner of the screen`,
`👉`,
);
console.log();