This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
forked from scotchfinance/scrt-swap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
56 lines (44 loc) · 1.87 KB
/
server.js
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
require('dotenv-defaults').config();
require('dotenv-expand');
require('console-stamp')(console, '[HH:MM:ss.l]');
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
const myEnv = dotenv.config();
dotenvExpand(myEnv);
const Web3 = require('web3');
const { Operator } = require('./operator');
const { Leader } = require('./leader');
const { Db } = require('./common/db');
const { CliSwapClient } = require('./common/cli_swap_client');
const { sleep } = require('./common/utils');
const config = require('./common/config');
const logger = require('./common/logger');
const provider = new Web3.providers.HttpProvider(config.ethProviderUrl);
const db = new Db(config.db_url, config.dbName);
if (process.env.ROLE === 'operator' && !config.operatorUser) {
throw new Error('OPERATOR_USER env variable required');
}
const tokenSwapClient = new CliSwapClient(config.chainClient, config.fromAccount, config.multisigAddress, config.password);
(async () => {
await db.init();
await sleep(3000);
if (process.env.ROLE === 'operator') {
const operator = new Operator(tokenSwapClient, config.operatorUser, config.multisigAddress, db, provider, config.networkId,
config.nbConfirmations, config.fromBlock, config.pollingInterval);
await operator.run();
} else if (process.env.ROLE === 'leader') {
const leader = new Leader(tokenSwapClient, config.multisigAddress, db, provider, config.networkId,
config.fromBlock, config.pollingInterval, config.multisigThreshold, config.broadcastInterval);
(async () => {
await leader.broadcastSignedSwaps();
})();
await leader.run();
}
})().catch(async (e) => {
await db.teardown().catch(
(error) => {
logger.error(`Fatal error tearing down DB: ${error}`);
}
);
logger.error('Fatal error starting: ', e);
});