This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi.js
More file actions
153 lines (118 loc) · 2.85 KB
/
api.js
File metadata and controls
153 lines (118 loc) · 2.85 KB
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
const Sentry = require('@sentry/node')
const Web3 = require('web3')
const express = require('express')
const helmet = require('helmet')
const compression = require('compression')
const asyncHandler = require('express-async-handler')
const Transaction = require('./Transaction')
const httpError = require('./http-error')
const {
PORT,
WEB3_URI,
NODE_ENV
} = process.env
if (!PORT) throw new Error('Invalid PORT')
const web3 = new Web3(WEB3_URI)
const app = express()
if (NODE_ENV === 'production') {
app.use(Sentry.Handlers.requestHandler())
}
app.use(helmet())
app.use(compression())
app.set('etag', false)
app.get('/status', asyncHandler(async (req, res, next) => {
let { maxgap } = req.query
const [latest, tx] = await Promise.all([
web3.eth.getBlock('latest'),
Transaction.findOne({}).sort('-blockNumber').select('blockNumber').exec()
])
const difference = latest.number - tx.blockNumber
try {
maxgap = parseInt(maxgap)
if (!maxgap || maxgap < 1) throw new Error('Invalid maxgap')
} catch (e) {
maxgap = false
}
res.set('Access-Control-Allow-Origin', '*')
const status = {
latestBlockNumber: latest.number,
latestScrapedBlockNumber: tx.blockNumber,
difference
}
if (maxgap && difference > maxgap) {
res.status(503)
res.json({
status: 'ERROR',
data: { status }
})
return
}
res.json({
status: 'OK',
data: { status }
})
}))
app.get('/txs/:account', asyncHandler(async (req, res, next) => {
const { account } = req.params
let { limit, page, sort } = req.query
const q = Transaction.find({
$or: [
{ to: account },
{ from: account }
]
})
if (sort === 'asc') {
q.sort('blockNumber')
} else {
sort = 'desc'
q.sort('-blockNumber')
}
try {
page = parseInt(page)
if (!page || page < 1) throw new Error('Invalid page')
} catch (e) {
page = 1
}
try {
limit = parseInt(limit)
if (!limit || limit < 1 || limit > 250) throw new Error('Invalid limit')
} catch (e) {
limit = 250
}
q.limit(limit).skip(limit * (page - 1))
const [latest, txs] = await Promise.all([
web3.eth.getBlock('latest'),
q.exec()
])
const data = {}
if (limit || page) {
data.pagination = {
sort,
page,
limit
}
}
data.txs = txs.map(tx => {
const json = tx.toJSON()
delete json._id
delete json.verified
delete json.__v
json.confirmations = latest.number - tx.blockNumber
return json
})
res.set('Access-Control-Allow-Origin', '*')
res.json({
status: 'OK',
data
})
}))
app.use((err, req, res, next) => {
const status = err.statusCode || 500
const message = err.message || err.toString()
if (NODE_ENV !== 'production') {
console.error(err)
}
return httpError(req, res, status, message)
})
app.listen(PORT)
console.log(`API is running on ${PORT}`)