Last active
December 14, 2017 20:29
-
-
Save alanpca/230640aeeb3c5aa342183771ab9611bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request-promise'); | |
const _ = require('lodash'); | |
const MIN_HASH_RATE_MH = 5.0; | |
const IFTTT_EVENT_NAME = 'ethermine_notification'; | |
const IFTTT_KEY = 'wompwomp'; | |
const ETHERMINE_ADDRESS = 'wompwomp'; | |
const POLL_TIME = 20 * 60 * 1000; | |
const RENOTIFICATION_TIMEOUT = 60 * 60 * 1000; | |
const NUM_WORKERS = 2; | |
var lastHashInfo = {}; | |
var sendNotification = function(options) { | |
request.post({ | |
url: `https://maker.ifttt.com/trigger/${IFTTT_EVENT_NAME}/with/key/${IFTTT_KEY}`, | |
json: options | |
}); | |
} | |
var shouldSendNotification = function(hashRate, hashInfo) { | |
console.log("checking worker", hashInfo); | |
if (hashInfo == null || hashInfo.time == null || hashInfo.hashRate == null) { | |
return hashRate < MIN_HASH_RATE_MH; | |
} | |
if ( | |
hashInfo.hashRate < MIN_HASH_RATE_MH && ( | |
hashRate != hashInfo.hashRate || | |
Date.now() - hashInfo.time >= RENOTIFICATION_TIMEOUT | |
) | |
) { | |
return true; | |
} | |
return false; | |
} | |
var poll = function() { | |
return request.get({ | |
url: `https://ethermine.org/api/miner_new/${ETHERMINE_ADDRESS}`, | |
json: true | |
}).then((output) => { | |
var workersReported = _.keys(output.workers).length; | |
if (NUM_WORKERS && workersReported < NUM_WORKERS) { | |
sendNotification({ | |
event_name: IFTTT_EVENT_NAME, | |
value1: `Only ${workersReported} workers are reporting` | |
}); | |
} | |
_.each(output.workers, (worker) => { | |
var hashRate = parseFloat(worker.hashrate); | |
var workerInfo = lastHashInfo[worker.worker]; | |
console.log(`${new Date()}: worker ${worker.worker} has hashrate ${hashRate}. Minimum is ${MIN_HASH_RATE_MH}`); | |
if (shouldSendNotification(hashRate, workerInfo)) { | |
console.log('Sending notification'); | |
sendNotification({ | |
event_name: IFTTT_EVENT_NAME, | |
value1: worker.worker, | |
value2: worker.hashrate | |
}); | |
} | |
lastHashInfo[worker.worker] = {time: Date.now(), hashRate}; | |
}); | |
}); | |
} | |
poll().then(() => { setInterval(poll, POLL_TIME) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment