forked from trustwallet/assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
454 lines (391 loc) · 17.1 KB
/
index.test.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
const eztz = require('eztz-lib')
import {
Binance, Cosmos, Tezos, Tron, IoTeX, Waves, Kava, Terra,
assetFolderAllowedFiles,
chainFolderAllowedFiles,
chainsFolderPath,
ethSidechains,
findFiles,
getBinanceBEP2Symbols,
getChainAssetLogoPath,
getChainAssetPath,
getChainAssetsPath,
getChainFolderFilesList,
getChainBlacklistPath,
getChainLogoPath,
getChainValidatorAssetLogoPath,
getChainValidatorsAssets,
getChainValidatorsListPath,
getChainWhitelistPath,
getChainAssetsList,
getChainValidatorsList,
isChecksum,
isLogoDimentionOK,
isLogoSizeOK,
isLowerCase,
isPathDir,
isPathExistsSync,
isTRC10, isTRC20, isWavesAddress,
isValidJSON,
isAssetInfoOK,
isValidatorHasAllKeys,
mapList,
pricingFolderPath,
readDirSync,
readFileSync,
rootDirAllowedFiles,
stakingChains,
} from "./helpers"
import { ValidatorModel, mapTiker, TickerType } from "./models";
import { getHandle } from "../../script/gen_info";
describe("Check repository root dir", () => {
const dirActualFiles = readDirSync(".")
test("Root should contains only predefined files", () => {
dirActualFiles.forEach(file => {
expect(rootDirAllowedFiles.indexOf(file), `File "${file}" should not be in root or added to predifined list`).not.toBe(-1)
})
})
})
describe(`Test "blockchains" folder`, () => {
const foundChains = readDirSync(chainsFolderPath)
test(`Chain should have "logo.png" image`, () => {
foundChains.forEach(chain => {
const chainLogoPath = getChainLogoPath(chain)
expect(isPathExistsSync(chainLogoPath), `File missing at path "${chainLogoPath}"`).toBe(true)
const [isOk, msg] = isLogoDimentionOK(chainLogoPath)
expect(isOk, msg).toBe(true)
})
})
test("Chain folder must have lowercase naming", () => {
foundChains.forEach(chain => {
expect(isLowerCase(chain), `Chain folder must be in lowercase "${chain}"`).toBe(true)
})
})
describe(`Asset folder should contain only predefined list of files`, () => {
readDirSync(chainsFolderPath).forEach(chain => {
const assetsPath = getChainAssetsPath(chain)
if (isPathExistsSync(assetsPath)) {
test(`Test asset folder allowed files on chain: ${chain}`, () => {
readDirSync(assetsPath).forEach(address => {
const assetFiles = getChainAssetPath(chain, address)
readDirSync(assetFiles).forEach(assetFolderFile => {
expect(assetFolderAllowedFiles.indexOf(assetFolderFile),`File "${assetFolderFile}" not allowed at this path: ${assetsPath}`).not.toBe(-1)
})
})
})
}
})
})
describe(`Chain folder should contain only predefined list of files`, () => {
readDirSync(chainsFolderPath).forEach(chain => {
getChainFolderFilesList(chain).forEach(file => {
expect(chainFolderAllowedFiles.indexOf(file),`File "${typeof file}" ${file} not allowed in chain folder: ${chain}`).not.toBe(-1)
})
})
})
describe("Check Ethereum side-chain folders", () => {
ethSidechains.forEach(chain => {
test(`Test chain ${chain} folder`, () => {
getChainAssetsList(chain).forEach(address => {
const assetPath = getChainAssetPath(chain, address)
expect(isPathDir(assetPath), `Expect directory at path: ${assetPath}`).toBe(true)
const checksum = isChecksum(address)
expect(checksum, `Expect asset at path ${assetPath} in checksum`).toBe(true)
const lowercase = isLowerCase(address)
if (lowercase) {
expect(checksum, `Lowercase address ${address} on chain ${chain} should be in checksum`).toBe(true)
}
const assetLogoPath = getChainAssetLogoPath(chain, address)
expect(isPathExistsSync(assetLogoPath), `Missing file at path "${assetLogoPath}"`).toBe(true)
const [isDimentionOK, dimensionMsg] = isLogoDimentionOK(assetLogoPath)
expect(isDimentionOK, dimensionMsg).toBe(true)
const [isLogoOK, sizeMsg] = isLogoSizeOK(assetLogoPath)
expect(isLogoOK, sizeMsg).toBe(true)
const [isInfoOK, InfoMsg] = isAssetInfoOK(chain, address)
expect(isInfoOK, InfoMsg).toBe(true)
})
})
})
})
describe(`Check "binace" folder`, () => {
it("Asset must exist on chain", async () => {
const tokenSymbols = await getBinanceBEP2Symbols()
const assets = readDirSync(getChainAssetsPath(Binance))
assets.forEach(asset => {
expect(tokenSymbols.indexOf(asset), `Asset ${asset} missing on chain`).not.toBe(-1)
})
})
})
describe(`Check "tron" folder`, () => {
const path = getChainAssetsPath(Tron)
test("Expect asset to be TRC10 or TRC20", () => {
readDirSync(path).forEach(asset => {
expect(isTRC10(asset) || isTRC20(asset), `Asset ${asset} at path ${path} non TRC10 nor TRC20`).toBe(true)
const assetsLogoPath = getChainAssetLogoPath(Tron, asset)
expect(isPathExistsSync(assetsLogoPath), `Missing file at path "${assetsLogoPath}"`).toBe(true)
const [isOk, msg] = isLogoDimentionOK(assetsLogoPath)
expect(isOk, msg).toBe(true)
})
})
})
describe("Check Staking chains", () => {
test("Make sure tests added for new staking chain", () => {
expect(stakingChains.length).toBe(7)
})
stakingChains.forEach(chain => {
const validatorsListPath = getChainValidatorsListPath(chain)
const validatorsList = getChainValidatorsList(chain)
test(`Chain ${chain} validator must have correct structure and valid JSON format`, () => {
validatorsList.forEach((val: ValidatorModel) => {
expect(isValidatorHasAllKeys(val), `Some key and/or type missing for validator ${JSON.stringify(val)}`).toBe(true)
expect(isValidJSON(validatorsListPath), `Not valid json file at path ${validatorsListPath}`).toBe(true)
})
})
test(`Chain ${chain} validator must have corresponding asset logo`, () => {
validatorsList.forEach(({ id }) => {
const path = getChainValidatorAssetLogoPath(chain, id)
expect(isPathExistsSync(path), `Chain ${chain} asset ${id} logo must be present at path ${path}`).toBe(true)
const [isOk, msg] = isLogoDimentionOK(path)
expect(isOk, msg).toBe(true)
})
})
const chainValidatorsAssetsList = getChainValidatorsAssets(chain)
switch (chain) {
case Cosmos:
testCosmosValidatorsAddress(chainValidatorsAssetsList)
break;
case Kava:
testKavaValidatorsAddress(chainValidatorsAssetsList)
break;
case Terra:
testTerraValidatorsAddress(chainValidatorsAssetsList)
break;
case Tezos:
testTezosValidatorsAssets(chainValidatorsAssetsList)
break;
case Tron:
testTronValidatorsAssets(chainValidatorsAssetsList)
break;
case Waves:
testWavesValidatorsAssets(chainValidatorsAssetsList)
break;
// case Solana:
// testSolanaValidatorsAssets(chainValidatorsAssetsList)
// break;
// TODO Add IoTex when taking suported by Trust
default:
break;
}
test("Make sure validator has corresponding logo", () => {
validatorsList.forEach(val => {
expect(chainValidatorsAssetsList.indexOf(val.id), `Expecting image asset for validator ${val.id} on chain ${chain}`)
.toBeGreaterThanOrEqual(0)
})
})
test("Make sure validator asset logo has corresponding info", () => {
chainValidatorsAssetsList.forEach(valAssetLogoID => {
expect(validatorsList.filter(v => v.id === valAssetLogoID).length, `Expect validator logo ${valAssetLogoID} to have info`)
.toBe(1)
})
})
})
})
})
function testTezosValidatorsAssets(assets: string[]) {
test("Tezos assets must be correctly formated tz1 address", () => {
assets.forEach(addr => {
expect(eztz.crypto.checkAddress(addr), `Ivalid Tezos address: ${addr}`).toBe(true)
})
})
}
function testTronValidatorsAssets(assets: string[]) {
test("TRON assets must be correctly formated", () => {
assets.forEach(addr => {
expect(isTRC20(addr), `Address ${addr} should be TRC20`).toBe(true)
})
})
}
function testWavesValidatorsAssets(assets: string[]) {
test("WAVES assets must have correct format", () => {
assets.forEach(addr => {
expect(isWavesAddress(addr), `Address ${addr} should be WAVES formated`).toBe(true)
})
})
}
// function testSolanaValidatorsAssets(assets: string[]) {
// test("Solana assets must have correct format", () => {
// assets.forEach(addr => {
// expect(isSolanaAddress(addr), `Address ${addr} should be Solana formated`).toBe(true)
// })
// })
// }
function testCosmosValidatorsAddress(assets: string[]) {
test("Cosmos assets must have correct format", () => {
assets.forEach(addr => {
expect(addr.startsWith("cosmosvaloper1"), `Address ${addr} should start from "cosmosvaloper1"`).toBe(true)
expect(addr.length, `Address ${addr} should have length 52`).toBe(52)
expect(isLowerCase(addr), `Address ${addr} should be in lowercase`).toBe(true)
})
})
}
function testKavaValidatorsAddress(assets: string[]) {
test("Kava assets must have correct format", () => {
assets.forEach(addr => {
expect(addr.startsWith("kavavaloper1"), `Address ${addr} should start from "kavavaloper1"`).toBe(true)
expect(addr.length, `Address ${addr} should have length 50`).toBe(50)
expect(isLowerCase(addr), `Address ${addr} should be in lowercase`).toBe(true)
})
})
}
function testTerraValidatorsAddress(assets: string[]) {
test("Terra assets must have correct format", () => {
assets.forEach(addr => {
expect(addr.startsWith("terravaloper1"), `Address ${addr} should start from "terravaloper1"`).toBe(true)
expect(addr.length, `Address ${addr} should have length 51`).toBe(51)
expect(isLowerCase(addr), `Address ${addr} should be in lowercase`).toBe(true)
})
})
}
describe("Test Coinmarketcap mapping", () => {
const cmcMap: mapTiker[] = JSON.parse(readFileSync("./pricing/coinmarketcap/mapping.json"))
test("Must have items", () => {
expect(cmcMap.length, `CMC map must have items`).toBeGreaterThan(0)
})
test(`Items must be sorted by "id" in ascending order`, () => {
cmcMap.forEach((el, i) => {
if (i > 0) {
const prevID = cmcMap[i - 1].id
const curID = el.id
expect(curID, `Item ${curID} must be greather or equal to ${prevID}`)
.toBeGreaterThanOrEqual(prevID)
}
})
})
test(`Items must be sorted by "coin" in ascending order if have same "id"`, () => {
cmcMap.forEach((el, i) => {
if (i > 0) {
const prevEl = cmcMap[i - 1]
const prevCoin = prevEl.coin
const prevID = cmcMap[i - 1].id
const curCoin = el.coin
const curID = el.id
if (prevID == curID) {
expect(curCoin, `Item ${JSON.stringify(el)} must be greather or equal to ${JSON.stringify(prevEl)}`)
.toBeGreaterThanOrEqual(prevCoin)
}
}
})
})
test("Properies value shoud not contain spaces", () => {
cmcMap.forEach(el => {
Object.keys(el).forEach(key => {
const val = el[key]
if (typeof val === "string") {
expect(val.indexOf(" ") >= 0, ` Property value "${val}" should not contain space`).toBe(false)
}
})
})
});
test("Params should have value and correct type", () => {
cmcMap.forEach(el => {
const {coin, type, id, token_id} = el
expect(typeof coin, `Coin ${coin} must be type "number"`).toBe("number")
expect(["token", "coin"], `Element with id ${id} has wrong type: "${type}"`).toContain(type)
if (type === "token") {
expect(token_id, `token_id ${token_id} with id ${id} must be type not empty`).toBeTruthy()
}
if (type === "coin") {
expect(el, `Element with id ${id} should not have property "token_id"`).not.toHaveProperty("token_id")
}
});
});
test(`"token_id" should be in correct format`, async () => {
const bep2Symbols = await getBinanceBEP2Symbols()
cmcMap.forEach(el => {
const {coin, token_id, type, id} = el
switch (coin) {
case 60:
if (type === TickerType.Token) {
expect(isChecksum(token_id), `"token_id" ${token_id} with id ${id} must be in checksum`).toBe(true)
break;
}
case 195:
if (type === TickerType.Token) {
expect(isTRC10(token_id) || isTRC20(token_id), `"token_id" ${token_id} with id ${id} must be in TRC10 or TRC20`).toBe(true)
break;
}
case 714:
if (type === TickerType.Token) {
expect(bep2Symbols.indexOf(token_id), `"token_id" ${token_id} with id ${id} must be BEP2 symbol`).toBeGreaterThan(0)
break;
}
default:
break;
}
})
})
test(`"token_id" shoud be unique`, () => {
const mappedList = cmcMap.reduce((acm, val) => {
if (val.hasOwnProperty("token_id")) {
if (acm.hasOwnProperty(val.token_id)) {
acm[val.token_id] == ++acm[val.token_id]
} else {
acm[val.token_id] = 0
}
}
return acm
}, {})
cmcMap.forEach(el => {
if (el.hasOwnProperty("token_id")) {
expect(mappedList[el.token_id], `CMC map ticker with "token_id" ${el.token_id} shoud be unique`).toBeLessThanOrEqual(0)
}
})
})
})
describe("Test blacklist and whitelist", () => {
const assetsChains = readDirSync(chainsFolderPath).filter(chain => isPathExistsSync(getChainAssetsPath(chain)))
assetsChains.forEach(chain => {
const whiteList = JSON.parse(readFileSync(getChainWhitelistPath(chain)))
const blackList = JSON.parse(readFileSync(getChainBlacklistPath(chain)))
test(`Whitelist should not contain assets from blacklist on ${chain} chain`, () => {
const blacklistMap = mapList(blackList)
whiteList.forEach(a => {
expect(a in blacklistMap, `Found whitelist asset ${a} in blacklist on chain ${chain}`).toBe(false)
})
})
test(`Blacklist should not contain assets from whitelist on ${chain} chain`, () => {
const whitelistMap = mapList(whiteList)
blackList.forEach(a => {
expect(a in whitelistMap, `Found blacklist asset ${a} in whitelist on chain ${chain}`).toBe(false)
})
})
})
})
describe("Test coins info.json file", () => {
});
describe("Test all JSON files to have valid content", () => {
const files = [
...findFiles(chainsFolderPath, 'json'),
...findFiles(pricingFolderPath, 'json')
]
files.forEach(file => {
expect(isValidJSON(file), `${file} path contains invalid JSON`).toBe(true)
});
})
describe("Test helper functions", () => {
test(`Test getHandle`, () => {
const urls = [
{
url: "https://twitter.com/aeternity",
expected: "aeternity"
},
{
url: "https://www.reddit.com/r/Aeternity",
expected: "Aeternity"
}
]
urls.forEach(u => {
expect(getHandle(u.url), `Getting handle from url ${u}`).toBe(u.expected)
})
})
});