forked from Colm3na/polkastats-backend-v3
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
110 lines (91 loc) · 2.21 KB
/
index.ts
File metadata and controls
110 lines (91 loc) · 2.21 KB
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
import { Sequelize } from 'sequelize';
import crawlers from './crawlers/crawlers';
import { wait } from './utils/utils';
import {
wsProviderUrl,
typeProvider,
dbConnect,
} from './config/config';
import { Logger } from './utils/logger';
import BlockExplorer from './blockexplorer';
import runtimeTypes from './config/runtime_types.json';
import { ProviderFactory } from './lib/providerAPI/providerAPI';
import { startServer } from './prometheus';
const log = new Logger();
async function getSequlize(sConnect) {
const db = new Sequelize(
sConnect,
{
logging: false,
pool: {
max: 60,
min: 0,
acquire: 120000,
idle: 100000,
},
},
);
try {
await db.authenticate();
} catch (error) {
log.error(error);
}
return db;
}
async function getPolkadotAPI(wsUrl, rtt) {
log.info(`Connecting to ${wsUrl}`);
const provider = new ProviderFactory(wsUrl, typeProvider);
const api = await provider.getApi(rtt);
const sdk = await provider.getSdk();
api.on('error', async (value) => {
log.error(value);
});
api.on('disconnected', async (value) => {
log.error(value);
});
await api.isReady;
log.info('API is ready!');
// Wait for node is synced
let node;
try {
node = await api.rpc.system.health();
} catch (e) {
log.error({
message: "Can't connect to node! Waiting 10s...",
name: 'disconnect',
stack: e.stack,
});
api.disconnect();
await wait(10000);
throw e;
}
log.info(`Node: ${JSON.stringify(node)}`);
if (node && node.isSyncing.eq(false)) {
// Node is synced!
log.info('Node is synced!');
} else {
log.default('Node is not synced! Waiting 10s...');
api.disconnect();
await wait(10000);
}
return { api, sdk };
}
async function main() {
const sequelize = await getSequlize(dbConnect);
const { api, sdk } = await getPolkadotAPI(wsProviderUrl, runtimeTypes);
const blockExplorer = new BlockExplorer(
api,
sdk,
sequelize,
crawlers
);
await blockExplorer.run();
startServer(() => {
log.info('Server running...');
});
}
main().catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(-1);
});