-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
64 lines (56 loc) · 1.66 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const { TIME_TYPE } = require("./src/enums");
const { getTokenPrices, getTimeTypeTokenPrices } = require("./src/db");
const tokensData = require("./src/tokens.json");
const app = express();
app.use(cors());
app.options("*", cors());
app.get("/getPrices", async (req, res) => {
const { tokenAddress, fromTime, toTime } = req.query;
let prices = [];
try {
prices = await getTokenPrices(tokenAddress, fromTime, toTime);
} catch (e) {
console.log(e);
}
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(prices);
});
app.get("/getCandlePrices", async (req, res) => {
const { tokenAddress, fromTime, toTime } = req.query;
let timeType = TIME_TYPE.per_hour;
if (toTime - fromTime > 60 * 60 * 24 * 7) {
timeType = TIME_TYPE.per_day;
}
let prices = [];
// console.log(tokenAddress, fromTime, toTime, timeType);
try {
prices = await getTimeTypeTokenPrices(
tokenAddress,
timeType,
fromTime,
toTime
);
} catch (e) {
console.log(e);
}
console.log(prices);
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(prices);
});
app.get("/isRunning", async (_req, res) => {
res.send("running");
});
app.get("/getTokenMetadata", async (_req, res) => {
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(tokensData);
});
const port = process.env.PORT;
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`);
});