-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfetchMissing.js
118 lines (105 loc) · 3.69 KB
/
fetchMissing.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
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 { HomeOmnibridgeAbi } from './abis/HomeOmnibridge.js';
import fs from "fs";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const { chains, publicClient, webSocketPublicClient } = configureChains(
[gnosis],
[publicProvider()],
)
const config = createConfig({
autoConnect: true,
publicClient
})
async function getMainnetAddress(gnosisAddr) {
const data = await readContract({
address: '0xf6A78083ca3e2a662D6dd1703c939c8aCE2e268d',
abi: HomeOmnibridgeAbi,
functionName: 'foreignTokenAddress',
args: [gnosisAddr]
})
return data;
}
const indexOf = (arr, q) => arr.findIndex(item => q.toLowerCase() === item.toLowerCase());
async function getLogo(gnosisAddr, tokenSymbol) {
const mainnetAddr = await getMainnetAddress(gnosisAddr);
const geckoTokens = await geckoList.tokens.map((x) => x.address);
const cowTokens = await cowList.tokens.map((x) => x.address);
const mainnetIndex = await indexOf(geckoTokens, mainnetAddr.toLowerCase());
const gnosisIndex = await indexOf(geckoTokens, gnosisAddr);
const cowIndex = await indexOf(cowTokens, gnosisAddr);
if (mainnetIndex > 0) {
const tokenFound = await geckoList.tokens[mainnetIndex].logoURI;
return tokenFound;
}
else if (gnosisIndex > 0) {
const tokenFound = await geckoList.tokens[gnosisIndex].logoURI;
return tokenFound;
}
else if (cowIndex > 0) {
const tokenFound = await cowList.tokens[cowIndex].logoURI;
return tokenFound;
} else {
console.log("Not on Coingecko or Cowswap List: ", gnosisAddr, " ", tokenSymbol);
}
return ""
}
async function fetchMissing(listed) {
const response = await fetch(
"https://blockscout.com/poa/xdai/api/?module=account&action=tokentx&address=0xf6A78083ca3e2a662D6dd1703c939c8aCE2e268d"
);
const data = await response.json();
if(!data)return null
const bridgedTokens = [
...new Set(data.result.map((x) => x.contractAddress.toLowerCase())),
];
const jsonListed = await JSON.parse(listed);
const listedTokens = jsonListed.map((x) => x.address.toLowerCase());
const missingTokens = bridgedTokens.filter((x) => !listedTokens.includes(x));
return Promise.all(
bridgedTokens
.map((x) => data.result.find((y) => y.contractAddress === x))
.map(
async ({ tokenName, tokenSymbol, tokenDecimal, contractAddress }) => {
const logoURI = await getLogo(contractAddress, tokenSymbol);
if (logoURI !== "") {
return {
name: tokenName,
address: contractAddress,
symbol: tokenSymbol,
decimals: parseInt(tokenDecimal),
chainId: 100,
logoURI,
};
}
}
)
);
}
const fetchGecko = await fetch("https://tokens.coingecko.com/uniswap/all.json");
const geckoList = await fetchGecko.json();
const fetchCow = await fetch("https://files.cow.fi/tokens/CowSwap.json");
const cowList = await fetchCow.json();
fs.readFile(__dirname + "/tokens/gnosis.json", { encoding: "UTF8" }, function read(err, data) {
if (err) {
throw err;
}
fetchMissing(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 + "/newGnosisTokens.json"), newTokens, function (err) {
if (err) console.log(err);
});
});
});