forked from near/near-contract-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiatValueManager.js
78 lines (61 loc) · 2.18 KB
/
FiatValueManager.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
const superagent = require('superagent');
const Dataloader = require('dataloader');
const Cache = require('node-cache');
const ENABLE_DEBUG = false;
const debugLog = (...args) => ENABLE_DEBUG && console.log('FiatValueManager', ...args);
function wrapNodeCacheForDataloader(cache) {
return {
get: (...args) => {
return cache.get(...args);
},
set: (...args) => {
return cache.set(...args);
},
delete: (...args) => {
return cache.del(...args);
},
clear: (...args) => {
return cache.flushAll(...args);
}
};
}
class FiatValueManager {
constructor({ nodeCache } = {}) {
// 0 checkperiod means we only purge values from the cache on attempting to read an expired value
// Which allows us to avoid having to call `.close()` on the cache to allow node to exit cleanly
this.valueCache = nodeCache || new Cache({ stdTTL: 10, checkperiod: 0, useClones: false });
this.fiatValueLoader = this.createFiatValueLoader(this.valueCache);
}
createFiatValueLoader(cache) {
return new Dataloader(
async (tokenIds) => {
const prices = await this.fetchFiatValues(tokenIds);
return tokenIds.map((id) => prices[id]);
},
{
cacheMap: wrapNodeCacheForDataloader(cache)
}
);
}
async fetchFiatValues(tokenIds) {
debugLog('fetchFiatValues()');
const { body } = await superagent
.get('https://api.coingecko.com/api/v3//simple/price')
.set('Accept', 'application/json')
.retry(3)
.query({
include_last_updated_at: true,
vs_currencies: 'usd,eur,cny',
ids: tokenIds.join(','),
});
return body;
}
async getPrice(tokens = ['near']) {
debugLog('getPrice()', { tokens });
const byTokenName = {};
const prices = await this.fiatValueLoader.loadMany(tokens);
tokens.forEach((tokenName, ndx) => byTokenName[tokenName] = prices[ndx]);
return byTokenName;
}
}
module.exports = FiatValueManager;