-
Notifications
You must be signed in to change notification settings - Fork 6
/
datahelper.js
executable file
·309 lines (250 loc) · 8.53 KB
/
datahelper.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
function getData(url, success, nocache){
//if(typeof(Storage) !== undefined && localStorage.fastMode == "true"){
console.log("Fast mode");
url = "https://raw.githubusercontent.com/daostats/daostats.github.io/master/" + url + ".js";
var updateCycle = Math.floor((new Date().getMinutes()-1)/5);
if(nocache != true) url += "?c=" + updateCycle;
//}
//else url = url + ".js";
console.log("Getting data at " + url);
$.getJSON(url, success) .done(function() {
console.log( "second success" );
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("error " + textStatus);
console.log("incoming Text " + jqXHR.responseText);
success([], "error");
})
.always(function() {
console.log( "complete" );
});
/*
$.ajax({
url: url,
cache: false
})
.done(function( str ) {
console.log(str);
success($.parseJSON(str));
});*/
}
function setElemText(elem, val){
$("#"+elem).html(val);
}
function applyData(data){
console.log(data);
$.each(data, function(key, val){
$("#"+key+"-js").html(val);
})
}
function dictSlice(dict, elems){
out = {}
for(e in elems)
{
out[elems[e]] = dict[elems[e]];
}
return out;
}
function dictToArray(dict, elems){
out = [];
for (var i = 0; i < elems.length; i++) {
out.push(dict[elems[i]]);
}
return out;
}
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
function formatFloat(f, e){
if (e === undefined)
return Math.floor(f*10000)/10000;
else{
m = Math.pow(10, e);
return Math.floor(f*m)/m;
}
}
function baseToDAO(value){
daos = value * (1e-16);
parts = (daos).toExponential().split("e");
m = parseFloat(parts[0]);
e = parseFloat(parts[1]);
if (e < -3) return String.format("{0}×10<sup>{1}</sup> DAO", +m.toFixed(2), e);
else if(e < 0) return String.format("{0} DAO", +daos.toFixed(3));
else if(e < 3) return String.format("{0} DAO", +daos.toFixed(4));
else if(e < 6) return String.format("{0} KDAO", +(daos/1000).toFixed(3));
else return String.format("{0} MDAO", +(daos/1000000).toFixed(3));
}
function baseToDAO2(value, suffix){
if(suffix === undefined) suffix = "";
daos = value * (1e-16);
parts = (daos).toExponential().split("e");
m = parseFloat(parts[0]);
e = parseFloat(parts[1]);
if (e < -3) return String.format("{0}×10<sup>{1}</sup> " + suffix, +m.toFixed(2), e);
else if(e < 0) return String.format("{0} " + suffix, +daos.toFixed(3));
else if(e < 3) return String.format("{0} " + suffix, +daos.toFixed(4));
else if(e < 6) return String.format("{0} " + suffix, (Math.round(daos)).toLocaleString());
else return String.format("{0} " + suffix, (Math.round(daos)).toLocaleString());
}
function baseToETH(value, base){
if (base == "eth"){
value = value * (1e18);
}
if (value < 1e3) return value + " Wei";
if (value < 1e6) return formatFloat(value*(1e-3)) + " KWei";
if (value < 1e9) return formatFloat(value*(1e-6)) + " MWei";
if (value < 1e12) return formatFloat(value*(1e-9)) + " GWei";
if (value < 1e15) return formatFloat(value*(1e-12)) + " szabo";
if (value < 1e18) return formatFloat(value*(1e-15)) + " finny";
if (value < 1e21) return formatFloat(value*(1e-18)) + " ether";
if (value < 1e24) return formatFloat(value*(1e-21)) + " Kether";
else return formatFloat(value*(1e-24)) + " Mether";
}
function baseToETH2(value, base){
if(value == 0) return "0 ether";
if (base == "eth"){
value = value * (1e18);
}
if (value < 1e12) return value.toLocaleString() + " Wei";
if (value < 1e15) return (Math.round(100*value*(1e-12))/100).toLocaleString() + " szabo";
if (value < 1e18) return (Math.round(100*value*(1e-15))/100).toLocaleString() + " finny";
else return (Math.round(100*value*(1e-18))/100).toLocaleString() + " ether";
}
function base2ETH2(a,b){
return baseToETH2(a,b);
}
function preferredAccountExplorer(){
if(typeof(Storage) !== undefined){
var exp = localStorage.explorer;
if (exp == "chain" || exp == "undefined" || exp == undefined) return "https://etherchain.org/account/"
else if(exp == "camp") return "https://live.ether.camp/account/"
else if(exp == "scan") return "https://etherscan.io/address/"
else return localStorage.explorerAccount;
}
else return "https://etherchain.org/account/"
}
function preferredBlockExplorer(){
if(typeof(Storage) !== undefined){
var exp = localStorage.explorer;
console.log(localStorage.explorer);
if (exp == "chain" || exp == "undefined" || exp == undefined) return "https://etherchain.org/block/"
else if(exp == "camp") return "https://live.ether.camp/block/"
else if(exp == "scan") return "https://etherscan.io/block/"
else return localStorage.explorerBlock;
}
else return "https://etherchain.org/block/"
}
function preferredTxExplorer(){
if(typeof(Storage) !== undefined){
var exp = localStorage.explorer;
console.log(localStorage.explorer);
if (exp == "chain" || exp == "undefined" || exp == undefined) return "https://etherchain.org/tx/"
else if(exp == "camp") return "https://live.ether.camp/transaction/"
else if(exp == "scan") return "https://etherscan.io/tx/"
else return localStorage.explorerBlock;
}
else return "https://etherchain.org/tx/"
}
function hexLink(){
var exp = preferredAccountExplorer();
$("a.hexLink").each(function(){
$(this).prop("href", exp + $(this).text());
});
}
var nullAddr = "0x0000000000000000000000000000000000000000";
function accLink(){
$("a.accLink").each(function(){
hash = $(this).text()
if($(this).text() == nullAddr){
$(this).text("The DAO token sale");
}
else if($(this).hasClass("shrink")){
$(this).text(hash.substr(0, 16) + "...");
}
$(this).prop("href", "tokenbalance.html#" + hash);
});
}
function blockLink(){
var exp = preferredBlockExplorer();
$("a.blockLink").each(function(){
$(this).prop("href", exp + $(this).text());
});
}
function propLink(){
$("a.propLink").each(function(){
$(this).prop("href", "proposal.html#" + $(this).text());
});
$("a.propLinkData").each(function(){
$(this).prop("href", "proposal.html#" + $(this).attr("data-proposal"));
});
}
function txLink(){
var exp = preferredTxExplorer();
$("a.txLink").each(function(){
hash = $(this).text();
$(this).prop("href", exp + $(this).text());
if($(this).hasClass("shrink")){
$(this).text(hash.substr(0, 42) + "...");
}
});
}
function ethSupply(block){
supAtOrigin = 79654622.06;
origin = 1447403;
ethPerBlock = 5;
return supAtOrigin + (block - origin)*ethPerBlock;
}
function cleanGraphClass(cname){
return cname.split(' ').join('');
}
function padTime(m){
return ('0' + m).slice(-2);
}
function binaryIndexOf(searchElement, elementName) {
'use strict';
var minIndex = 0;
var maxIndex = this.length - 1;
var currentIndex;
var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = this[currentIndex][elementName];
if (currentElement < searchElement) {
minIndex = currentIndex + 1;
}
else if (currentElement > searchElement) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return minIndex;
}
function SMHD(t){
t = Math.round(t);
var s;
var f = t;
if(t<60){
s = t + " second";
}
else if(t < 60*60){
f = Math.round(t/60);
s = f + " minute";
}
else if(t < 60*60*24){
f = Math.round(t/(60*60));
s = f + " hour";
}
else{
f = Math.round(t/(60*60*24))
s = f + " day"
}
if(f != 1)s += "s";
return s;
}