-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript_chainz.js
192 lines (183 loc) · 6.69 KB
/
script_chainz.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
//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)};
}
//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()
{
resdata = [];
var txndate = [], txnamount = [];
var rows = rawdata.split("\n");
var dated = [], amnt = [], tag = [], txid = [];
for (var i=2;i<rows.length;i++){
let thisrow = rows[i].split(';');
let dating = Date.parse(thisrow[2]);
dated.push(dating);
amnt.push(thisrow[4]);
tag.push(thisrow[3]);
txid.push(thisrow[0]);
}
var j = -1, k = -1, AmtTot = 0;
while(dated[++j]){
resdata.push([dated[j],amnt[j],tag[j],txid[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
alert("Processed and Graphed");
}
//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] == "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] == "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");
}