forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslushpool.5m.js
executable file
·224 lines (201 loc) · 9.56 KB
/
slushpool.5m.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
#!/usr/bin/env /usr/local/bin/node
"use 'esversion: 8'"; // travisci complaining about needing this even when the next line is provided
"use 'esversion: 9'";
/*
# Slushpool Monitoring Xbar Plugin
#
# <xbar.title>Slushpool Profile, Account, Worker Plugin</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>Mark Phillips</xbar.author>
# <xbar.author.github>pleasemarkdarkly</xbar.author.github>
# <xbar.desc>TLDR; Displays Slushpool Profile/Worker information from its API Service. This is an Xbar/Bitbar macOS menu plugin which provided with a SLUSHPOOL API_KEY with at least ACCESS READ-ONLY will provide your hash rate statistic, mining proceeds unconfirmed, confirmed, expected proceeds and details about your workers including OK, OFF, and disconnectd.</xbar.desc>
# <xbar.image>https://github.com/pleasemarkdarkly/slushpool-xbar-plugin/blob/main/images/slushpool-xbar-plugin.png?raw=true</xbar.image>
# <xbar.dependencies>node</xbar.dependencies>
# <xbar.abouturl>https://github.com/pleasemarkdarkly/slushpool-xbar-plugin/</xbar.abouturl>
#
# This is an Xbar/Bitbar macOS menu plugin which provided with a SLUSHPOOL API_KEY with at least ACCESS READ-ONLY will provide your hash rate statistic, mining proceeds unconfirmed, confirmed, expected proceeds and details about your workers including OK, OFF, and disconnectd. Additional information regarding your participation pool is provided but specifically not used in this plugin however it would be trival to do so.
# Apparently using node.js as the scripting language for Xbar is somewhat touchy, so I went with the most vanilla inplementation and foregoing the npm package of bitbar. Additionally, I have left a working READ-ONLY key of a handful of workers as to provide a better experience of someone seeking to use this plugin before replacing or modifying information with their own.
# This plugin is not supported. Please use at your own risk.
*/
const https = require('https');
const child_process = require('child_process');
const { once } = require('events');
const SLUSHPOOL_MINING_ICON = `⛏️`;
const BITCOIN_ICON = 'iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAQAAABLCVATAAAACXBIWXMAABYlAAAWJQFJUiTwAAABY0lEQVRIx2P4z0AdyEBzg1DAdIYfQJgCZHmCWdsYMAFRBs0BC2UAWT5g1p6hbZAggwIcrgALVQNZSWDWAQY24g3qwRtJ/xgeMqxkCGJgotQgGLzAoEUdg/4zvGQQIxzYLAyODF/gQv0MlgwWDK4MOQxbgV5DKG0nLtZ2wIUykII2EMmoU8QZtAWrQQwMB+HiDygzaDNc/CQlBskwfIKLN5JrkAxDFsMTuOh9BiFSDXoHDI2HDB9RlJ1kECc2r20hkI5OMXhQxyAQzCTNoDJgaAgAvaLLEMkwn+EbkuLvDBLkR78yUoD/Z0gn3yAGhnwk5V2UGBRGLYNmICkvIGzQLqwG8TA0oJQAVvgMymcoYehg+AUXWgoM0kygWC/DbpQ4+89wjYERt0FiRNeNX4GlFJ505EykMacZDPGn7HwCBnxiOMcwjcGJcOEvzqADh2vBQk1AVhaYdZCBc7TKpqJBA9ZiAwDMH49EXcmY2QAAAABJRU5ErkJggg=='
// due to the error prone nature of the above path, dependancies, etc, edit script directly vs. .env
const VAR_SLUSHPOOL_API_KEY = 'Zm1oK8hGaDExOBaE'; // create your own SLUSHPOOL API KEY https://help.slushpool.com/en/support/solutions/articles/77000433512-api-configuration-guide
let slushpool_api_token = VAR_SLUSHPOOL_API_KEY; // left over for when I had logic to pull from various locations
let BTC_EXISTING_BALANCE = '.78711908'; // previously confirmed rewards or whatever you want, example BTC balance
let USD_EXISTING_BTC_BALANCE = 0;
const numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
const getBTCToUSD = async (amount) => {
let result;
options = {
hostname: 'rate.sx',
port: 443,
path: '/' + amount.toString() + 'BTC',
method: 'GET',
}
req = await https.request(options, res => {
res.on('data', d => {
// console.log(d.toString());
result = d.toString();
});
});
req.on('error', error => { console.error(error); });
req.end();
await once(req, 'close')
return result
};
const getSlushpoolProfile = async (apikey) => {
let result;
let pending_rewards;
const headers = { 'User-Agent': 'request', 'SlushPool-Auth-Token': apikey };
let options = {
hostname: 'slushpool.com',
port: 443,
path: '/accounts/profile/json/btc/',
method: 'GET',
headers: headers,
};
let req = await https.get(options, res => {
let json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', async () => {
if (res.statusCode === 200) {
try {
let data = JSON.parse(json);
result = data.btc;
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
await once(req, 'close')
return result
};
const getSlushpoolStats = async (apikey) => {
let result;
const headers = { 'User-Agent': 'request', 'SlushPool-Auth-Token': apikey };
let options = {
hostname: 'slushpool.com',
port: 443,
path: '/accounts/workers/json/btc/',
method: 'GET',
headers: headers,
};
let req = await https.get(options, res => {
let json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
try {
let data = JSON.parse(json);
let worker_keys = Array.from(Object.keys(data.btc.workers));
result = { worker_keys, ...data.btc, };
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
await once(req, 'close')
return result
};
/*
Compute ongoing mining balances/estimates, existing balances.
*/
const collectSlushpoolInfo = async () => {
try {
const profile = await getSlushpoolProfile(slushpool_api_token);
const stats = await getSlushpoolStats(slushpool_api_token);
const { confirmed_reward, unconfirmed_reward, estimated_reward } = profile;
let reward = {};
reward.usd_confirmed_reward = await getBTCToUSD(parseFloat(confirmed_reward));
reward.usd_unconfirmed_reward = await getBTCToUSD(parseFloat(unconfirmed_reward));
reward.usd_estimated_reward = await getBTCToUSD(parseFloat(estimated_reward));
// include any permuation of confirmed, unconfirmed, estimate, existing BTC or USD here
USD_EXISTING_BTC_BALANCE = await getBTCToUSD(parseFloat(BTC_EXISTING_BALANCE));
return { profile, stats, reward, total_btc_in_usd: USD_EXISTING_BTC_BALANCE };
} catch (error) {
return error;
}
}
const debugSlushpoolOutputAndFormat = async (bitbar) => {
const { profile, stats, total_btc, total_btc_in_usd } = bitbar;
const { estimated_reward, hash_rate_unit, hash_rate_5m, hash_rate_60m, hash_rate_24h, ok_workers } = profile;
console.log(`${total_btc} BTC, \$${parseFloat(total_btc_in_usd).toFixed(2)} `);
console.log(`${parseFloat(hash_rate_5m).toFixed(2)} (5m) ${hash_rate_unit}, ${parseFloat(hash_rate_60m).toFixed(2)} (60m) ${hash_rate_unit}, ${parseFloat(hash_rate_24h).toFixed(2)} (24 / h) ${hash_rate_unit}, OK WORKERS: ${ok_workers} `);
const { worker_keys, workers } = stats;
worker_keys.forEach(k => {
if (workers[k].state == 'OK') {
console.log(k);
console.log(workers[k]);
}
});
}
const SLUSHPOOL_URL = 'href=https://slushpool.com/dashboard/?c=btc';
const REFRESH_FALSE = 'refresh=false';
const SEPARATOR = '---';
const printBitbar = async (bitbar) => {
const { profile, stats, reward, total_btc_in_usd } = bitbar;
const { confirmed_reward, unconfirmed_reward, estimated_reward, hash_rate_unit, hash_rate_5m, hash_rate_60m, hash_rate_24h, ok_workers } = profile;
const { worker_keys, workers } = stats;
// Show in the menu if a worker is off
worker_keys.forEach(k => {
if (workers[k].state == 'off') {
const worker_name = k.split('.')[1];
console.log(`${worker_name.toLowerCase()}`);
}
});
console.log(`${SLUSHPOOL_MINING_ICON} ${numberWithCommas(parseFloat(hash_rate_5m).toFixed(2))} ${hash_rate_unit} (5m)`);
console.log(`${SLUSHPOOL_MINING_ICON} ${numberWithCommas(parseFloat(hash_rate_60m).toFixed(2))} ${hash_rate_unit} (60m)`);
console.log(`${SLUSHPOOL_MINING_ICON} ${numberWithCommas(parseFloat(hash_rate_24h).toFixed(2))} ${hash_rate_unit} (24h)`)
console.log(SEPARATOR);
console.log(`${confirmed_reward}/\$${parseFloat(reward.usd_confirmed_reward).toFixed(2)} (confirmed reward) | templateImage=${BITCOIN_ICON}`);
console.log(`${unconfirmed_reward}/\$${parseFloat(reward.usd_unconfirmed_reward).toFixed(2)} (unconfirmed reward) | templateImage=${BITCOIN_ICON}`);
console.log(`${estimated_reward}/\$${parseFloat(reward.usd_estimated_reward).toFixed(2)} (estimated reward) | templateImage=${BITCOIN_ICON}`);
console.log(SEPARATOR);
console.log(`OK workers (${ok_workers}) | ${SLUSHPOOL_URL}`)
worker_keys.forEach(k => {
if (workers[k].state == 'OK') {
const worker_name = k.split('.')[1];
console.log(SEPARATOR);
console.log(`${worker_name.toLowerCase()}`);
console.log(`${numberWithCommas(parseFloat(workers[k].last_share))} (last share)`)
console.log(`${numberWithCommas(parseFloat(workers[k].hash_rate_5m))} ${workers[k].hash_rate_unit} (5m)`);
console.log(`${numberWithCommas(parseFloat(workers[k].hash_rate_60m))} ${workers[k].hash_rate_unit} (60m)`);
console.log(`${numberWithCommas(parseFloat(workers[k].hash_rate_24h))} ${workers[k].hash_rate_unit} (24h)`);
}
});
console.log(SEPARATOR); // existing balance
console.log(`${BTC_EXISTING_BALANCE} \$${numberWithCommas(parseFloat(USD_EXISTING_BTC_BALANCE).toFixed(2))} | ${REFRESH_FALSE} templateImage=${BITCOIN_ICON}`);
};
(async () => {
let boldColor = 'white';
try {
child_process.execSync('defaults read -g AppleInterfaceStyle', { stdio: 'ignore' });
} catch (err) {
// AppleInterfaceStyle not set, which means user has light menu bar style
boldColor = 'black';
}
// verify key information is available
const bitbar = await collectSlushpoolInfo();
// debugSlushpoolOutputAndFormat(bitbar);
printBitbar(bitbar);
})();