-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathfetchUnicly.js
More file actions
117 lines (93 loc) · 3.18 KB
/
Copy pathfetchUnicly.js
File metadata and controls
117 lines (93 loc) · 3.18 KB
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
const path = require('path')
const axios = require('axios')
const pickBy = require('lodash/pickBy')
const merge = require('lodash/merge')
const { ethers, InfuraProvider } = require('ethers')
const { exec, readdir, readJSON, writeFile, promisify } = require('./utils')
const repoPath = '0xLeia/unicly-utoken-info'
const treeURL = `https://api.github.com/repos/${repoPath}/git/trees/main?recursive=1`
const rawURL = `https://raw.githubusercontent.com/${repoPath}/main`
const fns = ['symbol', 'name', 'decimals', '_description']
const abi = fns.map(
name => `function ${name}() view returns (${name === 'decimals' ? 'uint8' : 'string'})`,
)
const provider = new ethers.providers.InfuraProvider(null, process.env.INFURA)
const main = async () => {
const { data } = await axios.get(treeURL)
const files = data.tree.filter(
t => t.path.startsWith('uTokens/0x') && (t.path.endsWith('.json') || t.path.endsWith('svg')),
)
const fileDatas = await Promise.all(
files.map(file => axios.get(`${rawURL}/${file.path}`).then(({ data }) => data)),
)
const contracts = files.reduce((acc, file, i) => {
const [fileName, h] = file.path.split('/').reverse()
const hash = h.toLowerCase()
if (!acc[hash]) {
acc[hash] = {}
}
if (fileName.endsWith('.svg')) {
acc[hash].logo = fileDatas[i]
} else {
acc[hash].data = pickBy(
{
description: (fileDatas[i].description || '').trim().replace(/\s\s+/g, ' '),
website: fileDatas[i].website,
links: pickBy(
{
discord: fileDatas[i].discord,
medium: fileDatas[i].medium,
twitter: fileDatas[i].twitter,
telegram: fileDatas[i].telegram,
},
f => f,
),
},
f => f,
)
}
return acc
}, {})
const contractDatas = await Promise.all(
Object.keys(contracts).map(async hash => {
const erc20 = new ethers.Contract(hash, abi, provider)
const datas = await Promise.all(fns.map(name => erc20[name]().catch(() => null)))
if (!datas[0]) {
return null
}
return pickBy(
fns.reduce((acc, name, i) => {
if (name === 'decimals') {
acc[name] = datas[i]
return acc
}
acc[name.replace(/_/, '')] = (datas[i] || '').trim().replace(/\s\s+/g, ' ')
return acc
}, {}),
f => f,
)
}),
)
const payload = Object.keys(contracts)
// .filter((key, i) => contractDatas[i] || contracts[key].data)
.map((key, i) => ({
key,
...contracts[key],
data: { ...contractDatas[i], ...contracts[key].data },
}))
for (const contract of payload) {
if (Object.keys(contract.data).length === 0) {
continue
}
const dir = path.join(__dirname, `../data/ethereum/assets/${contract.key}`)
await exec(`mkdir -p ${dir}`)
const metaPath = path.join(dir, 'meta.json')
const old = await readJSON(metaPath)
await writeFile(metaPath, JSON.stringify(merge(old, contract.data), null, 2))
const files = await readdir(dir)
if (!files.includes('logo.png')) {
writeFile(path.join(dir, 'logo.svg'), contract.logo)
}
}
}
main()