-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.js
executable file
·47 lines (44 loc) · 1.48 KB
/
metadata.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
// Array of all supported tokens
const polygonMainnetTokens = require('./constants/openq-polygon-mainnet-enumerable.json');
const mumbaiTokens = require('./constants/openq-polygon-mumbai-enumerable.json');
const localTokens = require('./constants/openq-local-enumerable.json');
const fetchCachedMetadata = require('./utils/fetchCachedMetadata');
const fetchMetadata = require('./utils/fetchMetadata');
const metadata = async (req, client) => {
const promise = new Promise(async (resolve, reject) => {
const { query } = req;
const cursor = parseInt(query.cursor);
const limit = parseInt(query.limit);
const list = query.list;
if (list === 'polygon') {
let tokenMetadata = [];
cachedMetadata = await fetchCachedMetadata(client, cursor, limit);
tokenMetadata = cachedMetadata.map(elem => JSON.parse(elem));
if (tokenMetadata.length === 0) {
console.log('cache miss');
const fetchedMetadata = await fetchMetadata(client, cursor, limit);
const paginatedTokens = fetchedMetadata.filter((elem, index) => {
return index >= cursor && index < limit + cursor;
});
tokenMetadata = paginatedTokens;
}
resolve(tokenMetadata);
}
else if (list === 'constants') {
switch (process.env.DEPLOY_ENV) {
case 'polygon-mainnet':
resolve(polygonMainnetTokens);
break;
case 'mumbai':
resolve(mumbaiTokens);
break;
case 'local':
resolve(localTokens);
break;
}
}
else resolve([]);
});
return promise;
};
module.exports = metadata;