-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.ts
executable file
·341 lines (323 loc) · 9.5 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env node
import { spawn } from "node:child_process";
import type { SignerOptions } from "@polkadot/api/types";
import type { KeyringPair } from "@polkadot/keyring/types";
import type { BN } from "@polkadot/util";
import {
type ApiPromise,
formatNumberToBalance,
getKeyringFromSeed,
initialize,
isValidAddress,
} from "avail-js-sdk";
import { Argument, Command, Option } from "commander";
enum NetworkNames {
Mainnet = "mainnet",
Turing = "turing",
Local = "local",
}
enum Wait {
Yes = "yes",
No = "no",
Final = "final",
}
const RPC_URLS: { mainnet: string; turing: string; local: string } = {
mainnet: "wss://mainnet-rpc.avail.so/ws",
turing: "wss://turing-rpc.avail.so/ws",
local: "wss://127.0.0.1:9944/ws",
};
const EXPLORER_URLS: { mainnet: string; turing: string; local: string } = {
mainnet:
"https://explorer.availproject.org/?rpc=wss://mainnet-rpc.avail.so/ws&light=https://api.lightclient.mainnet.avail.so/v1",
turing:
"https://explorer.availproject.org/?rpc=wss://turing-rpc.avail.so/ws&light=https://api.lightclient.turing.avail.so/v1",
local: "https://explorer.availproject.org/?rpc=wss://127.0.0.1:9944/ws",
};
const program = new Command();
program
.name("avail")
.description("A simple CLI for Avail network utilities")
.version("0.1.15");
const sendTransferTx = async (
api: ApiPromise,
to: string,
amount: BN,
keyring: KeyringPair,
opt: Partial<SignerOptions>,
network: NetworkNames,
wait: Wait,
): Promise<void> => {
return await new Promise((resolve, reject) => {
api.tx.balances
.transfer(to, amount)
.signAndSend(keyring, opt, (result) => {
if (wait === Wait.Yes && result.status.isInBlock) {
console.log(
`✅ Transfer included at block hash: ${String(result.status.asInBlock)}`,
);
if (typeof network !== "undefined") {
console.log(
`🧭 Link to explorer: ${EXPLORER_URLS[network]}/#/explorer/query/${String(result.status.asInBlock)}`,
);
}
resolve();
} else if (wait === Wait.Final && result.status.isFinalized) {
console.log(
`✅ Transfer finalized at block hash: ${String(result.status.asFinalized)}`,
);
if (typeof network !== "undefined") {
console.log(
`🧭 Link to explorer: https://${EXPLORER_URLS[network]}/#/explorer/query/${String(result.status.asFinalized)}`,
);
}
resolve();
}
})
.catch((error: string) => {
reject(new Error(`❌ Transaction failed: ${error}`));
});
});
};
const transfer = async (
to: string,
value: number,
options: {
seed: string;
network: NetworkNames;
rpc: string;
wait: Wait;
},
): Promise<void> => {
try {
if (!isValidAddress(to))
throw new Error(`${to} recipient address is invalid`);
const seed = options.seed;
let rpcUrl: string;
if (typeof RPC_URLS[options.network] === "undefined") {
rpcUrl = options.rpc;
} else {
rpcUrl = RPC_URLS[options.network];
}
const tempConsoleWarn = console.warn;
console.warn = () => {};
const api: ApiPromise = await initialize(rpcUrl, { noInitWarn: true });
console.warn = tempConsoleWarn;
const keyring = getKeyringFromSeed(seed);
const amount = formatNumberToBalance(value);
const opt: Partial<{ nonce: number }> = { nonce: -1 };
if (options.wait !== Wait.No) {
await sendTransferTx(
api,
to,
amount,
keyring,
opt,
options.network,
options.wait,
);
} else {
await api.tx.balances.transfer(to, amount).signAndSend(keyring, opt);
}
console.log(`✅ ${value} AVL successfully sent to ${to}`);
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
};
const sendBlobTx = async (
api: ApiPromise,
blob: string,
keyring: KeyringPair,
opt: Partial<SignerOptions>,
network: NetworkNames,
wait: Wait,
): Promise<void> => {
return await new Promise((resolve, reject) => {
api.tx.dataAvailability
.submitData(blob)
.signAndSend(keyring, opt, (result) => {
if (wait === Wait.Yes && result.status.isInBlock) {
console.log(
`✅ Blob included at block hash: ${String(result.status.asInBlock)}`,
);
if (typeof network !== "undefined") {
console.log(
`🧭 Link to explorer: ${EXPLORER_URLS[network]}/#/explorer/query/${String(result.status.asInBlock)}`,
);
}
resolve();
} else if (wait === Wait.Final && result.status.isFinalized) {
console.log(
`✅ Blob finalized at block hash: ${String(result.status.asFinalized)}`,
);
if (typeof network !== "undefined") {
console.log(
`🧭 Link to explorer: ${EXPLORER_URLS[network]}/#/explorer/query/${String(result.status.asFinalized)}`,
);
}
resolve();
}
})
.catch((error: string) => {
reject(new Error(`❌ Transaction failed: ${error}`));
});
});
};
async function data(
blob: string,
options: {
seed: string;
network: NetworkNames;
rpc: string;
appId: number;
wait: Wait;
},
): Promise<void> {
try {
const seed = options.seed;
let rpcUrl: string;
if (typeof RPC_URLS[options.network] === "undefined") {
rpcUrl = options.rpc;
} else {
rpcUrl = RPC_URLS[options.network];
}
const tempConsoleWarn = console.warn;
console.warn = () => {};
const api = await initialize(rpcUrl, { noInitWarn: true });
console.warn = tempConsoleWarn;
const keyring = getKeyringFromSeed(seed);
const opt: Partial<{ app_id: number; nonce: number }> = {
app_id: options.appId,
nonce: -1,
};
if (options.wait !== Wait.No) {
await sendBlobTx(api, blob, keyring, opt, options.network, options.wait);
} else {
await api.tx.dataAvailability.submitData(blob).signAndSend(keyring, opt);
}
console.log("✅ Data blob sent to Avail");
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
}
const lc = async (options: {
network: NetworkNames;
config: string;
identity: string;
noUpgrade: boolean;
}): Promise<void> => {
try {
let cmd = "curl -sL1 avail.sh | bash -s --";
if (typeof options.config !== "undefined") {
cmd = cmd.concat(` --config ${options.config}`);
} else {
cmd = cmd.concat(` --network ${options.network}`);
}
if (typeof options.identity !== "undefined") {
cmd = cmd.concat(` --identity ${options.identity}`);
}
if (!options.noUpgrade) {
cmd = cmd.concat(" --upgrade y");
}
const child = spawn(cmd, {
cwd: process.cwd(),
shell: true,
stdio: "inherit",
});
child.on("close", (code: number) => {
process.exit(code);
});
child.on("exit", (code: number) => {
process.exit(code);
});
} catch (err) {
console.error(err);
process.exit(1);
}
};
program
.command("transfer")
.description("Transfer AVL token to another account")
.addOption(
new Option("-n, --network <network name>", "network name")
.choices(["mainnet", "turing", "local"])
.default("turing")
.conflicts("rpc"),
)
.addOption(
new Option("-r, --rpc <RPC url>", "the RPC url to connect to")
.env("AVAIL_RPC_URL")
.default(RPC_URLS.turing),
)
.addOption(
new Option(
"-s, --seed <seed phrase>",
"the seed phrase for the Avail account",
)
.env("AVAIL_SEED")
.makeOptionMandatory(),
)
.addOption(
new Option("-w, --wait <status>", "wait for extrinsic inclusion")
.choices(["yes", "no", "final"])
.default("yes"),
)
.argument("<to>", "the recipient address")
.argument("<value>", "the amount of AVL (10e18 units) to transfer")
.action(transfer);
program
.command("data")
.description("Utilities to operate with data on Avail network")
.command("submit")
.description("Submit a data blob to an Avail network")
.addOption(
new Option("-n, --network <network name>", "network name")
.choices(["mainnet", "turing", "local"])
.default("turing")
.conflicts("rpc"),
)
.addOption(
new Option("-r, --rpc <RPC url>", "the RPC url to connect to")
.env("AVAIL_RPC_URL")
.default(RPC_URLS.turing),
)
.addOption(
new Option(
"-s, --seed <seed phrase>",
"the seed phrase for the Avail account",
)
.env("AVAIL_SEED")
.makeOptionMandatory(),
)
.addOption(
new Option(
"-a, --app-id <app ID>",
"the blob will be submitted with this app ID",
).default(0),
)
.addOption(
new Option("-w, --wait <status>", "wait for extrinsic inclusion")
.choices(["yes", "no", "final"])
.default("yes"),
)
.addArgument(new Argument("<blob>", "the data blob to submit"))
.action(data);
program
.command("lc")
.description("Utilities to operate an Avail light client")
.command("up")
.description("Spawns a new Avail light client or runs an existing one")
.addOption(
new Option("-n, --network <network name>", "network name")
.choices(["mainnet", "turing", "local"])
.default("turing")
.makeOptionMandatory(),
)
.option("-i, --identity <identity>", "the identity to use")
.option("-c, --config <path to config file>", "the config file to use")
.option("-nu, --no-upgrade", "do not upgrade the version of the light client")
.action(lc);
program.parse();