-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicehash-sync.js
72 lines (68 loc) · 2.69 KB
/
nicehash-sync.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
const PlatformStats = require('./models/platform-stats');
const moment = require('moment');
const rp = require('request-promise');
class NicehashSync {
constructor (walletAddress) {
this.walletAddress = walletAddress;
}
start () {
let totalRaisedBitcoin = 0;
let stats = PlatformStats.findOne({}).exec()
.then(stats => {
if (!stats) {
stats = new PlatformStats();
}
let end = moment(new Date());
let duration = moment.duration(end.diff(moment(stats.lastRun)));
let hours = duration.asHours();
console.log(hours);
if (hours < 0.1) {
return stats.save();
}
console.log('executing nice hash sync');
return rp.get(`https://api.nicehash.com/api?method=stats.provider.workers&addr=${this.walletAddress}`)
.then(response => {
response = JSON.parse(response);
let result = response.result;
stats.workers = result.workers.length;
console.dir(result);
return rp.get(`https://api.nicehash.com/api?method=stats.provider&addr=${this.walletAddress}`);
})
.then(response => {
response = JSON.parse(response);
let result = response.result;
result.stats.forEach(stat => {
totalRaisedBitcoin += parseFloat(stat.balance);
});
return rp.get(`https://blockchain.info/q/getreceivedbyaddress/${this.walletAddress}`);
})
.then(response => {
let result = parseInt(response);
totalRaisedBitcoin += (result / 100000000);
return rp.get('https://blockchain.info/ticker');
})
.then(response => {
response = JSON.parse(response);
let totalRaisedDollars = totalRaisedBitcoin * response.USD.buy;
console.log(`total amount raised: ${stats.amountRaised}-${totalRaisedDollars}`);
let ratePerHour = (totalRaisedDollars - stats.amountRaised)/duration;
stats.amountRaised = totalRaisedDollars;
stats.rate = ratePerHour * 24;
stats.lastRun = new Date();
console.dir(stats.toObject());
return stats.save();
});
})
.catch(err => {
console.error(err);
})
.finally(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve(this.start());
}, 20000);
});
});
}
}
module.exports = NicehashSync;