-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (143 loc) · 5.27 KB
/
index.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
const config = require('./config');
const fs = require("fs");
const path = require("path");
const handleDir = ({src, mapper, verified}) => {
try {
return fs.readdirSync(path.join(__dirname, src)).map((fileName) => {
try {
const file = fs.readFileSync(path.join(__dirname, `${src}/${fileName}`));
const {info, img} = mapper(JSON.parse(file.toString()));
return {info: {...info, verified}, img};
} catch (e) {
console.warn(e);
return undefined;
}
});
} catch (e) {
console.warn(e)
return [];
}
}
function sortList(list) {
return list.sort(function (t1, t2) {
if (t1.ticker && t2.ticker) {
return t1.ticker.toLowerCase() < t2.ticker.toLowerCase() ? -1 : 1;
}
return true;
});
}
const uniq = (items) => {
const hash = {};
items.forEach(item => {
if (hash[item.info.subject]) {
hash[item.info.subject] = {
info: { ...item.info, ...hash[item.info.subject].info },
img: hash[item.info.subject].img
};
} else {
hash[item.info.subject] = item;
}
});
return Object.values(hash);
}
const tokensInfo = sortList(uniq(config.paths.flatMap(configItem => handleDir(configItem).filter(Boolean)))
.map(({info, img}) => {
let filePath;
let url;
if (img) {
if (img.type === 'buffer') {
filePath = `./out/logos/${info.subject}.webp`;
url = `/logos/cardano/${info.subject}.webp`;
fs.writeFileSync(path.join(__dirname, filePath), img.content);
} else {
if (img.content.startsWith('http')) {
url = img.content
} else {
const [fileName] = img.content.split('/').reverse();
filePath = `./out/logos/${fileName}`;
url = `/logos/cardano/${fileName}`;
try {
fs.writeFileSync(path.join(__dirname, filePath), fs.readFileSync(path.join(__dirname, img.content)));
} catch (e) {
console.log(e);
console.log(`no icon for ${JSON.stringify(info)}`)
}
}
}
}
return url ? {...info, logo: url} : info;
}));
const tokenList = {
name: "Spectrum Finance Cardano Token List",
timestamp: new Date().toISOString(),
version: config.paths.map(c => c.version).join('--'),
tags: {},
keywords: ["spectrum finance", "tokens", "cardano tokens"],
tokens: tokensInfo,
}
const tokenListV2 = {
name: "Spectrum Finance Cardano Token List",
timestamp: new Date().toISOString(),
version: config.paths.map(c => c.version).join('--'),
tags: {},
keywords: ["spectrum finance", "tokens", "cardano tokens"],
tokens: tokensInfo.reduce((acc, asset) => ({
...acc,
[asset.subject
? [
asset.subject.slice(0, 56),
asset.subject.slice(56, asset.subject.length),
].join('.')
: '.']: {
...asset,
logo: asset.logo && !asset.logo?.startsWith('http') ?
`https://spectrum.fi${asset.logo}` :
asset.logo,
}
}), {}),
}
const tokenListSnekfun = {
name: "Spectrum Finance Cardano Token List",
timestamp: new Date().toISOString(),
version: config.paths.map(c => c.version).join('--'),
tags: {},
keywords: ["snekfun", "tokens", "cardano tokens"],
tokens: tokensInfo.filter(asset => asset.snekFun || !asset.subject).reduce((acc, asset) => ({
...acc,
[asset.subject
? [
asset.subject.slice(0, 56),
asset.subject.slice(56, asset.subject.length),
].join('.')
: '.']: {
...asset,
logo: asset.logo && !asset.logo?.startsWith('http') ?
`https://spectrum.fi${asset.logo}` :
asset.logo,
}
}), {}),
}
fs.writeFile(path.join(__dirname, './out/cardano-token-list.json'), JSON.stringify(tokenList, null, 2), 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File. ");
return console.log(err);
}
console.log("New cardano token list has been built.");
console.log("Current version is " + tokenList.version + ".");
})
fs.writeFile(path.join(__dirname, './out/cardano-token-list-v2.json'), JSON.stringify(tokenListV2, null, 2), 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File. ");
return console.log(err);
}
console.log("New cardano token list v2 has been built.");
console.log("Current version is " + tokenList.version + ".");
})
fs.writeFile(path.join(__dirname, './out/cardano-token-list-snekfun.json'), JSON.stringify(tokenListSnekfun, null, 2), 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File. ");
return console.log(err);
}
console.log("New cardano token list snekfun has been built.");
console.log("Current version is " + tokenList.version + ".");
})