forked from KaffinPX/Kaspool
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
81 lines (60 loc) · 2.46 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
import { RpcClient, Encoding, Resolver } from "./wasm/kaspa";
import Treasury from "./src/treasury";
import Templates from "./src/stratum/templates";
import Stratum from "./src/stratum";
import Pool from "./src/pool";
import config from "./config/config.json";
import dotenv from 'dotenv';
import Monitoring from './src/monitoring'
import { PushMetrics } from "./src/prometheus";
import axios from 'axios';
import fs from 'fs';
import path from 'path';
import { ExitStatus, getParsedCommandLineOfConfigFile } from "typescript";
export let DEBUG = 0
if (process.env.DEBUG == "1") {
DEBUG = 1;
}
// Send config.json to API server
async function sendConfig() {
if (DEBUG) monitoring.debug(`Main: Trying to send config to kaspool-monitor`);
try {
const configPath = path.resolve('./config/config.json');
const configData = fs.readFileSync(configPath, 'utf-8');
const response = await axios.post('http://kaspool-monitor:9302/postconfig', {
config: JSON.parse(configData)
});
monitoring.log(`Main: Config sent to API server. Response status: ${response.status}`);
} catch (error) {
monitoring.error(`Main: Error sending config: ${error}`);
}
}
const monitoring = new Monitoring();
monitoring.log(`Main: Starting kaspool App`)
dotenv.config();
monitoring.log(`Main: network: ${config.network}`);
const rpc = new RpcClient({
resolver: new Resolver({
urls : config.node
}),
encoding: Encoding.Borsh,
networkId: config.network,
});
await rpc.connect();
monitoring.log(`Main: RPC connection started`)
const serverInfo = await rpc.getServerInfo();
if (!serverInfo.isSynced || !serverInfo.hasUtxoIndex) throw Error('Provided node is either not synchronized or lacks the UTXO index.');
const treasuryPrivateKey = process.env.TREASURY_PRIVATE_KEY;
if (!treasuryPrivateKey) {
throw new Error('Environment variable TREASURY_PRIVATE_KEY is not set.');
}
const kaspoolPshGw = process.env.PUSHGATEWAY;
if (!kaspoolPshGw) {
throw new Error('Environment variable PUSHGATEWAY is not set.');
}
export const metrics = new PushMetrics(kaspoolPshGw);
sendConfig();
const treasury = new Treasury(rpc, serverInfo.networkId, treasuryPrivateKey, config.treasury.fee);
const templates = new Templates(rpc, treasury.address, config.stratum.templates.cacheSize);
const stratum = new Stratum(templates, config.stratum.port, config.stratum.difficulty, kaspoolPshGw, treasury.address, config.stratum.sharesPerMinute);
const pool = new Pool(treasury, stratum, stratum.sharesManager);