-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadataByToken.js
executable file
·64 lines (61 loc) · 1.81 KB
/
metadataByToken.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
const polygonMainnetTokenMetadata = require('./constants/openq-polygon-mainnet-indexable.json');
const mumbaiTokenMetadata = require('./constants/openq-polygon-mumbai-indexable.json');
const localTokenMetadata = require('./constants/openq-local-indexable.json');
const ethers = require('ethers');
const cachedMetadataByToken = require('./utils/cachedMetadataByToken');
const fetchMetadata = require('./utils/fetchMetadata');
const metadata = async (client, address) => {
const promise = new Promise(async (resolve, reject) => {
try {
const checkSummedAddress = ethers.utils.getAddress(address);
let metadata = await cachedMetadataByToken(client, checkSummedAddress);
if (!metadata) {
const fetchedMetadata = await fetchMetadata(client);
fetchedMetadata.forEach(async token => {
await client.hmset(
'tokenObject',
token.address,
JSON.stringify(token)
);
});
metadata = fetchedMetadata.find(
tokenData => tokenData.address === address.toLowerCase()
);
}
if (!metadata) {
switch (process.env.DEPLOY_ENV) {
case 'polygon-mainnet':
metadata = polygonMainnetTokenMetadata[checkSummedAddress];
break;
case 'mumbai':
metadata = mumbaiTokenMetadata[checkSummedAddress];
break;
case 'local':
metadata = localTokenMetadata[checkSummedAddress];
}
}
if (!metadata) {
metadata = {
chainId: 137,
name: 'Custom Token',
symbol: 'CUSTOM',
decimals: 18,
address: checkSummedAddress,
path: '/crypto-logos/ERC20.svg'
};
}
const tokenMetadata = {
[checkSummedAddress]: {
...metadata,
address: ethers.utils.getAddress(metadata.address)
}
};
resolve(tokenMetadata);
} catch (err) {
console.log(err);
reject(err);
}
});
return promise;
};
module.exports = metadata;