forked from trustwallet/tokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (119 loc) · 4.37 KB
/
index.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
145
146
147
148
149
150
151
const fs = require('fs')
const axios = require('axios')
const path = require('path')
const pngExp = /\.png$/
const upperCaseExp = /[A-F]/
const OxExp = /^0x/
const isAddress = address => /^(0x)?[0-9a-f]{40}$/i.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)
const isFilePng = name => pngExp.test(name)
const remotePngExtension = string => string.replace(/.png$/, '')
const readDirSync = path => fs.readdirSync(path)
checkRootDir()
readDirSync(`./tokens`).forEach(token => {
const address = remotePngExtension(token)
if (!isFilePng(token)) {
exitWithMsg(`${token} image must be png`)
}
if (upperCaseExp.test(address)) {
exitWithMsg(`${address} image must be in lowercase`)
}
if (!OxExp.test(address)) {
exitWithMsg(`'${address}' must start with 0x`)
}
if (!isAddress(address)) {
exitWithMsg(`${address} image must have length 42 instead have ${address.length}`)
}
})
function checkRootDir () {
readDirSync('.').forEach(file => {
if(isFilePng(file)) {
exitWithMsg(`Move ${file} to ./tokens folder`)
}
})
if(fs.existsSync("./images")) {
exitWithMsg(`Adding to ./image folder is restricted, please update your fork`)
}
}
checkBlockhainsFolder()
function checkBlockhainsFolder(){
const currentBlockchains = 47
const foundBlockchains = readDirSync('./blockchains')
if (foundBlockchains.length !== currentBlockchains) {
exitWithMsg(`Expected amount of chains in "./blockchains" = ${currentBlockchains}, found ${foundBlockchains.length}. Add tests for new folder`)
}
foundBlockchains.forEach(folder => {
commonChainCheck(folder)
//Check Ethereum sidechains folders
const ethereumSidechains = ["ethereum", "classic", "poa", "tomochain", "gochain", "wanchain", "callisto", "thundertoken"]
if (ethereumSidechains.indexOf(folder) !== -1) {
const assetsPath = `./blockchains/${folder}/assets`
const assets = readDirSync(assetsPath)
assets.forEach(asset => {
if (!isAddress(asset)) {
exitWithMsg(`Invalid asset naming "${assetsPath}"`)
}
// Check if asset folder contains logo.png image
const assetLogoPath = `${assetsPath}/${asset}/logo.png`
if (!fs.existsSync(assetLogoPath)) {
exitWithMsg(`${assetLogoPath} mush have logo.png`)
}
})
}
if (folder === "binance") {
checkBinance()
}
if (folder === "tron") {
checkTron()
}
console.log(`Folder ${folder} passed all checks`)
})
}
function commonChainCheck(folder) {
if (upperCaseExp.test(folder)) {
exitWithMsg(`"${folder}" must be in lowercase register`)
}
const pathToInfo = path.join(__dirname, '..', `blockchains/${folder}/info/logo.png`)
if (!fs.existsSync(pathToInfo)) {
exitWithMsg(`Can't find coin image inside "${pathToInfo}"`)
}
}
async function checkBinance() {
const tokenSymbols = await getBinanceTokenSymbols()
const path = `./blockchains/binance/assets`
const assets = readDirSync(path)
assets.forEach(asset => {
if (upperCaseExp.test(asset)) {
exitWithMsg(`${asset} folder must be in lowercase`)
}
if (tokenSymbols.indexOf(asset.toUpperCase()) == -1) {
exitWithMsg(`${asset} does not exist on chain`)
}
const assetLogoPath = `${path}/${asset}/logo.png`
if (!fs.existsSync(assetLogoPath)) {
exitWithMsg(`Path ${assetLogoPath} mush have logo.png`)
}
})
}
function checkTron() {
const path = `./blockchains/tron/assets`
const assets = readDirSync(path)
assets.forEach(asset => {
if (!(/^\d+$/.test(asset))) {
exitWithMsg(`${asset} folder must contain only digits`)
}
const assetLogoPath = `${path}/${asset}/logo.png`
if (!fs.existsSync(assetLogoPath)) {
exitWithMsg(`Path ${assetLogoPath} mush have logo.png`)
}
})
}
async function getBinanceTokenSymbols() {
return axios.get(`https://dex-atlantic.binance.org/api/v1/tokens?limit=1000`).then(res => {
return res.data.map(token => token.symbol)
})
}
const exitWithMsg = msg => {
console.log(msg)
process.exit(1)
}
console.log(`\nPassed all tests`)