-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
619 lines (532 loc) · 21 KB
/
popup.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
function tabAssets_onclick() {
}
function tabPools_onclick() {
}
function btnRefresh_onClick() {
force_refresh_tokens();
}
function btnDetachWindow_onClick() {
closeWindow();
window.open("popup.html?detached=true", "Osmosis Companion", "popup,location=off,height=535,width=645");
}
function closeWindow() {
window.close();
}
function btnSettings_onClick() {
chrome.runtime.openOptionsPage();
}
// check if tokens are older than 5 minutes, then
function refresh_assets_if_old() {
// cancel timeout
window.clearTimeout(window.refresh_timeout);
var is_tokens_old = false;
var is_assetlist_old = false;
var is_wallet_old = false;
var assets = {};
// call once to check current values
companion.assets.get()
.then((assets_orig) => {
// check if any of the asset objects need to be refreshed
// is token prices older than 5 minutes?
is_tokens_old = !(assets_orig.tokens && assets_orig.tokens.timestamp && (Date.now() - assets_orig.tokens.timestamp) < 10000);
// is assetlist older than 6 hours?
is_assetlist_old = !(assets_orig.assetlist && assets_orig.assetlist.timestamp && (Date.now() - assets_orig.assetlist.timestamp) < 240000);
// is wallet data older than 5 seconds?
// TODO: verify if assets_orig.wallet counts as "set"
if (assets_orig.wallet) { // only check if wallet has been set!
is_wallet_old = (assets_orig.wallet.timestamp && (Date.now() - assets_orig.wallet.timestamp) > 5000);
}
if (is_tokens_old || is_assetlist_old || is_wallet_old) {
companion.assets.get({
refresh_tokens: is_tokens_old,
refresh_assetlist: is_assetlist_old,
refresh_wallet: is_wallet_old
}).then((assets_new) => {
render_assets(assets_new);
update_lastRefreshed();
// tick
window.refresh_timeout = window.setTimeout(refresh_assets_if_old, 1000);
});
} else {
if (!is_assets_rendered()) {
render_assets(assets_orig);
}
// TODO: check if wallet link show/hide is being called here:
update_lastRefreshed();
// tick
window.refresh_timeout = window.setTimeout(refresh_assets_if_old, 1000);
}
});
}
function force_refresh_tokens() {
// cancel timeout
window.clearTimeout(window.refresh_timeout);
// show loading masks
maskElement(document.getElementById("btnRefresh"));
maskElement(document.getElementById("tab_myAssets"), "Fetching prices...", 95);
// get token info from external API...
return companion.assets.get({
refresh_tokens: true,
refresh_wallet: true,
refresh_assetlist: true /*'only_when_denoms_missing'*/
})
.then((assets) => {
console.log('%c Rendering prices...', 'background-color:#848;color:#fff');
// UI
// render_tokens(assets.tokens.data);
render_assets(assets);
// order of rows of table
sort_tokens(getCurrentSortOptions());
// set "last refreshed..."
update_lastRefreshed();
// remove loading masks
unmaskElement(document.getElementById("btnRefresh"));
unmaskElement(document.getElementById("tab_myAssets"));
})
.finally(() => {
// restart timer
window.refresh_timeout = window.setTimeout(refresh_assets_if_old, 1000);
});
}
function render_wallet_balances(arrWalletBalances) {
// iterate table of tokens
var total = 0;
arrWalletBalances.forEach((walletBalance, i) => {
// if there is a balance, update it, else set to zero.
// var row = document.querySelector("[data-ticker='" + walletBalance.symbol + "']");
var row = document.querySelector("[data-denom='" + walletBalance.denom + "']");
var value = walletBalance.amount * row.dataset.price;
// document.querySelector("[data-ticker='" + walletBalance.symbol + "'] .td-balance p").innerHTML = format_decimals(walletBalance.amount);
document.querySelector("[data-denom='" + walletBalance.denom + "'] .td-balance p").innerHTML = format_decimals(walletBalance.amount);
// document.querySelector("[data-ticker='" + walletBalance.symbol + "'] .td-value p").innerHTML = "$" + value.toFixed(2).toLocaleString('en-US');
document.querySelector("[data-denom='" + walletBalance.denom + "'] .td-value p").innerHTML = "$" + value.toFixed(2).toLocaleString('en-US');
row.dataset.balance = walletBalance.amount;
row.dataset.value = value;
total += value;
});
document.querySelector(".table-footer .total").innerHTML = "$" + total.toFixed(2).toLocaleString('en-US');
}
function render_tokens(tokens, change, assetlist) {
// sort default by liquidity
tokens.sort((a, b) =>
(parseFloat(a.liquidity) < parseFloat(b.liquidity)) ? 1 : -1
);
// remove previously rendered table's DOM
var assets_tbody = document.getElementById("assets_tbody");
assets_tbody.innerHTML = "";
// get HTML template for table row (for each asset)
var template_asset_tr = document.getElementById("template_asset_tr");
// iterate tokens object for dynamically creating each row
tokens.forEach((token, i) => {
// create row for token from template
var rowFragment = template_asset_tr.content.cloneNode(true);
// set values
var price = parseFloat((Math.round((token.price + Number.EPSILON) * 1000) / 1000).toFixed(5)); //parseFloat(price);
// var price = parseFloat((Math.round((token.price + Number.EPSILON) * 10000)).toFixed(5)); //parseFloat(price);
// console.log(price);
// var pricePretty = price.toFixed(2);
rowFragment.querySelector(".td-price p").innerHTML = "$" + price.toLocaleString('en-US');
//tooltips:
// + "<span class='button-alert'> 🔔</span> "
var tooltip = "<div class='tooltiptext tooltip-asset'><div class='info'><div class='liquidity'><span class='title'>Liquidity</span>$" + parseInt(token.liquidity).toLocaleString('en-US') + "</div><div class='volume'><span class='title'>24h Volume</span>$" + parseInt(token.volume_24h).toLocaleString('en-US') + "</div><div class='link'>Click to go to: https://info.osmosis.zone/token/" + token.symbol + "</div></div></div>";
rowFragment.querySelector(".td-name p").innerHTML = token.name + " <br><span class='ticker'>" + token.symbol + "</span>" + tooltip;
//tooltips:
rowFragment.querySelector(".td-rank").innerHTML = "<span class='rank'>" + (i + 1) + "</span>";
// rowFragment.querySelector("img").src = "https://info.osmosis.zone/assets/" + token.symbol.toLowerCase() + ".png";
var change_1h = 0,
change_24h = 0,
change_7d = 0;
// get asset img src from info.osmosis.zone, else get from assetlist
var assetlist_src = "";
for (var x = 0; x < assetlist.length; x++) {
if (assetlist[x].base == token.denom) {
if (assetlist[x].coingecko_id) {
change.forEach((item, i) => {
if (item.id == assetlist[x].coingecko_id) {
change_1h = parseFloat((Math.round((item.price_change_percentage_1h_in_currency + Number.EPSILON) * 1000) / 1000).toFixed(1));
change_24h = parseFloat((Math.round((item.price_change_percentage_24h_in_currency + Number.EPSILON) * 1000) / 1000).toFixed(1));
change_7d = parseFloat((Math.round((item.price_change_percentage_7d_in_currency + Number.EPSILON) * 1000) / 1000).toFixed(1));
if (change_1h != 0) {
rowFragment.querySelector(".td-change-1h").classList.add((change_1h > 0 ? "pump" : ((change_1h < 0) ? "dump" : "")));
}
if (change_24h != 0) {
rowFragment.querySelector(".td-change-24h").classList.add((change_24h > 0 ? "pump" : ((change_24h < 0) ? "dump" : "")));
}
if (change_7d != 0) {
rowFragment.querySelector(".td-change-7d").classList.add((change_7d > 0 ? "pump" : ((change_7d < 0) ? "dump" : "")));
}
rowFragment.querySelector(".td-change-1h p").innerHTML = change_1h.toLocaleString('en-US') + "%";
rowFragment.querySelector(".td-change-24h p").innerHTML = change_24h.toLocaleString('en-US') + "%";
rowFragment.querySelector(".td-change-7d p").innerHTML = change_7d.toLocaleString('en-US') + "%";
}
});
}
if (assetlist[x].logo_URIs.png) {
assetlist_src = assetlist[x].logo_URIs.png;
} else if (assetlist[x].logo_URIs.svg) {
assetlist_src = assetlist[x].logo_URIs.svg;
}
}
}
rowFragment.querySelector("img").src = assetlist_src;
// add row to table body
assets_tbody.appendChild(rowFragment);
var row = document.querySelector("#assets_tbody .inner-tr:last-child");
// image error handling
document.querySelector("#assets_tbody .inner-tr:last-child img").onerror = img_onerror;
// set dataset attribute for price:
row.dataset.rank = (i + 1);
row.dataset.denom = token.denom;
row.dataset.ticker = token.symbol;
row.dataset.name = token.name;
row.dataset.price = token.price;
row.dataset.change_1h = change_1h;
row.dataset.change_24h = change_24h;
row.dataset.change_7d = change_7d;
row.dataset.liquidity = token.liquidity;
row.dataset.volume_24h = token.volume_24h;
});
// external links for assets
document.querySelectorAll("#assets_tbody .inner-tr").forEach((tr, i) => {
tr.querySelector(".td-name p").addEventListener("click", () => {
window.open("https://info.osmosis.zone/token/" + tr.dataset.ticker);
});
});
}
function img_onerror(e) {
this.onerror = null;
this.src = 'images/default.png';
}
function render_assets(assets) {
maskElement(document.getElementById("tab_myAssets"), "Rendering assets...", 90);
// check if wallet is returned, else keep it blank to avoid error
var raw_balances = (assets.wallet && assets.wallet.data) ? assets.wallet.data : [];
var change = (assets.change && assets.change.data) ? assets.change.data : [];
var assetlist = assets.assetlist.data.assets;
var tokens = assets.tokens.data;
render_tokens(assets.tokens.data, change, assetlist);
var arrWalletBalances = [];
// loop user wallet assets, lookup the denoms against the assetlist, and get corresponding asset symbols
var isAssetFoundInAssetlist = false; // used to check if asset is found in assetlist. if not, then update assetlist
var isAssetFoundInTokenArray = false;
raw_balances.forEach((balance, i) => {
isAssetFoundInAssetlist = false; // reset flag
// dont bother with gamm tokens
if (balance.denom.indexOf("gamm/pool/") == -1) {
assetlist.forEach((asset, i) => {
// make sure it's in token list (i.e. only show ones in info.osmosis.zone)
// (this is a workaround until there's a better API to get price, liquidity and volume data...)
isAssetFoundInTokenArray = false;
tokens.forEach((token, i) => {
if (token.denom == asset.base) {
// if (token.symbol == asset.symbol) { // replaced with denom lookup, due to mismatch with wrapped symbols
isAssetFoundInTokenArray = true;
}
});
if (balance.denom == asset.base && isAssetFoundInTokenArray) {
isAssetFoundInAssetlist = true;
//// console.log('%c Found asset!', 'background-color:#088;color:#fff');
//// console.log(asset.symbol);
// get exponent (for decimal point)
let exponent = 0;
asset.denom_units.forEach((denom_unit, i) => {
// cater for if "denom" or "base" is used incorrectly in assetlist
if (denom_unit.denom.toLowerCase() == asset.display.toLowerCase() || denom_unit.denom.toLowerCase() == asset.base.toLowerCase()) {
exponent = denom_unit.exponent;
}
});
// build wallet balances
arrWalletBalances.push({
"symbol": asset.symbol,
"denom": balance.denom,
"amount": (exponent == 0) ? balance.amount * 1 : balance.amount / (10 ** exponent)
});
////console.log(balance.amount);
////console.log((10 ** exponent));
////console.log(balance.amount / (10 ** exponent));
}
});
}
});
render_wallet_balances(arrWalletBalances);
// Sort tokens
sort_tokens(getCurrentSortOptions());
// unmask
unmaskElement(document.getElementById("btnRefresh"));
unmaskElement(document.getElementById("tab_myAssets"));
}
function is_assets_rendered() {
if (document.getElementById("assets_tbody").innerHTML == "") {
return false;
}
return true;
}
function update_lastRefreshed_tokens(age) {
var seconds = Math.floor(age / 1000);
var elapsedText = "",
em = 0,
es = 0;
if (seconds / 60 > 1) {
em = parseInt(seconds / 60);
es = seconds % 60;
elapsedText = em + "m " + es + "s";
} else {
elapsedText = seconds + "s";
}
document.getElementById("age_tokens").innerHTML = "Prices last refreshed: " + elapsedText + "<span class='tooltiptext tooltip-bottom-right'>Refresh Now</span>";
}
function update_lastRefreshed_wallet(age) {
if (!age) {
document.getElementById("age_wallet").innerHTML = "";
document.getElementById("age_wallet").classList.add("hidden");
document.getElementById("link_options").classList.remove("hidden");
return;
}
if (age) {
var seconds = Math.floor(age / 1000);
var elapsedText = "",
em = 0,
es = 0;
if (seconds / 60 > 1) {
em = parseInt(seconds / 60);
es = seconds % 60;
elapsedText = em + "m " + es + "s";
} else {
elapsedText = seconds + "s";
}
document.getElementById("age_wallet").innerHTML = "Wallet last refreshed: " + elapsedText + "<span class='tooltiptext tooltip-bottom-right'>Refresh Now</span>";
document.getElementById("age_wallet").classList.remove("hidden");
document.getElementById("link_options").classList.add("hidden");
}
}
function update_lastRefreshed() {
companion.assets.last_updated()
.then((age) => {
update_lastRefreshed_tokens(age.tokens);
update_lastRefreshed_wallet(age.wallet);
});
}
function updateLastRefreshed_old(seconds) {
var seconds_remaining = 300 - seconds;
var remainingText = "",
rm = 0,
rs = 0;
// var elapsedText = "",
// em = 0,
// es = 0;
//
// if (seconds / 60 > 1) {
// em = parseInt(seconds / 60);
// es = seconds % 60;
// elapsedText = em + "m " + es + "s";
// } else {
// elapsedText = seconds + "s";
// }
if (seconds_remaining > 60) {
rm = parseInt(seconds_remaining / 60);
rs = seconds_remaining % 60;
remainingText = rm + "m " + rs + "s";
} else {
remainingText = seconds_remaining + "s";
}
document.getElementById("btnRefresh_lastRefreshed").innerHTML = "Refreshing price data in " + remainingText + "<span class='tooltiptext tooltip-bottom-right'>Refresh Now</span>";
}
// Sorting:
function btnSort_onClick(el) {
var sortInfo = update_sortButtons(el);
sort_tokens(sortInfo);
// save sort options:
companion.settings.default_sort.set(sortInfo);
}
function getCurrentSortOptions() {
var options = {};
var sortBy = "rank";
var sortOrder = "ascending";
document.querySelectorAll(".sort-button").forEach((sortButton, i) => {
if (sortButton.classList.contains("ascending")) {
sortBy = sortButton.dataset.sort;
sortOrder = "ascending";
} else if (sortButton.classList.contains("descending")) {
sortBy = sortButton.dataset.sort;
sortOrder = "descending";
}
});
options.sortBy = sortBy;
options.sortOrder = sortOrder;
// var sortBy = document.querySelectorAll(".sort-button").dataset.sort;
return options;
}
function setSortFromOptions(options) {
document.querySelectorAll(".sort-button").forEach((sortButton, i) => {
sortButton.classList.remove("ascending", "descending");
if (sortButton.dataset.sort == options?.sortBy) {
sortButton.classList.add(options.sortOrder);
return;
}
});
}
/**
* Sorts the rows of the Assets table
* @param {Object} options optional values for "sortBy" and "order". defaults to sort by name, descending
*/
function sort_tokens(options = {
sortBy: "name",
sortOrder: "descending"
}) {
var rows = document.getElementById("assets_tbody");
[...rows.children]
.sort((a, b) =>
compareFields(a, b, options)
)
.forEach(node => rows.appendChild(node));
}
/**
* helper function for sorting
*/
function compareFields(a, b, options) {
// a.dataset[options.sortBy].toLowerCase() > b.dataset[options.sortBy].toLowerCase()
var compareA, compareB;
switch (options.sortBy) {
case "rank":
case "liquidity":
case 'price':
case 'change_1h':
case 'change_24h':
case 'change_7d':
case 'balance':
case 'value':
compareA = parseFloat(a.dataset[options.sortBy]);
compareB = parseFloat(b.dataset[options.sortBy]);
break;
case "name":
compareA = a.dataset[options.sortBy].toLowerCase();
compareB = b.dataset[options.sortBy].toLowerCase();
break;
default:
break;
}
if (options.sortOrder == "descending") {
return (compareA < compareB) ? 1 : -1;
}
return (compareA > compareB) ? 1 : -1;
}
/**
* function to optionally return decimals according to input value
*/
function format_decimals(val) {
// no decimals
if (Math.floor(val) == val) {
return val;
}
// tiny
if (val < 0.001) {
return Number(val).toExponential();
}
// all others with decimal points, format and trim trailing zeroes
return parseFloat(Number(val).toFixed(3))
}
function update_sortButtons(el) {
// get state:
var sortBy = el.dataset.sort;
var newSortOrder = "descending";
// NB: default-ascending indicates behaviour when you click the column,
// not to be confused with default_sort from settings
if (el.classList.contains("default-ascending")) {
newSortOrder = "ascending";
}
if (el.classList.contains('descending')) {
newSortOrder = "ascending";
} else if (el.classList.contains('ascending')) {
newSortOrder = "descending";
}
// clear all
document.querySelectorAll(".sort-button").forEach((button) => {
button.classList.remove("ascending", "descending");
});
// apply new order
el.classList.add(newSortOrder);
return {
sortBy: sortBy,
sortOrder: newSortOrder
};
}
function maskElement(el, maskText = null, progress = null) {
// "grey out" / disable element
el.classList.add("masked");
// if progress is set, then set value and show
if (progress) {
document.querySelector("#mask progress").value = progress;
document.querySelector("#mask progress").innerHTML = progress;
document.querySelector("#mask progress").classList.add("show");
} else {
document.querySelector("#mask progress").classList.remove("show");
}
// if maskText is set, then set value and show
if (maskText) {
document.querySelector("#mask span").innerHTML = maskText;
document.querySelector("#mask span").classList.add("show");
} else {
document.querySelector("#mask span").classList.remove("show");
}
// show the mask
if (progress || maskText) {
document.getElementById("mask").classList.add("show-mask");
}
}
function findGetParameter(parameterName) {
var result = null,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function(item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}
function unmaskElement(el) {
el.classList.remove("masked");
document.querySelector("#mask span").classList.remove("show");
document.querySelector("#mask progress").classList.remove("show");
document.querySelector("#mask progress").value = 0;
document.querySelector("#mask progress").innerHTML = "0%";
document.getElementById("mask").classList.remove("show-mask");
}
document.addEventListener('DOMContentLoaded', function() {
if (findGetParameter("detached")) {
document.getElementById("btnDetachWindow").style.display = "none";
document.getElementById("aOpenInTab").style.display = "";
window.resizeTo(535, 645);
// window.resizeTo(555,700);
}
// refresh button
document.getElementById("btnRefresh").addEventListener("click", btnRefresh_onClick);
// detatch window button
document.getElementById("btnDetachWindow").addEventListener("click", btnDetachWindow_onClick);
// open in a tab <a>
document.getElementById("aOpenInTab").addEventListener("click", closeWindow);
// settings button
// document.getElementById("btnSettings").addEventListener("click", btnSettings_onClick);
document.getElementById("age_tokens").addEventListener("click", btnRefresh_onClick);
document.getElementById("age_wallet").addEventListener("click", btnRefresh_onClick);
// column sorting
companion.settings.default_sort.get()
.then((res) => {
setSortFromOptions(res.default_sort);
});
document.querySelectorAll(".sort-button").forEach((button) => {
button.addEventListener('click', () => {
// sort(button);
btnSort_onClick(button);
});
});
maskElement(document.getElementById("tab_myAssets"), "Loading assets...", 85);
refresh_assets_if_old();
window.refresh_timeout = window.setTimeout(refresh_assets_if_old, 1000);
});
//
// window.onload = function() {
// refresh_assets_if_old();
// }
// TODO: make sure calls arent repeated if promises aren't resolved yet!