forked from mycelium-ethereum/swaps-liquidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
54 lines (46 loc) · 1.54 KB
/
index.ts
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
require("dotenv").config();
import liquidationHandler from "./api/liquidate";
import mongoose, { MongooseOptions } from "mongoose";
import express from "express";
import { Registry } from "prom-client";
import { resetMetrics, registerMetrics } from "./src/utils/prometheus";
import { sleep } from "./src/helpers/helpers";
const app = express();
const INTERVAL = process.env.INTERVAL_MS ? parseInt(process.env.INTERVAL_MS) : 60000;
const IS_PAUSED = process.env.IS_PAUSED === "true";
app.listen(process.env.PORT, async () => {
console.info("*** Server started on port " + process.env.PORT + " ***");
resetMetrics();
await connectDatabase();
if (!IS_PAUSED) {
startInterval();
}
});
const startInterval = async () => {
console.log(`*** Starting liquidator with interval ${INTERVAL}ms ***`);
while (true) {
await liquidationHandler();
await sleep(INTERVAL / 1000);
}
};
const connectDatabase = async () => {
try {
await mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as MongooseOptions);
console.log("Database connected!");
} catch (err) {
console.log("Failed to connect to database!", err);
}
};
app.get("/", function (_req, res) {
res.send("Tracer Swaps Liquidator");
});
// Prometheus metrics
const registry = new Registry();
registerMetrics(registry);
app.get("/metrics", async (req, res) => {
res.setHeader("Content-Type", registry.contentType);
res.send(await registry.metrics());
});