-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
69 lines (57 loc) · 2.02 KB
/
index.ts
File metadata and controls
69 lines (57 loc) · 2.02 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
import fs from "fs";
import https from "https";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import { sendTelegramMessage } from "./utils/telegram";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const REMOTE_URL =
"https://raw.githubusercontent.com/lifinance/contracts/refs/heads/main/config/networks.json";
const LOCAL_CACHE = path.join(__dirname, "networks.cache.json");
interface NetworkMap {
[chainId: string]: any;
}
// Format: DD.MM.YYYY
const getFormattedDate = (): string => {
const now = new Date();
return now.toLocaleDateString("en-GB").replace(/\//g, ".");
};
function fetchRemoteJson(url: string): Promise<NetworkMap> {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(JSON.parse(data)));
})
.on("error", reject);
});
}
async function checkForNewChains() {
const latestData = await fetchRemoteJson(REMOTE_URL);
const newKeys = Object.keys(latestData);
let previousKeys: string[] = [];
if (fs.existsSync(LOCAL_CACHE)) {
const oldData: NetworkMap = JSON.parse(
fs.readFileSync(LOCAL_CACHE, "utf8")
);
previousKeys = Object.keys(oldData);
}
const added = newKeys.filter((key) => !previousKeys.includes(key));
const removed = previousKeys.filter((key) => !newKeys.includes(key));
if (added.length || removed.length) {
let message = `*LiFi Chain Monitor*\n📅 ${getFormattedDate()}\n`;
if (added.length) message += `🆕 Added: \`${added.join("`, `")}\`\n`;
if (removed.length) message += `❌ Removed: \`${removed.join("`, `")}\``;
await sendTelegramMessage(message);
} else {
console.log("✅ No changes in chain list.");
}
fs.writeFileSync(LOCAL_CACHE, JSON.stringify(latestData, null, 2));
}
checkForNewChains().catch((err) => {
console.error("[-] Script error:", err.message);
process.exit(1);
});