-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript_wallet_nottaxadvice.js
325 lines (311 loc) · 11.4 KB
/
script_wallet_nottaxadvice.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//Resdata is the array of processed values with length equal to the number of transactions.
var rawdata = null;
var resdata = [];
//365 days, 24 hours, 3600 seconds, 1000 milliseconds
let oneyear = 365*24*3600*1000;
let oneday = 24*3600*1000;
document.getElementById('fileInput').onchange = function () {
try{
let filetext = this.value.replace("C:\\fakepath\\", "");
alert(filetext);
document.getElementById('filearea').innerHTML=filetext;
document.getElementById('selfilebtn').style.background='#008000';
document.getElementById('impdatabtn').style.background='#FF0000';
}catch(err){alert(err)};
}
//Import the transaction export csv. Works with standard wallet exports, made for Peercoin but works with Bitcoin.
function impdata()
{
try{
var reader = new FileReader();
reader.onload = function (e) {rawdata = e.target.result};
reader.onloadend = function () {procdata()};
reader.readAsText(document.getElementById('fileInput').files[0]);
document.getElementById('impdatabtn').style.background='#008000';
document.getElementById('datebtn').style.background='#FF0000';
}
catch(err){alert(err);}
}
//Make plotting easier through Plotly
function plotdata(eggs, why, grapharea, mode, ylabel)
{
var trace = {
x: eggs,
y: why,
mode: mode,
type: 'scatter'
};
var data = [trace];
document.getElementById(grapharea).innerHTML='';
try{Plotly.newPlot(grapharea, data, {yaxis:{title:{text: ylabel}}});}
catch(err){Alert(err)};
}
/*
function plotlayout(ptitle, xlabel, ylabel)
{
var layout = {
title: {
text: 'Title',
font: {
family: 'Times New Roman',
size: 24
}
},
xaxis: {
title: {
text: xlabel,
font: {
family: 'Courier New, monospace',
size: 18,
color: '#7f7f7f'
}
}
},
yaxis: {
title: {
text: ylabel,
font: {
family: 'Courier New, monospace',
size: 18,
color: '#7f7f7f'
}
}
}
};
return[layout];
}
*/
//Process the data. This will recreate the resdata.
//resdata = [date, amount, txntag, address, readable date, balance]
//It also feeds the wallet [date,balance] out to the plotter
function procdata(targetaddr = "All")
{
resdata = [];
var txndate = [], txnamount = [];
var rows = rawdata.split("\n");
var dated = [], amnt = [], tag = [], addr = [];
for (var i=1;i<rows.length-1;i++){
let thisrow = rows[i].split('","');
let dating = Date.parse(thisrow[1]);
dated.push(dating);
amnt.push(thisrow[5]);
tag.push(thisrow[2]);
addr.push(thisrow[4]);
}
var j = -1, k = -1, AmtTot = 0;
while(dated[++j]){
if (targetaddr==addr[j] || targetaddr=="All"){
resdata.push([dated[j],amnt[j],tag[j],addr[j]]);
}
}
resdata.sort(function(a,b) {return a[0]-b[0]});
while(resdata[++k]){
dater=resdata[k][0].toString();
let readate = new Date(resdata[k][0]);
AmtTot = AmtTot + parseFloat(resdata[k][1]);
resdata[k].push(readate);
resdata[k].push(AmtTot);
txndate.push(readate);
txnamount.push(AmtTot);
}
//Plot and date ranges
plotdata(txndate,txnamount,'areaone','lines+markers','Balance (# Coins)');
document.getElementById('windowstart').value=resdata[0][4].toISOString().substring(0,10);
endindex = resdata.length-1;
document.getElementById('windowend').value=resdata[endindex][4].toISOString().substring(0,10);
//Setup address select if unpopulated
popaddr=document.getElementById('addressarea').length;
if (popaddr == 1){
var select = document.getElementById('addressarea'), usedaddr = [];
var ii=-1; jj=-1;
while(addr[++jj]){
if (!(usedaddr.includes(addr[jj]))){
usedaddr.push(addr[jj]);
}
}
while(usedaddr[++ii]){
var address = usedaddr[ii];
var addresses = document.createElement("option");
addresses.textContent = address;
addresses.value = address;
select.appendChild(addresses);
}
}
document.getElementById('trgtaddr').innerHTML=targetaddr;
if (targetaddr!='All'){
document.getElementById('addrwarn').innerHTML="Warning: individual addresses in .csv misrepresent send transactions";
}
alert("Processed and Graphed");
}
function impaddr()
{
selectaddr = document.getElementById('addressarea').value;
document.getElementById('impaddrbtn').style.background='#008000';
document.getElementById('datebtn').style.background='#FF0000';
procdata(selectaddr);
}
//Calculate average balance, stake minted, annualized interest over date window, and total reward as percentage of balance.
function calcintrst(mindate,maxdate)
{
var sum = 0, reward = 0, len=resdata.length, onswitch = 0, i=-1;
while(resdata[++i]){
if (resdata[i][0]>mindate && resdata[i][0]<maxdate) {
if (onswitch == 0 && i!=0) {
sum = sum + resdata[i-1][5]*(resdata[i][0]-mindate);
} else {
if (i+1==len || resdata[i+1][5]>maxdate) {
sum = sum + resdata[i][5]*(maxdate-resdata[i][0]);
} else {
sum = sum + resdata[i][5]*(resdata[i+1][0]-resdata[i][0]);
}
}
if (resdata[i][2] == "Mint by stake")
{
reward = reward + parseFloat(resdata[i][1]);
}
onswitch = 1;
}
}
let avg = sum/(maxdate-mindate);
let interest=100*reward*oneyear/sum;
let rewardpercent = 100*reward*(maxdate-mindate)/sum;
return[avg, reward, interest, rewardpercent];
}
function posreward(mindate,maxdate)
{
var posdate = [], posreward = [], posdatediff = [], k=-1, i=-1;
while(resdata[++i]){
if (resdata[i][0]>mindate && resdata[i][0]<maxdate) {
if (resdata[i][2] == "Mint by stake")
{
k=k+1;
posdate.push(resdata[i][4]);
posreward.push(parseFloat(resdata[i][1]));
if (k > 0) {
posdatediff.push((posdate[k]-posdate[k-1])/oneday);
} else {
posdatediff[0] = 0;
}
}
}
}
return[posdate, posreward, posdatediff];
}
//Set the bar for Continuous Minting as earning 0.75% average interest before v0.9 and 3.75% after.
function expavgint(min,max)
{
var cnfbar = 0.75;
var switchdate = new Date("2020-06-05T01:00:00Z");
var binter = 1;
var ainter = 5;
var astart = 0;
var tintage = 0;
if (min<switchdate){
tintage = tintage+binter*(switchdate-min);
astart = switchdate;
} else {astart = min;}
if (switchdate<max){
tintage = tintage+ainter*(max-astart);
} else {tintage = tintage+binter*(max-astart);}
var tinter = cnfbar*tintage/(max-min);
return tinter;
}
//Use the date window to spit out averages.
function datedata()
{
var mind = Date.parse(document.getElementById('windowstart').value);
var maxd = Date.parse(document.getElementById('windowend').value);
let avgint = calcintrst(mind,maxd);
var xint = [], yint2 = [], yint3 = [];
document.getElementById('avg').innerHTML=avgint[0];
document.getElementById('stake').innerHTML=avgint[1];
document.getElementById('interest').innerHTML=avgint[2];
if (avgint[2] > expavgint(mind,maxd)){
document.getElementById('prediction').innerHTML="You were a CONTINUOUS minter during this period";
} else {
document.getElementById('prediction').innerHTML="You were a PERIODIC minter during this period";
}
for (let i=mind;i<maxd; i=i+oneday){
let annualized = calcintrst(i-oneyear,i);
let day = new Date(i);
xint.push(day);
yint2.push(annualized[2]);
let cumint = calcintrst(mind,i);
yint3.push(cumint[3]);
}
//plots 4 and 5 (mint events)
var xintpos = [], yint4 = [], yint5 = [];
graphreward = posreward(mind,maxd);
xintpos = graphreward[0];
yint4 = graphreward[1];
yint5 = graphreward[2];
plotdata(xint,yint2,'areatwo','lines+markers','Annualized Interest (%)');
plotdata(xint,yint3,'areathree','lines+markers','Percentage of Balance Minted (%)');
plotdata(xintpos,yint4,'areafour','markers','Mint Reward (# Coins)');
plotdata(xintpos,yint5,'areafive','markers','Time Between Mint Events (Days)');
document.getElementById('datebtn').style.background='#008000';
alert("Date Window Processed and Graphed");
}
document.getElementById('priceInput').onchange = function () {
try{
let pfiletext = this.value.replace("C:\\fakepath\\", "");
alert(pfiletext);
document.getElementById('pricefilearea').innerHTML=pfiletext;
document.getElementById('pricefilebtn').style.background='#008000';
document.getElementById('imppricebtn').style.background='#FF0000';
}catch(err){alert(err)};
}
function impprice()
{
rawpricedata=null;
try{
var preader = new FileReader();
preader.onload = function (e) {rawpricedata = e.target.result};
preader.onloadend = function () {procprice(rawpricedata)};
preader.readAsText(document.getElementById('priceInput').files[0]);
document.getElementById('imppricebtn').style.background='#008000';
}
catch(err){alert(err);}
}
function procprice(rawpricedata)
{
var prows = rawpricedata.split("\n");
var pdated = [], pamnt = [];
for (let p=1;p<prows.length;p++){
let pthisrow = prows[p].split(',');
let pdating = new Date(pthisrow[0].replaceAll('-','/'));
pdated.push(pdating);
pamnt.push(pthisrow[1]);
}
priceexp(pdated,pamnt)
}
function priceexp(pdated,pamnt)
{
var stakedate = []; stakeprice = [];
let i=-1;
var mind = Date.parse(document.getElementById('windowstart').value);
var maxd = Date.parse(document.getElementById('windowend').value);
var pcsv = "Date,Amount (USD)\n";
while(resdata[++i]){
if (resdata[i][0]>mind && resdata[i][0]<maxd) {
if (resdata[i][2] == "Mint by stake")
{
stakedate.push(resdata[i][4]);
let pindex=pdated.findIndex(function(ptime) {
return ptime > resdata[i][4];
});
stakeprice.push(parseFloat(resdata[i][1])*pamnt[pindex]);
let stakeinfo = [];
stakeinfo.push(resdata[i][4]);
stakeinfo.push(parseFloat(resdata[i][1])*pamnt[pindex]);
pcsv += stakeinfo.join(',');
pcsv += "\n";
}
}
}
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(pcsv);
hiddenElement.target = '_blank';
hiddenElement.download = 'mintevents.csv';
hiddenElement.click();
}