-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
312 lines (274 loc) · 12.2 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
require("dotenv").config();
const axios = require('axios');
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
const CONFIGURATION = require("./config");
let lastRecordedTotalSupply = {
value: undefined,
recordedAt: undefined,
};
let lastRecordedCirculatingSupply = {
value: undefined,
recordedAt: undefined,
};
// Store the *last error* encountered during fetching/recording for each metric
let lastAttemptError = {
totalSupply: undefined, // String description of the last error, or undefined if last attempt was successful
circulatingSupply: undefined, // String description of the last error, or undefined if last attempt was successful
recordedAt: undefined, // Timestamp of the last error occurrence (or last successful attempt)
};
const largeNumber = 1000000000000000000;
const getUnixtimestamp = () => {
return Math.floor(Date.now() / 1000);
}
// Create an Axios instance pre-configured for UTXO RPC
const rpc = axios.create({
baseURL: `http://${process.env.SYSCOIN_CORE_RPC_HOST}:${process.env.SYSCOIN_CORE_RPC_PORT}/`,
auth: {
username: process.env.SYSCOIN_CORE_RPC_USERNAME,
password: process.env.SYSCOIN_CORE_RPC_PASSWORD
},
headers: { 'Content-Type': 'application/json' },
timeout: 10000
});
const explorerApi = axios.create({
timeout: 8000
});
/**
* Calls gettxoutsetinfo on UTXO JSON-RPC
*/
async function getTxOutSetInfo() {
console.log("Fetching UTXO gettxoutsetinfo via RPC...");
try {
const requestData = { jsonrpc: '1.0', id: 'gettxoutsetinfo', method: 'gettxoutsetinfo', params: [] };
const response = await rpc.post('', requestData);
if (response.data.error) {
throw new Error(`RPC error: ${response.data.error.message || JSON.stringify(response.data.error)}`);
}
if (!response.data.result || typeof response.data.result.total_amount !== 'number') {
throw new Error(`Invalid data structure in gettxoutsetinfo response: ${JSON.stringify(response.data)}`);
}
console.log("UTXO set info fetched successfully.");
return response.data.result;
} catch (err) {
let errorMessage = `Error fetching UTXO set info: ${err.message}`;
if (err.response) {
errorMessage += ` (Status: ${err.response.status}, Data: ${JSON.stringify(err.response.data)})`;
} else if (err.request) {
errorMessage += ` (No response received, check RPC connection/URL)`;
}
console.error(errorMessage);
throw new Error(errorMessage); // Re-throw for the recording function
}
}
const getSupply = async () => {
console.log("Fetching total supply components...");
try {
const [supplyInfo, explorerResponse, nevmAddResponse] = await Promise.all([
getTxOutSetInfo(),
explorerApi.get(
"https://explorer-v5.syscoin.org/api?module=stats&action=coinsupply"
),
explorerApi.get(
`https://explorer.syscoin.org/api?module=account&action=balance&address=${CONFIGURATION.SyscoinVaultManager}`
)
]);
// Extract data and validate
const utxoSupply = supplyInfo.total_amount; // Already validated in getTxOutSetInfo
const nevmSupply = explorerResponse.data;
const nevmAdd = nevmAddResponse.data;
if (typeof nevmSupply !== 'number' || nevmSupply < 0) {
throw new Error(`Invalid nevmSupply (explorer-v5): ${nevmSupply}`);
}
if (!nevmAdd || nevmAdd.status !== "1" || typeof nevmAdd.result !== 'string') {
throw new Error(`Invalid nevmAdd response structure or status: ${JSON.stringify(nevmAdd)}`);
}
const nevmAddContractSupply = nevmAdd.result;
const nevmContract = parseFloat(nevmAddContractSupply) / largeNumber;
if (isNaN(nevmContract)) throw new Error(`Could not parse nevmAddContractSupply: ${nevmAddContractSupply}`);
console.log({ utxoSupply, nevmSupply, nevmContract });
const cmcSupply = nevmSupply - nevmContract + utxoSupply;
if (isNaN(cmcSupply) || cmcSupply < 0) throw new Error(`Calculated cmcSupply is invalid: ${cmcSupply}`);
console.log("Total supply components fetched and calculated successfully."); // Add logging
return cmcSupply;
} catch (error) {
// Log the specific error causing getSupply to fail
console.error(`Error in getSupply calculation/fetching: ${error.message}`);
// Re-throw to be caught by the recording function
throw error;
}
};
const getCirculatingSupply = async () => {
console.log("Fetching circulating supply components...");
// Check dependency on successfully recorded total supply
if (lastRecordedTotalSupply.value === undefined) {
console.warn("Cannot calculate circulating supply: Total supply has never been successfully recorded.");
throw new Error("Dependency Error: Total Supply unavailable");
}
const treasuryBalance = 0; // Treasury is now factored into sys5 governance/utxo
const finalCirculatingSupply = lastRecordedTotalSupply.value - treasuryBalance;
console.log("Circulating supply calculated successfully.");
return finalCirculatingSupply;
};
const recordTotalSupply = async () => {
console.log("Attempting to record total supply...");
try {
const supply = await getSupply(); // Can throw errors
// Success Case: Update value and clear specific error
lastRecordedTotalSupply.value = supply;
lastRecordedTotalSupply.recordedAt = getUnixtimestamp();
lastAttemptError.totalSupply = undefined; // Clear error on success
lastAttemptError.recordedAt = getUnixtimestamp();
console.log(`Total supply recorded successfully: ${supply} at ${new Date(lastRecordedTotalSupply.recordedAt * 1000).toISOString()}`);
} catch (error) {
// Failure Case: Log, update error state, *keep existing value*
const errorMessage = `Failed to record total supply: ${error.message || "Unknown error"}`;
console.error(errorMessage);
lastAttemptError.totalSupply = errorMessage; // Store the error message
lastAttemptError.recordedAt = getUnixtimestamp();
// DO NOT update lastRecordedTotalSupply.value or .recordedAt
}
};
const recordCirculatingSupply = async () => {
console.log("Attempting to record circulating supply...");
// Check dependency: Has total supply *ever* been recorded successfully?
if (lastRecordedTotalSupply.value === undefined) {
const errorMessage = "Skipped: Total supply has never been recorded.";
console.warn(errorMessage);
lastAttemptError.circulatingSupply = errorMessage;
lastAttemptError.recordedAt = getUnixtimestamp();
return; // Don't proceed
}
// Check dependency: Did the *last attempt* to get total supply fail?
if (lastAttemptError.totalSupply !== undefined) {
const errorMessage = "Skipped: Last total supply fetch failed.";
console.warn(errorMessage);
lastAttemptError.circulatingSupply = errorMessage;
lastAttemptError.recordedAt = getUnixtimestamp();
return; // Don't proceed
}
try {
const supply = await getCirculatingSupply(); // Can throw errors
// Success Case: Update value and clear specific error
lastRecordedCirculatingSupply.value = supply;
lastRecordedCirculatingSupply.recordedAt = getUnixtimestamp();
lastAttemptError.circulatingSupply = undefined; // Clear error on success
lastAttemptError.recordedAt = getUnixtimestamp();
console.log(`Circulating supply recorded successfully: ${supply} at ${lastRecordedCirculatingSupply.recordedAt}`);
} catch (error) {
// Failure Case: Log, update error state, *keep existing value*
const errorMessage = `Failed to record circulating supply: ${error.message || "Unknown error"}`;
console.error(errorMessage);
if (error.response) console.error(" -> Axios Response Error Data:", error.response.data);
lastAttemptError.circulatingSupply = errorMessage; // Store the error message
lastAttemptError.recordedAt = getUnixtimestamp();
// DO NOT update lastRecordedCirculatingSupply.value or .recordedAt
}
};
const runRecordingCycle = async () => {
await recordTotalSupply();
// recordCirculatingSupply internally checks dependencies
await recordCirculatingSupply();
console.log("Current State - Total:", lastRecordedTotalSupply.value, "Circ:", lastRecordedCirculatingSupply.value, "Errors:", lastAttemptError.totalSupply, lastAttemptError.circulatingSupply);
};
app.get("/totalsupply", (req, res) => {
if (lastRecordedTotalSupply.value === undefined) {
// Data not yet successfully recorded
console.warn("Service Unavailable: Request to /totalsupply. Data not initialized.");
res.status(503).set("Content-Type", "text/plain").send("Service Unavailable: Data initialization failed or pending.");
} else {
// Data is available, send it
res.set("Content-Type", "text/html");
res.status(200).send(`${lastRecordedTotalSupply.value}`);
}
});
app.get("/circulatingsupply", (req, res) => {
if (lastRecordedCirculatingSupply.value === undefined) {
// Data not yet successfully recorded (or depends on total supply which failed)
console.warn("Service Unavailable: Request to /circulatingsupply. Data not initialized.");
res.status(503).set("Content-Type", "text/plain").send("Service Unavailable: Data initialization failed or pending.");
} else {
// Data is available, send it
res.set("Content-Type", "text/html"); // Keep original Content-Type on success
res.status(200).send(`${lastRecordedCirculatingSupply.value}`); // No '?? 0' needed here
}
});
app.get("/triggerRecordSupply", async (req, res) => {
console.log("Manual trigger received: Recording supply...");
await recordTotalSupply();
await recordCirculatingSupply();
// Send the state objects *after* the calls
res
.status(200)
.send(JSON.stringify({
newRecordedSupply: lastRecordedTotalSupply,
newCirculatingSupply: lastRecordedCirculatingSupply
}));
});
app.get("/health", async (req, res) => {
console.log("Health check", new Date());
res.status(200).send("OK");
});
app.get("/status", async (req, res) => {
if (undefined !== lastAttemptError.circulatingSupply || undefined !== lastAttemptError.totalSupply) {
res.status(200).json({
status: "ERROR",
// Send the last *good* recorded state
lastCirculatingSupply: lastRecordedCirculatingSupply,
lastTotalSupply: lastRecordedTotalSupply,
lastError: {
// Can be undefined or error string
circulatingSupply: lastAttemptError.circulatingSupply,
totalSupply: lastAttemptError.totalSupply,
},
});
} else {
res.status(200).json({status: "OK"});
}
});
const server = app.listen(port, async () => {
console.log(`Syscoin Info app listening on port ${port}`);
console.log("Performing initial supply recording on startup...");
await runRecordingCycle(); // Run the first cycle immediately
console.log("Initial supply recording attempt complete.");
// --- Periodic Polling ---
const POLLING_INTERVAL_SECONDS = process.env.POLLING_INTERVAL_SECONDS ? parseInt(process.env.POLLING_INTERVAL_SECONDS, 10) : 30; // Default 30 seconds
const POLLING_INTERVAL_MS = POLLING_INTERVAL_SECONDS * 1000;
if (POLLING_INTERVAL_MS > 0) { // Avoid interval of 0 or less
console.log(`Starting supply polling every ${POLLING_INTERVAL_SECONDS} seconds.`);
const pollingInterval = setInterval(runRecordingCycle, POLLING_INTERVAL_MS);
} else {
console.log("Polling interval is set to 0 or less. Polling disabled after initial fetch.");
}
const shutdown = (signal) => {
console.log(`${signal} signal received: Shutting down server...`);
console.log('Attempting to stop polling interval (if active)...'); // Best effort log
if (pollingInterval) { // If stored locally
clearInterval(pollingInterval);
console.log('Polling stopped.');
}
server.close((err) => { // Close the HTTP server
if (err) {
console.error("Error closing HTTP server:", err);
process.exit(1);
} else {
console.log('HTTP server closed.');
process.exit(0);
}
});
setTimeout(() => {
console.error('Could not close connections gracefully, forcefully shutting down');
process.exit(1);
}, 10*1000);
};
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});