-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (70 loc) · 2.29 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
import express from "express";
import { createRequire } from "module";
import cors from "cors";
import helmet from "helmet";
import path from "path";
import { fileURLToPath } from "url";
import getEvents from "./getEvents.js";
import getTokenPrices from "./getTokenPrices.js";
import eventsComputePrices from "./eventsComputePrices.js";
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const strategies = require("./strategies.json");
const tokenDictionary = require("./tokenDictionary.json");
const testData = require("./dataTest.json");
const app = express();
// ** MIDDLEWARE ** //
const whitelist = [
"http://localhost:3000",
"http://localhost:5000",
"https://badger-dao-analytics.herokuapp.com/",
];
const corsOptions = {
origin: function (origin, callback) {
console.log("** Origin of request " + origin);
if (whitelist.indexOf(origin) !== -1 || !origin) {
console.log("Origin acceptable");
callback(null, true);
} else {
console.log("Origin rejected");
callback(new Error("Not allowed by CORS"));
}
},
};
app.use(helmet());
app.use(cors(corsOptions));
app.get("/strategies", (req, res) => {
res.json(strategies);
});
app.get("/tokenDictionary", (req, res) => {
res.json(tokenDictionary);
});
app.get("/events", async (req, res) => {
let request = req.query;
console.log("request", request);
console.time(`FullEventFetch`);
let events = await getEvents(request.strategy);
if (events.status === "0") {
console.log("Error EtherScan API", events.result);
res.json(events.result);
}
let tokenPrices = await getTokenPrices();
console.log("event", events[0]);
let eventsPrices = await eventsComputePrices(events, tokenPrices);
res.json(eventsPrices);
console.timeEnd(`FullEventFetch`);
});
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
process.on("unhandledRejection", (reason) => {
console.log("Unhandled Rejection at:", reason.stack || reason);
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, (req, res) => {
console.log(`server listening on port: ${PORT}`);
});