forked from ZanderAdam/StellarTransactionExport
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
113 lines (91 loc) · 2.98 KB
/
main.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
// lumenaut payments export
var loadedPayments = [];
document.getElementById("load").onclick = function () {
lumenaut = 'GCCD6AJOYZCUAQLX32ZJF2MKFFAUJ53PVCFQI3RHWKL3V47QYE2BNAUT';
address = document.getElementById('address').value;
if (!address) {
console.error("Your Public Key is missing!");
return;
}
var timeFrameValue = document.getElementById("timeframe").value;
endDate = moment();
startDate = moment().subtract(30, 'days').startOf('day');
switch (timeFrameValue) {
case 'year':
startDate = moment().subtract(1, 'year').startOf('day');
break;
case 'prevYear':
endDate = moment().startOf('year');
timeFrameValue
startDate = moment().startOf('year').subtract(1, 'year');
break;
}
var url = 'https://horizon.stellar.org/accounts/' + address + '/payments?limit=200&order=desc'
getData(url, lumenaut, startDate, endDate, function (payments) {
loadedPayments = payments;
var dataTable = document.getElementById("data");
dataTable.innerHTML = '';
payments.forEach(function (payment) {
dataTable.innerHTML += `<tr><td>${payment.date.format()}</td><td>${payment.amount}</td></tr>`;
});
document.getElementById("csv").disabled = false;
});
}
document.getElementById("csv").onclick = function () {
var lumenaut = 'GCCD6AJOYZCUAQLX32ZJF2MKFFAUJ53PVCFQI3RHWKL3V47QYE2BNAUT';
var timeFrameValue = document.getElementById("timeframe").value;
//create csv
var csvFile = 'Date,Amount(XLM)';
loadedPayments.forEach(function (payment) {
csvFile += '\n' + payment.date.format() + ',' + payment.amount;
})
//download as file
var a = document.createElement('a');
var file = new Blob([csvFile], {
type: 'csv'
});
a.href = URL.createObjectURL(file);
a.download = `${lumenaut.substring(0,6)}_${timeFrameValue}_PaymentsExport.csv`;
a.click();
}
function getData(url, lumenaut, startDate, endDate, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var payments = JSON.parse(xmlHttp.responseText);
processPayments(payments, lumenaut, startDate, endDate, callback);
}
else if (xmlHttp.status == 404) {
callback([]);
}
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function processPayments(payments, lumenaut, startDate, endDate, callback) {
var paymentsOfInterest = [];
var lastPaymentDate;
var records = payments['_embedded'].records;
if (records.length == 0) {
callback(paymentsOfInterest);
return;
}
records.forEach(function (payment) {
lastPaymentDate = moment(payment['created_at']);
if (!lastPaymentDate.isBetween(startDate, endDate)) {
return;
}
if (payment['source_account'] == lumenaut) {
paymentsOfInterest.push({
date: lastPaymentDate,
amount: payment.amount
});
}
});
if (lastPaymentDate.isAfter(startDate))
getData(payments['_links'].next.href, lumenaut, startDate, endDate, function (newPayments) {
callback(paymentsOfInterest.concat(newPayments));
});
else
callback(paymentsOfInterest);
}