forked from pendle-finance/external-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-config.js
69 lines (55 loc) · 1.89 KB
/
merge-config.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
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
async function run() {
const protocolsPath = path.join(__dirname, 'protocols');
const combinedConfigPath = path.join(__dirname, 'config.json');
const protocolIds = fs.readdirSync(protocolsPath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory()) // Filter only directories
.filter(dirent => fs.existsSync(path.join(protocolsPath, dirent.name, 'config.json'))) // Check if config.json exists
.map(dirent => dirent.name);
const data = {protocols: []};
for (const protocolId of protocolIds) {
const configPath = path.join(protocolsPath, protocolId, 'config.json');
const protocolConfigStr = fs.readFileSync(configPath, 'utf8');
const protocolConfig = formatProtocolConfig({
id: protocolId,
...JSON.parse(protocolConfigStr)
})
const {icon} = protocolConfig;
const iconPath = path.join(__dirname, `protocols/${protocolId}/${icon}`);
protocolConfig.hash = await createMD5(iconPath);
data.protocols.push(protocolConfig);
}
fs.writeFile(combinedConfigPath, JSON.stringify(data, null, 2), (err) => {
if (err) {
console.error('Error writing to file', err);
}
});
}
function formatProtocolConfig(config) {
const {metadata} = config;
const {pt, yt, lp} = metadata;
lowercaseAddressOfMetadata(pt);
lowercaseAddressOfMetadata(yt);
lowercaseAddressOfMetadata(lp);
return config;
}
function lowercaseAddressOfMetadata(assets) {
for (const asset of (assets ?? [])) {
asset.address = asset.address.toLowerCase();
}
}
function createMD5(filePath) {
return new Promise((res, rej) => {
const hash = crypto.createHash('md5');
const rStream = fs.createReadStream(filePath);
rStream.on('data', (data) => {
hash.update(data);
});
rStream.on('end', () => {
res(hash.digest('hex'));
});
})
}
void run();