-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.js
119 lines (84 loc) · 3.58 KB
/
start.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
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
const conf = require('ocore/conf.js');
const network = require('ocore/network.js');
const eventBus = require('ocore/event_bus.js');
const lightWallet = require('ocore/light_wallet.js');
const wallet_general = require('ocore/wallet_general.js');
const db = require('ocore/db.js');
const dag = require('aabot/dag.js');
const clc = require("cli-color");
const operator = require('aabot/operator.js');
const marketDB = require('./db');
const { justsayingHandler, responseHandler } = require('./handlers');
const webserver = require('./webserver');
const { sportDataService } = require('./SportData');
const { wait } = require('./utils/wait');
const ResultCommitter = require('./resultCommitter');
lightWallet.setLightVendorHost(conf.hub);
eventBus.once('connected', function (ws) {
network.initWitnessesIfNecessary(ws, start);
});
async function addWatchedAas() {
console.log('addWatchedAas');
conf.factoryAas.map((address) => wallet_general.addWatchedAddress(address, null, console.log));
network.addLightWatchedAa(conf.tokenRegistryAaAddress, null, console.log)
};
async function watchMarketAa(objAa) {
return new Promise(async function (resolve) {
wallet_general.addWatchedAddress(objAa.address, resolve);
});
}
async function discoverMarketAas() {
let factoryStateVars = {};
const stateVarsGetter = conf.factoryAas.map((aa) => dag.readAAStateVars(aa).then((stateVars) => Object.assign(factoryStateVars, { [aa]: stateVars })));
await Promise.all(stateVarsGetter);
const allMarkets = [];
Object.entries(factoryStateVars).forEach(([factory, stateVars]) => {
Object.keys(stateVars).forEach((key) => {
const { created_at } = stateVars[key];
const marketAddress = key.replace("prediction_", "");
if ((factory === conf.factoryAas[0] && created_at <= conf.factoryUpgradeFixQuietPeriodTimestamp) || (factory === conf.factoryAas[1] && created_at <= conf.factoryUpgradeRemoveIssueFeeForLiqTimestamp) || factory === conf.factoryAas[2]) {
allMarkets.push(marketAddress);
} else {
console.error('ignore ', marketAddress);
}
})
})
const rows = await db.query("SELECT aa_address FROM markets");
const knownAaAddresses = rows.map(obj => obj.aa_address);
const newMarketsAas = allMarkets.filter(address => !knownAaAddresses.includes(address));
await Promise.all(newMarketsAas.map((aa) => watchMarketAa(aa)));
}
async function start() {
await marketDB.create();
await wait(60 * 1000);
await sportDataService.init();
addWatchedAas();
eventBus.on('connected', addWatchedAas);
lightWallet.refreshLightClientHistory();
await lightWallet.waitUntilHistoryRefreshDone();
await dag.loadAA(conf.tokenRegistryAaAddress);
await discoverMarketAas()
await marketDB.api.refreshSymbols();
webserver.start();
console.error('webserver has been started');
if (conf.enableCommitter) {
const committer = new ResultCommitter();
await committer.init();
}
if (conf.automaticSymbolsReg) {
await operator.start();
const address = await operator.getAddress();
if (!address) throw "no address, please check conf.js";
const balance = await dag.readBalance(address);
const reserve_balance = balance?.base?.total || 0;
if (reserve_balance < 1e9) {
throw clc.red.bold('Your wallet contains less than 1 GBYTE.')
}
if (!conf.admin_email || !conf.from_email) {
throw clc.red.bold('please specify admin_email and from_email in your conf.json');
}
}
}
eventBus.on('aa_response', responseHandler);
eventBus.on("message_for_light", justsayingHandler);
process.on('unhandledRejection', up => { throw up });