-
Notifications
You must be signed in to change notification settings - Fork 12
/
track-repayments.html
268 lines (236 loc) · 10.3 KB
/
track-repayments.html
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html lang="en">
<head>
<title>Repayments</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<center>
<h2>Apr 2020 security incident repayment report</h2>
<small>Hard-coded input parameters, copied from: <a href="https://github.com/bisq-network/admin/issues/76">https://github.com/bisq-network/admin/issues/76</a>
<br>You can paste updated info into this box, or update this source file when new information is available.
<br>(NB: tabs are significant)</small>
<br>
<br>
<textarea rows=10 cols=72 id="phrase"></textarea>
<br>
<br>
<button id='START' onclick='startTest();'>run report</button>
<br>
<br>
<div id="results">results</div>
</center>
</body>
<script type="text/javascript">
window.readyHandlers = [];
window.ready = function ready(handler) {
window.readyHandlers.push(handler);
handleState();
};
window.handleState = function handleState () {
if (['interactive', 'complete'].indexOf(document.readyState) > -1) {
while(window.readyHandlers.length > 0) {
(window.readyHandlers.shift())();
}
}
};
document.onreadystatechange = window.handleState;
ready(function () {
// this hard-coded input data is cut-and-paste copied from
// https://github.com/bisq-network/admin/issues/76
// will need to update here whenever a change is made (i.e. a new address)
var str =
"ID btc lost usd value repayment address\n"
+"1 4.08 $25,391.88 19qA2BVPoyXDfHKVMovKG7SoxGY7xrBV8c\n"
+"2 4.60 $31,126.54 13sxMq8mTw7CTSqgGiMPfwo6ZDsVYrHLmR\n"
+"3 2.30 $14,314.05 1HpvvMHcoXQsX85CjTsco5ZAAMoGu2Mze9\n"
+"4 8.80 $54,766.80 3EfRGckBQQuk7cpU7SwatPv8kFD1vALkTU\n"
+"5 12.78 $93,418.99 19BNi5EpZhgBBWAt5ka7xWpJpX2ZWJEYyq\n"
+"6 2.30 $16,812.49 1EKXx73oUhHaUh8JBimtiPGgHfwNmxYKAj\n"
+"\n\n";
document.getElementById('phrase').value = str;
});
function startTest(){
jmcData.prepareData();
document.body.style.cursor = 'wait';
window.setTimeout(makeApiRequest,10);
}
function makeApiRequest() { // regular API request
// get a line from the input textarea
var addrs = jmcData.getAddressList();
if (listUnspentBlockchair(addrs) == true) {
document.getElementById('results').innerHTML = "calculating...\n";
}
else {
document.getElementById('results').innerHTML = "error\n";
}
}
// ===================================================================
// data manipulation functions
var jmcData = {
work: {
accounts: [],
total_btc_lost: 0.0,
total_usd_lost: 0.0,
total_btc_received: 0.0,
total_usd_received: 0.0,
transaction_count: 0,
first_seen_receiving: null,
blockchairStatus: null
},
// grab the static source data required to make the report (repayment_address, etc)
prepareData: function() {
var lines = document.getElementById('phrase').value.split('\n');
var count = 0;
while (lines.length > 0) {
var str = lines[0];
lines.splice(0,1); // remove the first element
var fields = str.split('\t');
if (fields[0].trim() == "ID")
continue; // ignore header line
if (fields.length >= 4) {
var account = {
id: fields[0].trim(),
btc_lost: Number(fields[1].trim().replace(/\,/g, '')),
usd_value: Number(fields[2].trim().replace(/[\$\,]/g, '')),
repayment_address: fields[3].trim(),
btc_received: 0.0,
usd_received: 0.0
};
this.work.accounts.push(account);
this.work.total_btc_lost += account.btc_lost;
this.work.total_usd_lost += account.usd_value;
}
count = count + 1;
}
},
// get list of addresses for which we are reporting on
getAddressList: function() {
var retVal = "";
var all_info = this.work.accounts;
for (const i of all_info) {
if (i.repayment_address != "TBD") {
if (retVal.length > 0) { retVal += ","; }
retVal += i.repayment_address;
}
}
return retVal;
},
// populate the report data with information retrieved from blockchair
setAmounts: function(repayment_address, btc_received, usd_received) {
var all_info = this.work.accounts;
for (const i of all_info) {
if (i.repayment_address == repayment_address) {
i.btc_received = btc_received;
i.usd_received = usd_received;
this.work.total_btc_received += btc_received;
this.work.total_usd_received += usd_received;
}
}
},
// write the report into an HTML table
updateDocument: function(data) {
data.pct_btc_received =
100*(data.total_btc_received / data.total_btc_lost);
data.pct_usd_received =
100*(data.total_usd_received / data.total_usd_lost);
var results = "<table border=1 cellpadding=10>";
results += "<tr>"
+"<td style='background-color:#a0a0a0'>ID</td>"
+"<td style='background-color:#a0a0a0'>Repayment address</td>"
+"<td style='background-color:#a0a0a0'>BTC lost</td>"
+"<td style='background-color:#a0a0a0'>BTC received</td>"
+"<td style='background-color:#a0a0a0'>USD value</td>"
+"<td style='background-color:#a0a0a0'>USD received</td></tr>\n";
var all_info = data.accounts;
for (const i of all_info) {
results += "<tr>"
+"<td>" + i.id.toString() + "</td>"
+"<td>" + i.repayment_address + "</td>"
+"<td>" + i.btc_lost.toString() + "</td>"
+"<td>" + i.btc_received.toString() + "</td>"
+"<td>$" + i.usd_value.toString() + "</td>"
+"<td>$" + Number(i.usd_received).toFixed(2) + "</td>"
+"</tr>\n";
}
results += "<tr>"
+"<td><b>" + "totals" + "</b></td>"
+"<td>" + " " + "</td>"
+"<td><b>" + data.total_btc_lost.toString() + "</b></td>"
+"<td><b>" + data.total_btc_received.toFixed(8) + "</b></td>"
+"<td><b>$" + data.total_usd_lost.toString() + "</b></td>"
+"<td><b>$" + data.total_usd_received.toFixed(2) + "</b></td>"
+"</tr>\n";
results += "<tr>"
+"<td><b>" + " " + "</b></td>"
+"<td>" + " " + "</td>"
+"<td>" + " " + "</td>"
+"<td>" + data.pct_btc_received.toFixed(2) + "%</td>"
+"<td>" + " " + "</td>"
+"<td>" + data.pct_usd_received.toFixed(2) + "%</td>"
+"</tr>\n";
results += "</table>\n";
results += "<br><br>First seen receiving: " + data.first_seen_receiving + "<br>\n";
results += "<br><br>Transaction count: " + data.transaction_count.toString() + "<br>\n";
if (data.blockchairStatus != null) { results += "<br><br>Blockchair status: " + data.blockchairStatus + "<br>\n"; }
document.getElementById('results').innerHTML = results;
document.body.style.cursor = 'auto';
},
}; // end of jmcData
/* retrieve data from blockchair */
function listUnspentBlockchair(addr){
var options = {
url: "https://api.blockchair.com/bitcoin/dashboards/addresses/"+addr,
type: 'GET',
data: {},
dataType: 'json',
successCodes: [304, 401, 403, 404, 500],
statusCode: {},
success: [],
error: [],
complete: []
};
var req = new XMLHttpRequest();
var url = options.url;
req.addEventListener("progress", updateProgress);
req.addEventListener("load", transferComplete);
req.addEventListener("error", transferFailed);
req.addEventListener("abort", transferCanceled);
req.onreadystatechange = function ()
{
if (req.readyState > 3)
{
if (req.responseText.length > 0) {
var data = JSON.parse(req.responseText);
if ((data.context && data.data) && data.context.code =='200'){
var all_info = data.data.addresses;
for (var m in all_info){
var o = all_info[m];
var received = Number(((o.received.toString()*1)/100000000).toFixed(8));
var received_usd = Number(o.received_usd);
jmcData.setAmounts(m, received, received_usd);
}
jmcData.work.first_seen_receiving = data.data.set.first_seen_receiving;
jmcData.work.transaction_count = data.data.set.transaction_count;
}
if ((data.context && typeof data.context.error !== 'undefined')){
jmcData.work.blockchairStatus = data.context.error.toString();
}
jmcData.updateDocument(jmcData.work);
}
}
};
req.open(options.type, url, true);
req.send();
function updateProgress(evt) {
}
function transferComplete(evt) {
}
function transferFailed(evt) {
}
function transferCanceled(evt) {
}
return true;
} // end of listUnspentBlockchair
</script>
</html>