-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (98 loc) · 4.03 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
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const { HookClientFactory, Defaults } = require('evernode-js-client');
const schedule = require('node-schedule');
// Set default configuration for Evernode
Defaults.set({
governorAddress: "rBvKgF3jSZWdJcwSsmoJspoXLLDVLDp6jg",
rippledServer: "wss://xahau.network",
stateIndexId: "evernodeprod",
networkID: 21337
});
// Telegram Bot Token and Chat ID from .env file
const token = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_TOKEN;
const bot = new TelegramBot(token, { polling: true });
const allowedUserId = 5242456536;
// Hosts to monitor
const hostsToMonitor = new Set([
"raddress",
"raddress",
"raddress",
"raddress",
]);
function formatDomain(domain) {
// Insert a zero-width space after the dot
return domain.replace(/\./g, '.\u200B');
}
// Query the Xahau Ledger for active and inactive hosts
async function queryXahauLedger() {
try {
const registryClient = await HookClientFactory.create('REGISTRY');
await registryClient.connect();
const allHosts = await registryClient.getAllHostsFromLedger();
let output = '*Hosts Report*\n\n';
let totalAccumulatedReward = 0;
let inactiveHostsOutput = '\n*Inactive Hosts*\n\n';
allHosts.forEach(host => {
if (hostsToMonitor.has(host.address)) {
const formattedDomain = formatDomain(host.domain);
let rewardFormatted = parseFloat(host.accumulatedRewardAmount);
if (isNaN(rewardFormatted)) {
rewardFormatted = 'Data Unavailable';
} else {
rewardFormatted = rewardFormatted.toFixed(2);
}
// Truncate address to last 8 digits
const truncatedAddress = host.address.slice(-8);
const activeOutput = `*Domain*: \`${formattedDomain}\`\n` +
`*Address*: ${truncatedAddress}\n` +
`*Active*: ${host.active ? 'True' : 'False'}\n` +
`*Accumulated Reward*: ${rewardFormatted}\n` +
`*Max Instances*: ${host.maxInstances}\n` +
`*Active Instances*: ${host.activeInstances}\n\n`;
if (host.active) {
output += activeOutput;
if (!isNaN(rewardFormatted)) {
totalAccumulatedReward += parseFloat(rewardFormatted);
}
} else {
inactiveHostsOutput += activeOutput;
}
}
});
output += `*Total Accumulated Reward for Active Hosts*: ${totalAccumulatedReward.toFixed(2)}\n`;
output += inactiveHostsOutput;
await registryClient.disconnect();
return output || '_No matching hosts found._';
} catch (error) {
console.error('Error in queryXahauLedger:', error);
return '_Error processing your request._';
}
}
// Scheduled task to fetch ledger data
schedule.scheduleJob('50 * * * *', async function () {
console.log('Running scheduled task to fetch ledger data');
const ledgerData = await queryXahauLedger();
if (ledgerData) {
bot.sendMessage(chatId, ledgerData, { parse_mode: 'Markdown' });
} else {
bot.sendMessage(chatId, 'No data available or an error occurred.');
}
});
// Handle Telegram commands
bot.on('message', async (msg) => {
if (msg.from.id === allowedUserId && msg.text === '/query') {
bot.sendMessage(msg.chat.id, "Processing your request...");
const ledgerData = await queryXahauLedger();
if (ledgerData) {
bot.sendMessage(chatId, ledgerData, { parse_mode: 'Markdown' });
} else {
bot.sendMessage(chatId, 'No data available or an error occurred.');
}
}
});
// Start the bot
bot.on('polling_error', (error) => {
console.log(error);
});