-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolygonscan.js
62 lines (53 loc) · 1.59 KB
/
polygonscan.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
const fs = require('fs')
const path = require('path')
const delay = require('delay')
const axios = require('axios')
const chalk = require('chalk')
const polygonscan = axios.create({
baseURL: 'https://api.polygonscan.com/',
timeout: 5000,
params: { apikey: process.env.POLYGONSCAN_API_KEY }
})
const verifiedCachePath = path.join(__dirname, './out/verifiedCache.json')
let verifiedCache
if (fs.existsSync(verifiedCachePath)) {
verifiedCache = JSON.parse(fs.readFileSync(verifiedCachePath, 'utf-8'))
} else {
verifiedCache = {}
}
function saveVerifiedCache () {
fs.writeFileSync(verifiedCachePath, JSON.stringify(verifiedCache), 'utf-8')
}
async function contractVerified (address, tryCount) {
if (typeof verifiedCache[address] !== 'undefined') return verifiedCache[address]
if (!tryCount) tryCount = 0
tryCount++
process.stdout.write(chalk.dim(`Checking to see if ${address} has a verified source...`))
await delay(400)
let resp
try {
resp = await polygonscan.get('/api', {
params: {
module: 'contract',
action: 'getabi',
address: address
}
})
} catch (err) {
console.error(chalk.red('polygonscan API error'))
if (tryCount < 5) {
await delay(2000)
return contractVerified(address, tryCount) // retry after delay
} else {
saveVerifiedCache()
throw new Error('cannot continue without polygonscan')
}
}
verifiedCache[address] = resp.data && resp.data.status === '1'
console.log(chalk.dim(verifiedCache[address]))
return verifiedCache[address]
}
module.exports = {
contractVerified,
saveVerifiedCache
}