forked from vechain/nft-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
256 lines (220 loc) · 7.54 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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 { getTokens, greenFont, yellowFont } = require('./utils')
const { NETS: NET_FOLDERS } = require('./const')
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, net)
let result = []
const listJson = infos
.sort((a, b) => {
if (a.createdAt < b.createdAt) {
return -1
} else {
return 1
}
})
.map(item => {
return {
...item,
imgName: item.img ? rename(item.img) + '.webp' : ''
}
})
file.mkdirSync(ASSETS)
for (const item of listJson) {
if (item.img) {
file.copyFileSync(item.img, path.join(ASSETS, `${item.imgName}`))
}
result.push({
address: item.address,
name: item.name,
creator: item.creator,
description: item.description,
icon: item.imgName ? `assets/${item.imgName}` : undefined,
marketplaces: item.marketplaces || [],
chainData: item.chainData,
...item.extra
})
}
console.table(listJson, [
'address',
'name',
'creator',
'createdAt'
])
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, net) {
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(), net))
}
return result
}
async function tokenInfo(tokenPath, address, net) {
const files = file.readdirSync(tokenPath)
const infoFile = path.join(tokenPath, 'info.json')
const info = require(infoFile)
const extraInfo = files.includes('extra.json') ? getExtraInfo(path.join(tokenPath, 'extra.json')) : null
const marketplaceInfo = files.includes('marketplace.json') ? getMarketplaceInfo(path.join(tokenPath, 'marketplace.json')) : null
info.img = files.includes('token.webp') ? path.join(tokenPath, 'token.webp') : ''
info.createdAt = await getCreatedAtFromGit(tokenPath)
info.address = address
info.extra = extraInfo
info.marketplaces = marketplaceInfo
info.chainData = await getContractAttributesFromEnergy(net, address)
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 = ['discord', '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
}
function getMarketplaceInfo(filePath) {
const urlRegExp = /(https):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/
const marketplaces = require(filePath)
const result = []
if (!Array.isArray(marketplaces)) {
console.warn(yellowFont(`marketplace.json is no valid array`))
return
}
marketplaces.forEach((marketplace, index) => {
if (!urlRegExp.test(marketplace.link)) {
console.warn(yellowFont(`The marketplace at index ${index} link is invalid`))
return
}
if (marketplace.tokenLink && !urlRegExp.test(marketplace.tokenLink)) {
console.warn(yellowFont(`The marketplace at index ${index} tokenLink is invalid`))
return
}
if (marketplace.tokenLink && !marketplace.tokenLink.includes('{{tokenId}}')) {
console.warn(yellowFont(`The marketplace at index ${index} tokenLink is does not contain a {{tokenId}} placeholder`))
return
}
if (!marketplace.name) {
console.warn(yellowFont(`The marketplace at index ${index} name is invalid`))
return
}
result.push(marketplace)
})
return result
}
async function getCreatedAtFromGit(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))
})
})
}
async function getContractAttributesFromEnergy(net, address) {
try {
const { data } = await axios.post(`https://api.vechain.energy/v1/call/${net}`, {
clauses: [
{ to: address, signature: "name() returns (string name)" },
{ to: address, signature: "supportsInterface(bytes4 0x36372b07) returns(bool erc20)" },
{ to: address, signature: "supportsInterface(bytes4 0x01ffc9a7) returns(bool erc165)" },
{ to: address, signature: "supportsInterface(bytes4 0xa1c0ed36) returns(bool erc712)" },
{ to: address, signature: "supportsInterface(bytes4 0x80ac58cd) returns(bool erc721)" },
{ to: address, signature: "supportsInterface(bytes4 0x5b5e139f) returns(bool erc721Metadata)" },
{ to: address, signature: "supportsInterface(bytes4 0x780e9d63) returns(bool erc721Enumerable)" },
{ to: address, signature: "supportsInterface(bytes4 0x150b7a02) returns(bool erc721Receiver)" },
{ to: address, signature: "supportsInterface(bytes4 0xe5cfc6d0) returns(bool erc777)" },
{ to: address, signature: "supportsInterface(bytes4 0xd9b67a26) returns(bool erc1155)" },
{ to: address, signature: "supportsInterface(bytes4 0x1820a4b3) returns(bool erc1820)" },
{ to: address, signature: "supportsInterface(bytes4 0x2a55205a) returns(bool erc2981)" },
{ to: address, signature: "supportsInterface(bytes4 0x8c65f84d) returns(bool erc5643)" }
]
})
const attributes = data.reduce((attributes, attribute) => {
const keys = Object.keys(attribute)
keys
.filter(key => !['0', '__length__'].includes(key))
.forEach(key => {
if (key.slice(0, 3) === 'erc') {
attributes.supportsInterface[key] = attribute[key]
}
else {
attributes[key] = attribute[key]
}
})
return attributes
}, { supportsInterface: {} })
return attributes
}
catch (err) { }
}
module.exports = {
clean: clear,
build: packToken
}