-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
165 lines (158 loc) · 4.81 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require("dotenv").config();
const express = require("express");
const _ = require("lodash");
const bodyParser = require("body-parser");
const RTokenAnalytics = require("rtoken-analytics");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger-config.json");
var cors = require("cors");
global.XMLHttpRequest = require("xhr2");
const options = {
infuraEndpointKey: process.env.INFURA_ENDPOINT_KEY,
};
const analytics = new RTokenAnalytics(options);
var app = express();
app.set("port", 3001);
if (process.env.NODE_ENV === "production") {
app.set("port", 4001);
}
app.use(bodyParser.json());
app.use(cors());
app.get("/v1/allOutgoing", async (req, res) => {
try {
const owner = req.query.owner;
let outgoing = await analytics.getAllOutgoing(owner);
res.send(outgoing);
} catch (err) {
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/allIncoming", async (req, res) => {
try {
const owner = req.query.owner;
let incoming = await analytics.getAllIncoming(owner);
res.send(incoming);
} catch (err) {
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/interestSent", async (req, res) => {
try {
const from = req.query.from;
const to = req.query.to;
let interest = await analytics.getInterestSent(
from.toLowerCase(),
to.toLowerCase()
);
res.send(interest.toString());
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
// High Priests additions
app.get("/v1/receivedSavingsOf", async (req, res) => {
try {
const owner = req.query.owner;
let savings = await analytics.receivedSavingsOf(owner.toLowerCase());
res.send({ amount: savings.toString() });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/receivedSavingsOfByHat", async (req, res) => {
try {
const hatID = req.query.hatID;
let savings = await analytics.receivedSavingsOfByHat(hatID);
res.send({ amount: savings.toString() });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/amountEarnedByHat", async (req, res) => {
// !! Danger: this does not track which has been withdrawn by the recipient
try {
const hatID = req.query.hatID;
let earned = await analytics.amountEarnedByHat(hatID);
res.send({ amount: earned.toString() });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/getHatIDByAddress", async (req, res) => {
try {
const owner = req.query.owner;
let hatID = await analytics.getHatIDByAddress(owner);
res.send({ hatID: hatID });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/allUsersWithHat", async (req, res) => {
try {
const hatID = req.query.hatID;
let users = await analytics.allUsersWithHat(hatID);
res.send(users);
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/allUsersWithHat", async (req, res) => {
try {
const hatID = req.query.hatID;
let users = await analytics.allUsersWithHat(hatID);
res.send(users);
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/userContributionToHat", async (req, res) => {
try {
const hatID = req.query.hatID;
const owner = req.query.owner;
let amount = await analytics.userContributionToHat(hatID, owner);
res.send({ amount: amount.toString() });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/getTopDonorByHatGroup", async (req, res) => {
try {
const hats = req.query.hats;
let donor = await analytics.getTopDonorByHatGroup(hats);
res.send(donor);
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
app.get("/v1/sortHatsByReceivedSavingsOf", async (req, res) => {
try {
const hats = req.query.hats;
let sortedHats = await analytics.sortHatsByReceivedSavingsOf(hats);
res.send({ sortedHats });
} catch (err) {
console.log(err);
res.status(500).send(`Error fetching data: "${err}"`);
}
});
var swaggerOptions = {
customCssUrl: "./swagger.css",
};
app.use("/", swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions));
app.listen(app.get("port"), () => {
console.log(
`_______________________________________________________________\n`
);
console.log(`################# rToken API Server ####################\n`);
console.log(`Started on port ${app.get("port")}`);
console.log(`______________________________________________________________`);
});
module.exports = { app };