-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
70 lines (59 loc) · 2.17 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
const tax = require('./tax')
const form = document.querySelector("form");
const progress = document.querySelector("#progress");
const warning = document.querySelector("#warning");
form.addEventListener("submit", event => {
event.preventDefault();
gtag('event', 'submit')
const hotspots = new Set()
const validators = new Set()
let transactionCount = 0
let hntSum = 0
let usdSum = 0
const progressCB = ({ address, hnt, usd, isValidator }) => {
if (isValidator) {
validators.add(address)
} else {
hotspots.add(address)
}
transactionCount += 1
hntSum += hnt
usdSum += usd
const hotspotCount = (hotspots.size > 1) ? `${hotspots.size} hotspots / ` : ""
const validatorsCount = (validators.size > 1) ? `${validators.size} validators / ` : ""
progress.innerHTML = hotspotCount + validatorsCount + `${transactionCount} transactions / ${hntSum.toFixed(0)} HNT / $${usdSum.toFixed(2)} income`
}
const warningCB = (kind, message) => {
const li = document.createElement("li");
li.innerHTML = message
gtag('event', kind)
warning.appendChild(li)
}
const address = form.elements.address.value;
if(!/^\w+$/.test(address)) {
warningCB('bad_address', 'Use address and not name <a target="_blank" href="https://gist.github.com/davetapley/723f2d266f17561c9b2fbc70993fb6ba">More info here.</a>')
return
}
const year = form.elements.year.value;
tax(address, year, progressCB, warningCB).then((rows) => {
if(rows.length === 0) {
warningCB('no_rows', `No rewards during ${year}`)
return
}
gtag('event', 'success')
progress.innerHTML += ' ✅'
const header = `${Object.keys(rows[0]).join(',')}\n`
const values = rows.reverse().map((row) => `${Object.values(row).join(',')}\n`)
const csv = [header].concat(values)
var a = window.document.createElement('a');
a.style.display = 'none';
a.href = window.URL.createObjectURL(new Blob(csv, { type: 'text/csv' }));
a.download = `${address}.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}).finally(() => {
progress.classList.add('done')
})
progress.classList.add('active')
})