-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathverify.js
157 lines (124 loc) · 4.41 KB
/
verify.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
const child_process = require("node:child_process");
const fs = require("node:fs");
const ethers = require("ethers");
const imageSize = require("image-size");
const logos = {};
for (let i = 0; i < fs.readdirSync("logo/").length; i++) {
const file = fs.readdirSync("logo/")[i];
logos[file] = true;
}
for (const file of Object.keys(logos)) {
const info = imageSize(`logo/${file}`);
if (info.type !== "svg" && info.type !== "png") {
throw Error(`logo file ${file} is not SVG/PNG`);
}
if (info.height !== info.width && file !== "swaap.png")
throw Error(
`logo dimensions not square: ${file} (${info.height} x ${info.width})`,
);
}
for (const file of fs.readdirSync(".")) {
if (!/^\d+$/.test(file)) continue;
validateChain(file);
}
console.log("OK");
///////////
function validateChain(chainId) {
const entities = loadJsonFile(`${chainId}/entities.json`);
const vaults = loadJsonFile(`${chainId}/vaults.json`);
const products = loadJsonFile(`${chainId}/products.json`);
const points = loadJsonFile(`${chainId}/points.json`);
for (const entityId of Object.keys(entities)) {
const entity = entities[entityId];
if (!validSlug(entityId))
throw Error(`entities: invalid slug: ${entityId}`);
if (!entity.name) throw Error(`entities: missing name: ${entityId}`);
for (const addr of Object.keys(entity.addresses || {})) {
if (addr !== ethers.getAddress(addr))
throw Error(`entities: malformed address: ${addr}`);
}
if (entity.logo && !logos[entity.logo])
throw Error(`entities: logo not found: ${entity.logo}`);
}
for (const vaultId of Object.keys(vaults)) {
const vault = vaults[vaultId];
if (vaultId !== ethers.getAddress(vaultId))
throw Error(`vaults: malformed vaultId: ${vaultId}`);
if (!vault.name) throw Error(`vaults: missing name: ${vaultId}`);
for (const entity of getArray(vault.entity)) {
if (!entities[entity])
throw Error(`vaults: no such entity ${vault.entity}`);
}
}
const vaultsSeenInProducts = {};
for (const productId of Object.keys(products)) {
const product = products[productId];
if (!validSlug(productId))
throw Error(`products: invalid slug: ${entityId}`);
if (!product.name) throw Error(`products: missing name: ${productId}`);
for (const addr of product.vaults) {
if (addr !== ethers.getAddress(addr))
throw Error(
`products: malformed vault address: ${ethers.getAddress(addr)}`,
);
if (!vaults[addr]) throw Error(`products: unknown vault: ${addr}`);
if (vaultsSeenInProducts[addr])
throw Error(`products: vault in multiple products: ${addr}`);
vaultsSeenInProducts[addr] = true;
}
for (const entity of getArray(product.entity)) {
if (!entities[entity]) throw Error(`products: no such entity ${entity}`);
}
if (product.logo && !logos[product.logo])
throw Error(`products: logo not found: ${product.logo}`);
}
for (const vaultId of Object.keys(vaults)) {
if (!vaultsSeenInProducts[vaultId])
throw Error(`vault does not exist in product: ${vaultId}`);
}
for (const point of points) {
if (point.token && point.token !== ethers.getAddress(point.token))
throw Error(`points: malformed token: ${point.token}`);
if (!point.name) throw Error(`points: missing name: ${point.name}`);
if (point.url && !validUrl(point.url))
throw Error(`points: missing name: ${point.name}`);
if (point.logo && !logos[point.logo])
throw Error(`points: logo not found: ${product.logo}`);
if (point.skipValidation) continue;
if (!point.collateralVaults?.length && !point.liabilityVaults?.length) {
throw Error(
`points: missing collateral or liability vaults for ${point.name}`,
);
}
if (point.collateralVaults) {
for (const addr of point.collateralVaults) {
if (addr !== ethers.getAddress(addr))
throw Error(`points: malformed vault address: ${addr}`);
}
}
if (point.liabilityVaults) {
for (const addr of point.liabilityVaults) {
if (addr !== ethers.getAddress(addr))
throw Error(`points: malformed vault address: ${addr}`);
}
}
for (const entity of getArray(point.entity)) {
if (!entities[entity]) throw Error(`points: no such entity ${entity}`);
}
}
}
function loadJsonFile(file) {
return JSON.parse(fs.readFileSync(file).toString());
}
function validSlug(slug) {
return /^[a-z0-9-]+$/.test(slug);
}
function validUrl(url) {
return /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,63}(\/[^\s]*)?$/.test(
url,
);
}
function getArray(v) {
if (Array.isArray(v)) return v;
return [v];
}