-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchCoingeckoChainlist.ts
66 lines (66 loc) · 1.79 KB
/
fetchCoingeckoChainlist.ts
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
import axios from "axios";
import { csl } from "./func";
import * as fs from "fs";
import { chainItem, coingeckoChain } from "./types";
const fetchChainList = async (): Promise<coingeckoChain[]> => {
const url = "https://api.coingecko.com/api/v3/asset_platforms";
const res = await axios.get(url);
const data = res.data;
fs.writeFile(`coingeckoChainList.json`, JSON.stringify(data), (e) => {
if (e) {
console.error("error: ", e);
}
console.log("coingecko chain list saved");
});
return data;
};
export const mapCoingeckoChainName = async () => {
let maps: { [cg: string]: string } = {};
let chainlist: chainItem[] = [];
try {
chainlist = JSON.parse(fs.readFileSync(`chainlist.json`, "utf8"));
} catch (e) {
console.log("No chain list was found");
return undefined;
}
let cgchain: coingeckoChain[] = [];
try {
console.log("feching new data");
cgchain = (await fetchChainList())!;
csl(cgchain);
} catch (e) {
cgchain = JSON.parse(fs.readFileSync(`coingeckoChainList.json`, "utf8"));
}
for (const c of chainlist) {
const cg = cgchain.find((i) => {
if (c.id) {
if (i.chain_identifier) {
if (i.chain_identifier === c.id) {
return true;
}
} else {
if (i.native_coin_id == c.native_token?.coingecko_id) {
return true;
}
}
} else {
if (i.native_coin_id == c.native_token?.coingecko_id) {
return true;
}
}
});
if (cg) {
maps[cg.id] = c.chain ?? "";
}
}
fs.writeFile(`chainmap.json`, JSON.stringify(maps), (e) => {
if (e) {
console.error("error: ", e);
}
console.log("coingecko chain map saved");
});
return maps;
};
// (async () => {
// await mapCoingeckoChainName();
// })();