forked from ethereum-optimism/ethereum-optimism.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
344 lines (321 loc) · 11.4 KB
/
validate.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
import fs from 'fs'
import path from 'path'
import { URLSearchParams } from 'url'
import { glob } from 'glob'
import fetch from 'node-fetch'
import { Validator } from 'jsonschema'
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
import { schema } from '@uniswap/token-lists'
import { ethers } from 'ethers'
import { sleep } from '@eth-optimism/core-utils'
import { getContractInterface } from '@eth-optimism/contracts'
import { generate } from './generate'
import { TOKEN_DATA_SCHEMA } from './schemas'
import {
L2_STANDARD_BRIDGE_INFORMATION,
L2_TO_L1_PAIR,
NETWORK_DATA,
} from './chains'
import { TOKEN_ABI } from './abi'
import {
Chain,
ExpectedMismatches,
L2Chain,
TokenData,
ValidationResult,
} from './types'
/**
* Validates a token list data folder.
*
* @param datadir Directory containing data files.
* @param tokens List of tokens to run validation on.
*
* @return Validation results.
*/
export const validate = async (
datadir: string,
tokens: string[]
): Promise<ValidationResult[]> => {
// Load data files to validate and filter for requested tokens
console.log(tokens)
const folders = fs
.readdirSync(datadir)
.sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase())
})
.filter((folder) => {
return !tokens || tokens.includes(folder)
})
const results = []
// Load the CoinGecko tokenlist once to avoid additional requests
let cgret
let cg
try {
cgret = await fetch('https://tokens.coingecko.com/uniswap/all.json')
cg = await cgret.json()
} catch (err) {
console.error('fetch for CoinGecko token list failed', err)
results.push({
type: 'warning',
message: 'fetch for CoinGecko token list failed',
})
}
for (const folder of folders) {
// Make sure the data file exists
const datafile = path.join(datadir, folder, 'data.json')
if (!fs.existsSync(datafile)) {
results.push({
type: 'error',
message: `data file ${datafile} does not exist`,
})
}
// Load the data now that we know it exists
const data: TokenData = JSON.parse(fs.readFileSync(datafile, 'utf8'))
// Make sure ONE logo file exists
const logofiles = glob.sync(`${path.join(datadir, folder)}/logo.{png,svg}`)
if (logofiles.length !== 1) {
results.push({
type: 'error',
message: `${folder} has ${logofiles.length} logo files, make sure your logo is either logo.png OR logo.svg`,
})
}
const expectedMismatchesFilePath = path.join(
datadir,
folder,
'expectedMismatches.json'
)
const expectedMismatches: ExpectedMismatches = fs.existsSync(
expectedMismatchesFilePath
)
? JSON.parse(fs.readFileSync(expectedMismatchesFilePath, 'utf8'))
: {}
// Validate the data file
const v = new Validator()
const result = v.validate(data, TOKEN_DATA_SCHEMA as any)
if (!result.valid) {
for (const error of result.errors) {
results.push({
type: 'error',
message: `${folder}: ${error.property}: ${error.message}`,
})
}
// Since the data file is invalid, we can't continue validating the rest of the files
continue
}
// Validate each token configuration
for (const [chain, token] of Object.entries(data.tokens)) {
// Validate any standard tokens
if (folder !== 'ETH' && data.nonstandard !== true) {
const networkData = NETWORK_DATA[chain as Chain]
const contract = new ethers.Contract(
token.address,
TOKEN_ABI,
networkData.provider
)
// Check that the token exists on this chain
if ((await contract.provider.getCode(token.address)) === '0x') {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} does not exist`,
})
}
// Check that the token has the correct decimals
if (token.overrides?.decimals === undefined) {
try {
if (data.decimals !== (await contract.decimals())) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} has incorrect decimals`,
})
}
} catch (err) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} failed to get decimals`,
})
}
} else {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} has overridden decimals`,
})
}
// Check that the token has the correct symbol
if (token.overrides?.symbol === undefined) {
try {
if (
data.symbol !== (await contract.symbol()) &&
expectedMismatches.symbol !== data.symbol
) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} has incorrect symbol`,
})
}
} catch (err) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} failed to get symbol`,
})
}
} else {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} has overridden symbol`,
})
}
// Check that the token has the correct name
if (token.overrides?.name === undefined) {
try {
const name = await contract.name()
if (data.name !== name && expectedMismatches.name !== data.name) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} has incorrect name. Got ${name}`,
})
}
} catch (err) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} failed to get name`,
})
}
} else {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} has overridden name`,
})
}
// Check that the Ethereum token exists in the CG token list
if (chain === 'ethereum' && cg !== undefined) {
const found = cg.tokens.find((t) => {
return t.address.toLowerCase() === token.address.toLowerCase()
})
// Trigger manual review if the Ethereum token is not in the CG token list
if (!found) {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} not found on CoinGecko token list`,
})
}
}
if (networkData.layer === 2) {
if (!data.nobridge) {
if (token.overrides?.bridge === undefined) {
try {
const tokenContract = new ethers.Contract(
token.address,
getContractInterface('L2StandardERC20'),
networkData.provider
)
const l2Bridge = (await tokenContract.l2Bridge()) as string
// Trigger review if the bridge for the token is not set
// to the standard bridge address.
if (
l2Bridge?.toUpperCase() !==
L2_STANDARD_BRIDGE_INFORMATION[
chain as L2Chain
].l2StandardBridgeAddress.toUpperCase()
) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} not using standard bridge`,
})
}
const l1Token = (await tokenContract.l1Token()) as string
const l1Chain = L2_TO_L1_PAIR[chain as Chain]
const l1ChainData = data.tokens[l1Chain]
if (
l1ChainData &&
l1ChainData.address.toUpperCase() !== l1Token.toUpperCase()
) {
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} does not match L1 token address`,
})
}
} catch (e) {
console.error(
`${folder} on chain ${chain} token ${token.address} could not fetch l2Bridge or l1Token`,
e
)
results.push({
type: 'error',
message: `${folder} on chain ${chain} token ${token.address} could not fetch l2Bridge or l1Token.
This token most likely needs nobridge or a bridge override set.`,
})
}
} else {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} has an overridden bridge`,
})
}
}
} else {
try {
// Make sure the token is verified on Etherscan.
// Etherscan API is heavily rate limited, so sleep for 1s to avoid errors.
await sleep(1000)
const { result: etherscanResult } = await (
await fetch(
`https://api${
chain === 'ethereum' ? '' : `-${chain}`
}.etherscan.io/api?` +
new URLSearchParams({
module: 'contract',
action: 'getsourcecode',
address: token.address,
// If we ever get rate limited by etherscan uncomment this line and add a method for
// fetching the appropriate etherscan api key based on the chain.
// https://linear.app/optimism/issue/FE-1396
// apikey: getEtherscanApiKey(),
})
)
).json()
// Trigger review if code not verified on Etherscan
if (
etherscanResult[0].ABI === 'Contract source code not verified'
) {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} code not verified on Etherscan`,
})
}
} catch (e) {
console.error(
`unable to fetch etherscan ${folder} on chain ${chain} token ${token.address}`,
e
)
results.push({
type: 'warning',
message: `Etherscan API request failed for ${folder} on chain ${chain} token ${token.address}`,
})
}
}
} else {
results.push({
type: 'warning',
message: `${folder} on chain ${chain} token ${token.address} is a nonstandard token`,
})
}
}
}
// Verify that the final generated token list is valid
const list = generate(datadir)
const ajv = new Ajv({ allErrors: true, verbose: true })
addFormats(ajv)
const validator = ajv.compile(schema)
if (!validator(list)) {
results.push({
type: 'error',
message: `final token list is invalid: ${JSON.stringify(
validator.errors,
null,
2
)}`,
})
}
return results
}