This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
150 lines (127 loc) · 4.21 KB
/
server.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
const express = require('express')
var cors = require('cors')
const bodyParser = require('body-parser')
const api = require('./httpApi')
// create express app
const app = express()
app.use(cors())
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse requests of content-type - application/json
app.use(bodyParser.json())
// middleware for parsing provider form req query
const providerMiddleware = (req, res, next) => {
req.provider = req.query.provider
next()
}
// define a simple route
app.get('/', (req, res) => {
res.json({'message': 'Welcome'})
})
app.get('/job/:jobId', providerMiddleware, async function (req, res) {
result = await api.getJobValues(req.params.jobId, req.provider)
// console.log('wut', result)
res2 = {}
for (let i = 0; i < result.length; i++) {
res2[i] = {
'value': result[i].value,
'timestamp': result[i].timestamp
}
}
// result = JSON.stringify(result)
// console.log(result)
res.json(res2)
})
app.get('/activeJobs/', async function (req, res) {
result = await api.getActiveJobs()
res.json({'message': result})
})
app.get('/jobs/', providerMiddleware, async function (req, res) {
result = await api.getJobs(req.provider)
res.json({'message': result})
})
app.get('/stakers/', async function (req, res) {
result = await api.getStakers()
res.json({result})
})
app.get('/stakerEvents/:address', async function (req, res) {
result = await api.getStakerEvents(req.params.address)
res.json({result})
})
app.post('/job/', async function (req, res) {
result = await api.createJob()
res.json({'message': result})
})
app.get('/votes/:jobId/', providerMiddleware, async function (req, res) {
result = await api.getVotesLastEpoch(req.params.jobId, req.provider)
res.json({'message': result})
})
app.get('/voteEvents/:jobId/', providerMiddleware, async function (req, res) {
result = await api.getVotingEvents(req.params.jobId, req.provider)
// console.log(result)
res.json({'message': result})
})
app.get('/voteEvents/', providerMiddleware, async function (req, res) {
result = await api.getVotingEvents(null, req.provider)
// console.log(result)
res.json({'message': result})
})
app.get('/jobEvents/', providerMiddleware, async function (req, res) {
result = await api.getJobEvents(req.provider)
// console.log(result)
res.json({'message': result})
})
app.get('/blockEvents/', providerMiddleware, async function (req, res) {
result = await api.getBlockEvents(req.provider)
// console.log(result)
res.json({'message': result})
})
app.get('/stakingEvents/', providerMiddleware, async function (req, res) {
result = await api.getStakingEvents(req.provider)
// console.log(result)
res.json({'message': result})
})
// app.get('/events/', async function (req, res) {
// result1 = await api.getStakingEvents()
// result = await api.getStakingEvents()
// console.log(result)
// res.json({'message': result})
// })
app.get('/epoch/', providerMiddleware, async function (req, res) {
result = await api.getEpoch(req.provider)
res.json({'message': result})
})
app.get('/poolChanges/', async function (req, res) {
result = await api.getPoolChanges()
res.json({'message': result})
})
app.get('/random/', async function (req, res) {
// result = await api.getEpoch()
result = Math.random() * 1000000
res.json({'message': result})
})
app.get('/getSchBalance/:address', async function (req, res) {
// result = await api.getEpoch()
result = await api.getSchBalance(req.params.address)
res.json({'message': result})
})
app.get('/razor/circulating', async function (req, res) {
// result = await api.getEpoch()
result = await api.getCirculatingSupply()
res.json(result)
})
app.get('/getStake/:address', async function (req, res) {
// result = await api.getEpoch()
let id = Number(await api.getStakerId(req.params.address))
result = await api.getStake(id)
res.json({'message': result})
})
app.get('/getEthBalance/:address', async function (req, res) {
// result = await api.getEpoch()
result = await api.getEthBalance(req.params.address)
res.json({'message': result})
})
// listen for requests
app.listen(3000, () => {
console.log('Server is listening on port 3000')
})