-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfixLists.js
144 lines (134 loc) · 3.99 KB
/
fixLists.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
import fetch from "node-fetch";
import utils from "web3-utils";
import { gnosis } from "@wagmi/core/chains";
import { configureChains, readContract, createConfig } from "@wagmi/core";
import { publicProvider } from "@wagmi/core/providers/public";
import fs from "fs";
import { fileURLToPath } from "url";
import { dirname } from "path";
import axios from "axios";
const __dirname = dirname(fileURLToPath(import.meta.url));
const logos = {};
function sortTokenList(list) {
function compareStrings(a, b) {
// Assuming you want case-insensitive comparison
a = a.toLowerCase();
b = b.toLowerCase();
return a < b ? -1 : a > b ? 1 : 0;
}
return list.sort(function (a, b) {
return compareStrings(a.name, b.name);
});
}
function removeDuplicates(list) {
let i = 0;
while (i < list.length - 1) {
const token = list[i];
if (list[i].symbol === list[i + 1].symbol) {
const regex = /.*(from Mainnet)/gm;
const parsed = regex.exec(token.name);
const isBridged = parsed ? true : false;
console.log(token.name);
if (isBridged) {
list.splice(i, 1);
} else {
list.splice(i + 1, 1);
}
}
token.name = token.name.replace("from Ethereum", "on Gnosis");
token.name = token.name.replace("from Mainnet", "on Gnosis");
token.name = token.name.replace("on xDai", "on Gnosis");
i++
}
return list;
}
function checkIfLogoAlreadyExists(address) {
function test(format) {
if (fs.existsSync(__dirname + `/assets/gnosis/${address}/logo.${format}`))
return true;
return false;
}
const formats = ["png", "jpg", "svg", "jpeg"];
for (let ff of formats) {
if (test(ff)) {
return `https://raw.githubusercontent.com/1Hive/default-token-list/master/src/assets/gnosis/${address}/logo.${ff}`;
}
}
return false;
}
async function getLogo(url, address, name) {
const check = await checkIfLogoAlreadyExists(address);
if (check !== false) {
return check;
}
const regex = /.*(\.[png;jpg;svg;jpeg]+)/gm;
try {
const format = regex.exec(url);
const ff = format ? format[1] : ".png";
const newDir = __dirname + `/assets/gnosis/${address}`;
const newPath = newDir + `/logo${ff}`;
if (newPath !== url) {
const response = await axios
.get(url, { timeout: 20000, responseType: "stream" })
.then((res) => {
if (!fs.existsSync(newDir)) {
fs.mkdirSync(newDir, { recursive: true });
}
if (address === `0x0acd91f92fe07606ab51ea97d8521e29d110fd09`);
res.data.pipe(fs.createWriteStream(newPath));
});
return `https://raw.githubusercontent.com/1Hive/default-token-list/master/src/assets/gnosis/${address}/logo${ff}`;
}
} catch (error) {
console.error(`${name}: ${error.message}`);
}
}
async function fixLists(currentList) {
const listedTokens = JSON.parse(currentList);
const sortedList = sortTokenList(listedTokens);
const parsedList = removeDuplicates(sortedList);
return Promise.all(
parsedList.map(
async ({ name, address, symbol, decimals, chainId, logoURI }) => {
address = address.toLowerCase();
name = name.trim();
const newUri = await getLogo(logoURI, address, name);
if (logoURI !== "") {
return {
name: name,
address: address,
symbol: symbol,
decimals: decimals,
chainId: chainId,
logoURI: newUri,
};
}
}
)
);
}
fs.readFile(
__dirname + "/tokens/gnosis.json",
{ encoding: "UTF8" },
function read(err, data) {
if (err) {
throw err;
}
fixLists(data).then((x) => {
const cleanJson = x.filter(function (obj) {
if (!obj) {
return false; // skip
}
return true;
});
const newTokens = JSON.stringify(cleanJson, null, " ");
fs.writeFile(
__dirname + "/tokens/gnosis.json",
newTokens,
function (err) {
if (err) console.log(err);
}
);
});
}
);