-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
129 lines (108 loc) · 3.15 KB
/
index.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
const HyperExpress = require('hyper-express')
let airdropConfig = require('./airdrop-config.json')
const env = require('./env')
const { initDB, getKeys } = require('./db')
const webserver = new HyperExpress.Server()
async function main() {
const timeKey = 'Airdrop tracker Server init'
console.time(timeKey)
webserver.use((_req, res, next) => {
res.append('Access-Control-Allow-Origin', '*')
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
next()
});
initDB()
let router = webserver
/*
const subPath = env.API2_SUBPATH
if (subPath) {
router = new HyperExpress.Router()
webserver.use('/' + subPath, router)
}
*/
router.get('/config', getAirdropConfig)
router.get('/check/:keys', getAirdropInfo)
router.get('/eigen/:key', getAirdropEigen)
router.get('/eigens/:key', getAirdropEigens)
webserver.listen(env.PORT)
.then(() => {
console.timeEnd(timeKey)
console.log('Webserver started on port ' + env.PORT)
})
.catch((e) => console.log('Failed to start webserver on port ' + env.PORT, e))
}
main()
async function getAirdropConfig(_, res) {
successResponse(res, airdropConfig)
}
async function getAirdropInfo(req, res) {
try {
const keys = req.params.keys.split(',')
const data = await getKeys(keys)
successResponse(res, data)
} catch (e) {
console.error(e)
errorResponse(res, e)
}
}
async function getAirdropEigen(req, res) {
try {
const address = req.params.key
const data = await getEigenData(address)
successResponse(res, data)
} catch (e) {
console.error(e)
errorResponse(res, e)
}
}
function getEigenData(address) {
return fetch(
`https://claims.eigenfoundation.org/clique-eigenlayer-api/campaign/eigenlayer/credentials?walletAddress=${address.toLowerCase()}`
)
.then((r) => r.json())
.then((r) => r.data.pipelines)
}
async function getAirdropEigens(req, res) {
try {
const addresses = req.params.key.split(',')
const response = {}
for (const address of addresses) {
response[address] = await getEigenData(address)
}
successResponse(res, response)
} catch (e) {
console.error(e)
errorResponse(res, e)
}
}
function successResponse(res, data, cacheMinutes = 30, {
isJson = true,
isPost = false,
} = {}) {
res.setHeaders({
"Expires": getTimeInFutureMinutes(cacheMinutes) // cache for 5 minutes
})
if (isPost)
res.removeHeader("Expires")
isJson ? res.json(data) : res.send(data)
}
function errorResponse(res, data = 'Internal server error', {
statusCode = 400,
} = {}) {
res.status(statusCode)
res.send(data, true)
}
function getTimeInFutureMinutes(minutes) {
const date = new Date();
// add five minutes to the current time
date.setMinutes(date.getMinutes() + minutes);
return date.toUTCString()
}
//pulls config from github every 3 hours
setInterval(() => updateConfig, 1000 * 60 * 60 * 3)
function updateConfig() {
fetch('https://raw.githubusercontent.com/DefiLlama/airdrop-checker/master/airdrop-config.json')
.then(res => res.json())
.then((data) => airdropConfig = data)
.catch((e) => console.error('Failed to fetch airdrop config', e))
}