-
Notifications
You must be signed in to change notification settings - Fork 98
/
script.js
199 lines (171 loc) · 4.98 KB
/
script.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const axios = require('axios')
const file = require('file-system')
const fs = require('fs')
const path = require('path')
const hashName = require('hash-file')
const { exec } = require('child_process')
const BN = require('bignumber.js')
const { abi } = require('thor-devkit')
const { getTokens, redFont, greenFont, yellowFont } = require('./utils')
const { NETS: NET_FOLDERS, NODES } = require('./const')
const abis = require('./abis')
const { verifyContract, getContractDetails } = require('./contract')
const DIST = path.join(__dirname, './dist')
const ASSETS = path.join(DIST, 'assets')
const clear = () => {
console.time(greenFont('clean'))
let hasDist = true
try {
fs.statSync(DIST)
} catch (error) {
hasDist = false
}
if (hasDist) {
file.rmdirSync(DIST)
}
console.timeEnd(greenFont('clean'))
}
async function packToken(net) {
console.time(greenFont(`build-${net}-tokens`))
const folder = path.join(__dirname, `./tokens/${NET_FOLDERS[net]}`)
const infos = await getTokensInfo(folder)
let result = []
const listJson = infos
.sort((a, b) => {
if (a.createTime < b.createTime) {
return -1
} else {
return 1
}
})
.map(item => {
return {
...item,
imgName: rename(item.img) + '.png'
}
})
file.mkdirSync(ASSETS)
for (let i = 0; i < listJson.length; i++) {
const item = listJson[i]
const {
name,
symbol,
decimals,
totalSupply
} = await getContractDetails(item.address, NODES[net])
if (name !== item.name)
throw new Error(`name does not match contract name (info=${item.name}, contract=${name})`)
if (symbol !== item.symbol)
throw new Error(`symbol does not match contract symbol (info=${item.symbol}, contract=${symbol})`)
if (decimals !== item.decimals)
throw new Error(`decimals does not match contract decimals (info=${item.decimals}, contract=${decimals})`)
await verifyContract(item.address, NODES[net])
file.copyFileSync(item.img, path.join(ASSETS, `${item.imgName}`))
result.push({
name,
symbol,
decimals,
address: item.address,
desc: item.desc,
icon: item.imgName,
totalSupply: item.symbol === 'VTHO' ? 'Infinite' : totalSupply,
...item.extra
})
}
console.table(listJson, [
'name',
'symbol',
'decimals',
'address',
'createTime'
])
file.writeFileSync(
path.join(__dirname, `./dist/${net}.json`),
JSON.stringify(result, null, 2)
)
console.timeEnd(greenFont(`build-${net}-tokens`))
}
function rename(img) {
return hashName.sync(img)
}
async function getTokensInfo(folder) {
const tokens = getTokens(folder)
const result = []
for (let i = 0; i < tokens.length; i++) {
const item = tokens[i]
result.push(await tokenInfo(path.join(folder, item), item.toLowerCase()))
}
return result
}
async function tokenInfo(tokenPath, address) {
const files = file.readdirSync(tokenPath)
const infoFile = path.join(tokenPath, 'info.json')
const img = path.join(tokenPath, 'token.png')
const info = require(infoFile)
let extraInfo = null
if (files.includes('additional.json')) {
extraInfo = getExtraInfo(path.join(tokenPath, 'additional.json'))
}
info.img = img
info.createTime = await getCreateTimeFromGit(tokenPath)
info.address = address
info.extra = extraInfo
return info
}
function getExtraInfo(filePath) {
const urlRegExp = /(https):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/
const keys = ['website', 'whitePaper']
const LinkSymbol = 'links'
const linkNames = ['twitter', 'telegram', 'facebook', 'medium', 'github', 'slack']
const extraInfo = require(filePath)
const links = extraInfo[LinkSymbol]
const linkKeys = links ? Object.keys(links) : null
let result = {}
let linksTemp = []
keys.forEach(item => {
if (!extraInfo[item]) {
return
}
if (!urlRegExp.test(extraInfo[item])) {
console.warn(yellowFont(`The ${item} link invalid`))
return
}
result[item] = extraInfo[item]
})
if (linkKeys && linkKeys.length) {
linkKeys.forEach(item => {
if (linkNames.includes(item) && links[item]) {
if (urlRegExp.test(links[item])) {
linksTemp.push({
[item]: links[item]
})
} else {
console.warn(yellowFont(`The ${item} link invalid`))
}
}
})
}
if (linksTemp.length) {
result[LinkSymbol] = linksTemp
}
return result
}
async function getCreateTimeFromGit(dirPath) {
const command =
'git log --diff-filter=A --follow --format=%aD -- [path] | tail -1'
return new Promise((resolve, reject) => {
exec(command.replace('[path]', dirPath), (err, stdout, stderr) => {
if (err) return reject(err)
if (stderr) return reject(stderr)
if (!stdout)
return reject(
new Error('Can not find create time from git for dir: ' + dirPath)
)
return resolve(new Date(stdout))
})
})
}
module.exports = {
clean: clear,
build: packToken
}