forked from across-protocol/relayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (64 loc) · 2.25 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
import minimist from "minimist";
import { CommonConfig } from "./src/common";
import { config, delay, getSigner, help, Logger, processCrash, usage, winston } from "./src/utils";
import { runRelayer } from "./src/relayer";
import { runDataworker } from "./src/dataworker";
import { runMonitor } from "./src/monitor";
import { runFinalizer } from "./src/finalizer";
let logger: winston.Logger;
export async function run(args: { [k: string]: boolean | string }): Promise<void> {
logger = Logger;
const config = new CommonConfig(process.env);
const cmds = {
dataworker: runDataworker,
finalizer: runFinalizer,
help: help,
monitor: runMonitor,
relayer: runRelayer,
};
// todo Make the mode of operation an operand, rather than an option.
// i.e. ts-node ./index.ts [options] <relayer|...>
const cmd = Object.keys(cmds).find((_cmd) => !!args[_cmd]);
if (cmd === "help") cmds[cmd](); // no return
else if (cmd === undefined) usage(""); // no return
else if (typeof args["wallet"] !== "string" || args["wallet"].length === 0) {
// todo: Update usage() to provide a hint that wallet is missing/malformed.
usage(""); // no return
} else {
do {
try {
// One global signer for use with a specific per-chain provider.
// todo: Support a void signer for monitor mode (only) if no wallet was supplied.
await cmds[cmd](logger, await getSigner());
} catch (error) {
// eslint-disable-next-line no-process-exit
if (await processCrash(logger, cmd, config.pollingDelay, error)) process.exit(1);
}
} while (config.pollingDelay !== 0);
}
}
if (require.main === module) {
config();
const opts = {
boolean: ["dataworker", "finalizer", "help", "monitor", "relayer"],
string: ["wallet", "keys"],
alias: { h: "help" },
unknown: usage,
};
const args = minimist(process.argv.slice(2), opts);
run(args)
.then(() => {
// eslint-disable-next-line no-process-exit
process.exit(0);
})
.catch(async (error) => {
logger.error({
at: "InfrastructureEntryPoint",
message: "There was an error in the main entry point!",
error,
notificationPath: "across-error",
});
await delay(5);
await run(args);
});
}