Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. const request = require('request');
  2. const _ = require('underscore');
  3. const moment = require('moment');
  4. const util = require('util');
  5. const get = util.promisify(request.get);
  6.  
  7. const CURRENCY_CODE = 'USD';
  8. const ALGORITHM_MAP = { 20 : 'DaggerHashimoto', 21 : 'Decred' };
  9. const WALLET_ADDR = '1G73qGRBHqG5SjxCB9gg1jzPghy51ViTgP';
  10.  
  11. function parseInput() {
  12. const input = {};
  13.  
  14. const minutes = _.isUndefined(process.argv[2]) ? 60 : parseInt(process.argv[2], 10);
  15. const endTime = _.isUndefined(process.argv[3]) ? moment().unix() : moment().unix() - (parseInt(process.argv[3], 10) * 60);
  16.  
  17. input.end = endTime / 300;
  18. input.start = (moment().unix() - (minutes * 60)) / 300;
  19. input.minutes = minutes;
  20.  
  21. return input;
  22. }
  23.  
  24. async function getConversion() {
  25. const response = await get('https://blockchain.info/ticker');
  26. const body = JSON.parse(response.body)
  27.  
  28. return body[CURRENCY_CODE].last
  29. }
  30.  
  31. async function getNiceHashData() {
  32. const uri = 'http://api.nicehash.com/api?method=stats.provider.ex&addr=' + WALLET_ADDR;
  33. const response = await get(uri);
  34.  
  35. return JSON.parse(response.body);
  36. }
  37.  
  38. async function parseData(input) {
  39. let total = 0;
  40.  
  41. const conversion = await getConversion();
  42. const data = await getNiceHashData();
  43. const result = data.result;
  44.  
  45. process.stdout.write('\033c');
  46.  
  47. console.log('Getting average over the last %d minutes\n', input.minutes);
  48. _.each(result.past, function(algo) {
  49. const algorithm = _.isUndefined(ALGORITHM_MAP[algo.algo]) ? algo.algo : ALGORITHM_MAP[algo.algo];
  50. console.log('Algorithm: %s', algorithm);
  51.  
  52. const applicable = _.filter(algo.data, function(report) {
  53. return report[0] > input.start && report[0] < input.end && !_.isUndefined(report[1].a);
  54. });
  55.  
  56. const idleTime = _.filter(algo.data, function(report) {
  57. return report[0] > input.start && report[0] < input.end && _.isUndefined(report[1].a);
  58. }).length * 5;
  59.  
  60. const sum = _.reduce(applicable, function(aggr,report) {
  61. return aggr + parseFloat(report[1].a);
  62. }, 0);
  63.  
  64. const average = sum / applicable.length;
  65.  
  66. const extraInfo = _.find(result.current, function(miner) {
  67. return miner.algo === algo.algo;
  68. });
  69.  
  70. const profitability = average * parseFloat(extraInfo.profitability);
  71.  
  72. const suffix = extraInfo.suffix;
  73.  
  74. total += profitability;
  75.  
  76. console.log('Average hashrate: %d %s', average.toFixed(3), suffix);
  77. console.log('Profitability: %d mBTC/Day', (profitability * 1000).toFixed(2));
  78. console.log('Profitability: %d %s/Day', (profitability * conversion).toFixed(2), CURRENCY_CODE);
  79. console.log('Idle time: %d\n', idleTime);
  80. });
  81.  
  82. console.log('Total profitability: %d mBTC/Day', (total * 1000).toFixed(2));
  83. console.log('Total profitability: %d %s/Day', (total * conversion).toFixed(2), CURRENCY_CODE);
  84. }
  85.  
  86. async function start() {
  87. const input = parseInput();
  88.  
  89. parseData(input);
  90.  
  91. setInterval(() => { parseData(input); }, 11000);
  92. }
  93.  
  94. start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement