-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
189 lines (150 loc) · 6.45 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
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
const axios = require('axios').default;
const moment = require('moment');
const async = require('async');
const csv = require('csv-stringify')
const fs = require('fs');
const _ = require('lodash');
const colors = require('colors/safe');
const jsonfile = require('jsonfile')
const HELIUM_API = "https://api.helium.io/v1";
const HT_ADDRESSES = process.env.HT_ADDRESSES;
const START_DATE = process.env.HT_START_DATE || moment().subtract(7, 'days').format('YYYY-MM-DD');
const END_DATE = process.env.HT_END_DATE || moment().format('YYYY-MM-DD');
const MAX_DAYS = moment(END_DATE).diff(moment(START_DATE), 'days');
const get_days = (start, end) => {
let days = [];
while (moment(start).format('YYYY-MM-DD') != moment(end).format('YYYY-MM-DD')) {
days.push(moment(start).format('YYYY-MM-DD'));
start = moment(start).add(1, 'days');
}
days.push(moment(end).format('YYYY-MM-DD'));
return days;
};
const prices_file = 'data/prices.json';
const days = get_days(START_DATE, END_DATE);
if (!fs.existsSync(prices_file)) {
jsonfile.writeFileSync(prices_file, {});
}
const miner_history = (address, callback) => {
const cached_prices = jsonfile.readFileSync(prices_file);
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
console.log(`getting data from ${moment(START_DATE).format('YYYY-MM-DD')} to ${moment(END_DATE).format('YYYY-MM-DD')} Total: ${MAX_DAYS} Days for ${colors.yellow(address)}`);
async.parallel({
mined: (cb) => {
let results = {};
async.eachLimit(days, 10, (day, next) => {
const min_time = moment(day);
const max_time = moment(day).add(1, 'days');
axios.get(`${HELIUM_API}/accounts/${address}/rewards/sum?min_time=${min_time.toISOString()}&max_time=${max_time.toISOString()}`)
.then((resp) => {
const mined = Number(_.get(resp, 'data.data.total', 0).toFixed(2));
console.log(`${min_time.format('YYYY-MM-DD')} mined ${mined}HNT`);
_.set(results, [day], _.merge({}, _.get(results, [day], {}), {
mined: mined,
date: day
}));
next();
})
.catch((err) => {
if (err) console.log(err);
next(err)
});
}, (err) => {
cb(err, results);
});
},
prices: (cb) => {
let results = {};
async.eachSeries(days, (day, next) => {
const date = moment(day);
if (_.has(cached_prices, day)) {
const price = _.get(cached_prices, day);
console.log(`${date.format('YYYY-MM-DD')} price $${price} from CACHE`);
_.set(results, [day], _.merge({}, _.get(results, [day], {}), {
price: price
}));
next();
} else {
axios.get(`https://api.coingecko.com/api/v3/coins/helium/history?date=${date.format('DD-MM-YYYY')}&localization=false`)
.then((resp) => {
const price = Number(_.get(resp, 'data.market_data.current_price.usd', 0).toFixed(2));
console.log(`${date.format('YYYY-MM-DD')} price $${price} from API`);
_.set(results, [day], _.merge({}, _.get(results, [day], {}), {
price: price
}));
if (days.length > 100) {
sleep(500).then(next);
} else {
next();
}
})
.catch((err) => {
if (err) console.log(err);
next(err)
});
}
}, (err) => {
cb(err, results);
});
},
}, function (err, results) {
if (err) console.log(err);
jsonfile.writeFileSync(prices_file, _.merge(cached_prices, _.mapValues(results.prices, 'price')), {
spaces: 2
});
const final_data = _
.chain(results.mined)
.merge(results.prices)
.values()
.sortBy('date')
.map((d) => {
d.taxable = Number((d.price * d.mined).toFixed(2));
return d;
})
.value()
callback(err, final_data);
});
};
let grand_total = {
taxable: 0,
mined: 0
};
async.eachSeries(_.split(HT_ADDRESSES, ','), (address, callback) => {
miner_history(address, (err, results) => {
if (err) callback(err);
const taxable_total = _.reduce(results, function (sum, r) {
return sum + r.taxable;
}, 0);
const mined_total = _.reduce(results, function (sum, r) {
return sum + r.mined;
}, 0);
grand_total.taxable = grand_total.taxable + taxable_total;
grand_total.mined = grand_total.mined + mined_total;
console.log(colors.green(`Mined for period ${mined_total.toFixed(2)} HNT`));
console.log(colors.green(`Taxable income for period $${taxable_total.toFixed(2)}`));
const total = {
mined: mined_total,
date: "TOTAL",
price: 0,
taxable: taxable_total
};
csv(_.concat([total], results), {
header: true,
columns: ['date', 'mined', 'price', 'taxable']
}, function (err, data) {
if (err) callback(err);
fs.writeFile(`reports/${address}_${moment(START_DATE).format('YYYY-MM-DD')}_${moment(END_DATE).format('YYYY-MM-DD')}.csv`, data, function (err) {
if (err) callback(err);
console.log(colors.yellow(`FILE: reports/${address}_${moment(START_DATE).format('YYYY-MM-DD')}_${moment(END_DATE).format('YYYY-MM-DD')}.csv`))
callback(err);
});
});
});
}, (err) => {
if (err) console.log(err);
console.log(colors.red(`For ${HT_ADDRESSES} between ${START_DATE} and ${END_DATE}`));
console.log(colors.red(`You mined ${grand_total.mined.toFixed(2)} HNT!`));
console.log(colors.red(`Taxable income: $${grand_total.taxable.toFixed(2)}`));
})