forked from coinchimp/kaspool-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
96 lines (76 loc) · 2.88 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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";
function shutdown() {
console.log("\n\n Gracefully shutting down the pool")
process.exit();
}
process.on('SIGINT', shutdown);
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 katpool-monitor`);
try {
const configPath = path.resolve('./config/config.json');
const configData = fs.readFileSync(configPath, 'utf-8');
const katpoolMonitor = process.env.MONITOR;
if (!katpoolMonitor) {
throw new Error('Environment variable MONITOR is not set.');
}
const response = await axios.post(`${katpoolMonitor}/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 katpool App`)
dotenv.config();
monitoring.log(`Main: network: ${config.network}`);
const rpc = new RpcClient({
url: "localhost:17210",
// resolver: new Resolver(
// {
// urls : ["http://localhost:16210/"],
// }
// ),
encoding: Encoding.Borsh,
networkId: config.network,
});
try{
await rpc.connect();
} catch(err) {
monitoring.error(`Error while connecting to rpc url : ${rpc.url}`)
}
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 katpoolPshGw = process.env.PUSHGATEWAY;
if (!katpoolPshGw) {
throw new Error('Environment variable PUSHGATEWAY is not set.');
}
export const metrics = new PushMetrics(katpoolPshGw);
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, katpoolPshGw, treasury.address, config.stratum.sharesPerMinute);
const pool = new Pool(treasury, stratum, stratum.sharesManager);