-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompanion.js
620 lines (562 loc) · 17.4 KB
/
companion.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
620
// helper function definition:
companion = {
/* all functions related to assets tab */
assets: {
coingecko: {
change: {
ls: {
get: async function() {},
set: async function(change) {}
},
get: function() {}
}
},
/* info.osmosis.zone listed token data */
tokens: {
/* localstorage functions */
ls: {
get: async function() {},
set: async function(tokens) {}
},
/* API functions */
api: {
get: async function() {}
},
/* get tokens from ls, or from the API if refresh = true */
get: async function(options = {
refresh: false
}) {}
},
/* github assetlist metadata */
assetlist: {
/* localstorage functions */
ls: {
get: async function() {},
set: async function(tokens) {}
},
/* API functions */
api: {
get: async function() {}
},
/* get assetlist from ls, or from the API if refresh = true */
get: async function(options = {
refresh: false
}) {}
},
/* user osmosis wallet */
wallet: {
/* localstorage functions */
ls: {
get: async function() {},
set: async function(wallet) {}
},
/* API functions */
api: {
get: async function() {}
},
/* get assetlist from ls, or from the API if refresh = true */
get: async function(options = {
refresh: false
}) {},
address: {
sync: {
get: async function() {},
set: async function(address) {}
}
}
},
/* get asset data */
get: async function(options = {
refresh: false,
refresh_wallet: false,
refresh_tokens: false,
refresh_assetlist: false
}) {},
last_updated: async function() {}
},
zones: {
/* localstorage functions */
ls: {
get: async function() {},
set: async function(tokens) {}
},
/* API functions */
api: {
get: async function() {}
},
/* get tokens from ls, or from the API if refresh = true */
get: async function(options = {
refresh: false
}) {}
},
ui: {
render_assets: async function(assets = null) {},
render_dashboard: async function(data = null) {},
render_settings: async function(settings = null) {}
},
derivation_lookup: async function(address) {},
settings: {
default_sort: {
get: async function() {},
set: async function() {}
}
}
};
/* COINGECKO */
{
/*
get coingecko 1 h, 24 h, 7 d change
for all tokens in assetlist(provided that they have a coingecko_id
*/
companion.assets.coingecko.change.get = async function(options = {
refresh: false
}) {
var isChangeLoaded = false;
await companion.assets.coingecko.change.ls.get()
.then((res) => {
change = res.change;
// flag if it's been stored
if (change && change.timestamp) {
isChangeLoaded = true;
}
});
// if refresh is forced OR coingecko.change.ls is empty
if (!isChangeLoaded || options.refresh) {
var ids = "";
var arr_request_ids = []; // coingecko API results per page doesnt work correctly, and only ever returns up to 50 tokens
await companion.assets.assetlist.get()
.then((res) => {
let idsCount = 0;
res.assetlist.data.assets.forEach((item, i) => {
if (idsCount > 0) {
ids += ",";
}
ids += item.coingecko_id;
idsCount++;
if (idsCount == 50) {
arr_request_ids.push(ids);
ids = "";
idsCount = 0;
}
});
if (ids != "") {
arr_request_ids.push(ids);
}
});
let fetch_promises = [];
for (var request_ids of arr_request_ids) {
fetch_promises.push(fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=' + request_ids + '&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=1h%2C24h%2C7d%2C30d%2C1y', {
cache: "reload"
}));
}
let combined_cg_data = [];
await Promise.all(fetch_promises)
.then(async (responses) => {
for (var response of responses) {
await response.json()
.then((data) => {
combined_cg_data.push(...data);
});
}
companion.assets.coingecko.change.ls.set(combined_cg_data);
});
// await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=' + ids + '&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=1h%2C24h%2C7d%2C30d%2C1y', {
// cache: "reload"
// })
// .then(response => response.json())
// .then((data) => {
// companion.assets.coingecko.change.ls.set(data);
// });
}
return companion.assets.coingecko.change.ls.get();
};
/* gets latest coingecko change data from localstorage */
companion.assets.coingecko.change.ls.get = async function() {
// return await getStorageValuePromise("tokens");
return await chrome.storage.local.get("change");
};
/* sets coingecko change data in localstorage */
companion.assets.coingecko.change.ls.set = async function(change) {
chrome.storage.local.set({
"change": {
"is": "change",
"data": change,
"timestamp": Date.now()
}
});
};
}
/* ASSETS */
{
/* gets tokens (assets & data from info.osmosis.zone) from localstorage */
companion.assets.tokens.ls.get = async function() {
// return await getStorageValuePromise("tokens");
return await chrome.storage.local.get("tokens");
};
companion.assets.tokens.ls.set = async function(tokens) {
chrome.storage.local.set({
"tokens": {
"is": "tokens",
"data": tokens,
"timestamp": Date.now()
}
});
};
/* get tokens from the API */
companion.assets.tokens.api.get = async function() {
console.log('%c Fetching tokens...', 'background-color:#080;color:#fff');
return await fetch("https://api-osmosis.imperator.co/tokens/v2/all", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"sec-gpc": "1"
},
"referrer": "https://info.osmosis.zone/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "omit"
});
};
/* get tokens from ls, or from the API if refresh = true */
companion.assets.tokens.get = async function(options = {
refresh: false
}) {
var ls = {};
if (options.refresh) {
await companion.assets.tokens.api.get()
.then((response) => response.json())
.then((data) => {
companion.assets.tokens.ls.set(data);
});
}
return companion.assets.tokens.ls.get();
};
companion.assets.tokens.ls.last_updated = function() {
let ret = false;
companion.assets.tokens.ls.get()
.then((res) => {
ret = res.tokens.timestamp;
});
return ret;
}
/* returns assetlist (asset manifest containing ibc denoms) from localstorage */
companion.assets.assetlist.ls.get = async function() {
return await getStorageValuePromise("assetlist");
};
companion.assets.assetlist.ls.set = async function(assetlist) {
chrome.storage.local.set({
"assetlist": {
"is": "assetlist",
"data": assetlist,
"timestamp": Date.now()
}
});
};
/* get assetlist from the API */
companion.assets.assetlist.api.get = async function() {
console.log('%c Fetching assetlist...', 'background-color:#080;color:#fff');
// return await fetch('osmosis-1.assetlist.json', {
// return await fetch('https://raw.githubusercontent.com/jasbanza/assetlists/main/osmosis-1/osmosis-1.assetlist.json', {
return await fetch('https://raw.githubusercontent.com/osmosis-labs/assetlists/main/osmosis-1/osmosis-1.assetlist.json', {
cache: "reload"
});
};
/* get assetlist from ls, or from the API if refresh = true OR refresh = "only_when_denoms_missing" and denoms are missing */
companion.assets.assetlist.get = async function(options = {
refresh: "only_when_denoms_missing"
}) {
var isAssetlistLoaded = false;
await companion.assets.assetlist.ls.get()
.then((res) => {
assetlist = res.assetlist;
// flag if it's been stored
if (assetlist && assetlist.timestamp) {
isAssetlistLoaded = true;
}
});
if (!isAssetlistLoaded || options.refresh == true || (options.refresh == "only_when_denoms_missing" && !ls_assetlist_has_all_denoms())) {
/* force refresh */
await companion.assets.assetlist.api.get()
.then((response) => response.json())
.then((data) => {
companion.assets.assetlist.ls.set(data);
});
}
// get from localstorage
return companion.assets.assetlist.ls.get();
};
/* returns assetlist (asset manifest containing ibc denoms) from localstorage */
companion.assets.wallet.ls.get = async function() {
return await getStorageValuePromise("wallet");
};
companion.assets.wallet.ls.set = async function(wallet) {
chrome.storage.local.set({
"wallet": {
"is": "wallet",
"data": wallet,
"timestamp": Date.now()
}
});
};
/* get assetlist from the API */
companion.assets.wallet.api.get = async function(address) {
console.log('%c Fetching wallet balances...', 'background-color:#080;color:#fff');
return osmo.wallet.balances(address);
};
companion.assets.wallet.address.sync.get = async function() {
return await getStorageValuePromiseSync("address");
};
companion.assets.wallet.address.sync.set = async function(address) {
chrome.storage.sync.set({
"address": address
});
};
companion.assets.wallet.get = async function(options = {
refresh: 5000 /* 5 seconds */
}) {
var address = "";
// 1) get address
await companion.assets.wallet.address.sync.get()
.then((res) => {
address = res.address;
});
if (!address) {
return;
}
// 2) check if wallet is previously fetched.
// ... We will do timestamp age checking later
var wallet = null;
var isWalletLoaded = false;
await companion.assets.wallet.ls.get()
.then((res) => {
wallet = res.wallet;
// flag if it's been stored
if (wallet && wallet.timestamp) {
isWalletLoaded = true;
}
});
// 3) refresh from API if necessary,
// but only after 20 minutes after epoch has ended,
// as the osmosis API is problematic at this time
// due to reward distribution
var epoch_end_unix = "";
await chrome.storage.local.get("epoch_end_unix").then((res) => {
epoch_end_unix = res.epoch_end_unix;
});
// check if epoch has been "started" (will be false if the epoch hasn't been updated)
if (Math.floor(epoch_end_unix - Date.now()) > 0) {
// if wallet isn't loaded yet
if (!isWalletLoaded || options.refresh == true || (options.refresh)) {
/* force refresh */
await companion.assets.wallet.api.get(address)
.then((data) => {
if (!data.error) {
companion.assets.wallet.ls.set(data.result);
}
});
}
}
// get from localstorage
return companion.assets.wallet.ls.get();
};
/* simultaneously & asyncronously get tokens, assetlist data & wallet data */
companion.assets.get = async function(options = {
refresh: false,
refresh_wallet: false,
refresh_tokens: false,
refresh_assetlist: false
}) {
const promise_tokens = companion.assets.tokens.get({
refresh: ((options.refresh) ? options.refresh : options.refresh_tokens)
});
// cross-references assetlist:
const promise_change = companion.assets.coingecko.change.get({
refresh: ((options.refresh) ? options.refresh : options.refresh_tokens)
});
const promise_assetlist = companion.assets.assetlist.get({
refresh: ((options.refresh) ? options.refresh : options.refresh_assetlist)
});
const promise_wallet = companion.assets.wallet.get({
refresh: ((options.refresh) ? options.refresh : options.refresh_wallet)
});
var assets = {};
return Promise.all([promise_assetlist, promise_change, promise_tokens, promise_wallet])
.then((values) => {
values.forEach((ls, i) => {
if (ls) {
if (ls.tokens) {
assets.tokens = ls.tokens;
}
if (ls.change) {
assets.change = ls.change;
}
if (ls.assetlist) {
assets.assetlist = ls.assetlist;
}
if (ls.wallet) {
assets.wallet = ls.wallet;
}
}
});
return assets;
});
};
companion.assets.last_updated = async function() {
var unix_now = Date.now();
const promise_tokens = companion.assets.tokens.ls.get();
const promise_assetlist = companion.assets.assetlist.ls.get();
const promise_wallet = companion.assets.wallet.ls.get();
var age = {};
return Promise.all([promise_assetlist, promise_tokens, promise_wallet])
.then((values) => {
values.forEach((ls, i) => {
if (ls) {
if (ls.tokens) {
age.tokens = unix_now - ls.tokens.timestamp;
}
if (ls.assetlist) {
age.assetlist = unix_now - ls.assetlist.timestamp;
}
if (ls.wallet) {
age.wallet = unix_now - ls.wallet.timestamp;
}
}
});
return age;
});
};
}
/* ZONES */
{
/* gets tokens (assets & data from info.osmosis.zone) from localstorage */
companion.zones.ls.get = async function() {
// return await getStorageValuePromise("tokens");
return await chrome.storage.local.get("zones");
};
companion.zones.ls.set = async function(zones) {
chrome.storage.local.set({
"zones": {
"is": "zones",
"data": zones,
"timestamp": Date.now()
}
});
};
companion.zones.api.get = async function() {
console.log('%c Fetching zones...', 'background-color:#080;color:#fff');
return await fetch("https://api.smcloud.app/api/coins/", {
"headers": {
"accept": "application/json, text/plain, */*",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"sec-gpc": "1"
}
});
};
/* get zones from ls, or from the API if refresh = true */
companion.zones.get = async function(options = {
refresh: false
}) {
var ls = {};
if (options.refresh) {
await companion.zones.api.get()
.then((response) => response.json())
.then((data) => {
companion.zones.ls.set(data);
});
}
return companion.zones.ls.get();
};
companion.zones.ls.last_updated = function() {
let ret = false;
companion.zones.ls.get()
.then((res) => {
ret = res.zones.timestamp;
});
return ret;
}
companion.zones.ls.last_updated = function() {
let ret = false;
companion.zones.ls.get()
.then((res) => {
ret = res.zones.timestamp;
});
return ret;
}
}
/* SETTINGS */
// settings: {
// default_sort: {
// get: async function() {},
// set: async function() {}
// }
// }
{
companion.settings.default_sort.get = async function() {
return await chrome.storage.sync.get("default_sort");
};
companion.settings.default_sort.set = async function(default_sort) {
chrome.storage.sync.set({
"default_sort": default_sort
});
};
}
/* OTHER */
{
// REVIEW: is this necessary
companion.ui.render_assets = async function(forceRefresh = false) {
refresh_tokens();
};
/**
* Checks if all tokens are in the assetlist
* @param {[type]} tokens tokens from localstorage
* @param {[type]} assetlist assetlist from localstorage
* @return {bool} true if all accounted for, otherwise false
*/
function ls_assetlist_has_all_denoms() {
var all_accounted_for = true;
// 1) get tokens from LS
var tokens = [];
companion.assets.tokens.ls.get()
.then((res) => {
tokens = res.tokens.data;
});
// 2) get assetlist from LS
var assetlist = [];
companion.assets.assetlist.ls.get()
.then((res) => {
assetlist = res.assetlist.data;
});
// 3) check if tokens are all accounted for in assetlist
tokens.forEach((token, i) => {
var token_found_in_list = false;
assetlist.forEach((asset, i) => {
if (token.symbol.toLowerCase() == asset.symbol.toLowerCase()) {
token_found_in_list = true;
}
});
if (!token_found_in_list) {
all_accounted_for = false;
}
});
return all_accounted_for;
}
/* Helper function, wrapping the chrome storage API local get result as a promise's resolve */
function getStorageValuePromise(key) {
return new Promise((resolve) => {
chrome.storage.local.get(key, resolve);
});
}
/* Helper function, wrapping the chrome storage API sync get result as a promise's resolve */
function getStorageValuePromiseSync(key) {
return new Promise((resolve) => {
chrome.storage.sync.get(key, resolve);
});
}
}