-
Notifications
You must be signed in to change notification settings - Fork 226
/
index.ts
132 lines (113 loc) · 4.17 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
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
import * as fs from "fs/promises";
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import { performance } from "perf_hooks";
import { Liq } from "./DefiLlama-Adapters/liquidations/utils/types";
import * as _venus from "./DefiLlama-Adapters/liquidations/venus";
// import * as _aaveV2 from "./DefiLlama-Adapters/liquidations/aave-v2";
import maker from "./additional-adapters/maker";
interface LiquidationAdapter {
// chain name
[chain: string]: {
liquidations: () => Promise<Liq[]>;
};
}
const venus = (_venus as any).default as LiquidationAdapter;
// const aaveV2 = (_aaveV2 as any).default as LiquidationAdapter;
dotenv.config();
const STORE = {};
const app: Express = express();
const port = process.env.PORT;
const handleLiqsReq = (protocol: string, chain: string) => async (_req: Request, res: Response) => {
try {
const data = await getCachedLiqs(protocol, chain);
if (data) {
res.setHeader("Content-Type", "application/json");
res.setHeader("Cache-Control", "public, max-age=1200");
res.send(data);
} else {
res.setHeader("Cache-Control", "public, max-age=600");
res.status(404).send("No data");
}
} catch (e) {
console.error(e);
res.setHeader("Cache-Control", "public, max-age=600");
res.status(500).send("Internal Server Error");
}
};
app.get("/venus/bsc", handleLiqsReq("venus", "bsc"));
// app.get("/aave-v2/ethereum", handleLiqsReq("aave-v2", "ethereum"));
// app.get("/aave-v2/polygon", handleLiqsReq("aave-v2", "polygon"));
app.get("/maker/ethereum", handleLiqsReq("maker", "ethereum"));
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
});
const fetchVenusLiquidations = async () => {
await Promise.all(
Object.entries(venus).map(async ([chain, liquidationsFunc]) => {
try {
const _start = performance.now();
console.log(`Fetching ${"venus"} data for ${chain}`);
const liquidations = await liquidationsFunc.liquidations();
await storeCachedLiqs("venus", chain, JSON.stringify(liquidations));
const _end = performance.now();
console.log(`Fetched ${"venus"} data for ${chain} in ${((_end - _start) / 1000).toLocaleString()}s`);
} catch (e) {
console.error(e);
}
}),
);
};
// const fetchAaveV2Liquidations = async () => {
// for (const chain of Object.keys(aaveV2)) {
// if (chain === "ethereum") {
// continue;
// }
// try {
// const _start = performance.now();
// console.log(`Fetching ${"aave-v2"} data for ${chain}`);
// const liquidations = await aaveV2[chain].liquidations();
// await storeCachedLiqs("aave-v2", chain, JSON.stringify(liquidations));
// const _end = performance.now();
// console.log(`Fetched ${"aave-v2"} data for ${chain} in ${((_end - _start) / 1000).toLocaleString()}s`);
// } catch (e) {
// console.error(e);
// }
// }
// };
const fetchMakerLiquidations = async () => {
for (const chain of Object.keys(maker)) {
try {
const _start = performance.now();
console.log(`Fetching ${"maker"} data for ${chain}`);
const liquidations = await maker[chain].liquidations();
await storeCachedLiqs("maker", chain, JSON.stringify(liquidations));
const _end = performance.now();
console.log(`Fetched ${"maker"} data for ${chain} in ${((_end - _start) / 1000).toLocaleString()}s`);
} catch (e) {
console.error(e);
}
}
};
const storeCachedLiqs = async (protocol: string, chain: string, data: string) => {
delete STORE[protocol];
STORE[protocol] = { [chain]: data };
};
const getCachedLiqs = async (protocol: string, chain: string) => {
const data = STORE[protocol]?.[chain];
return data;
};
// run every 30 minutes
setInterval(fetchVenusLiquidations, 1000 * 60 * 30);
fetchVenusLiquidations();
// // run every 30 minutes but delayed by 20 minutes
// setTimeout(() => {
// setInterval(fetchAaveV2Liquidations, 1000 * 60 * 30);
// fetchAaveV2Liquidations();
// // }, 1000 * 60 * 20);
// }, 0);
// run every 30 minutes
setTimeout(() => {
setInterval(fetchMakerLiquidations, 1000 * 60 * 30);
fetchMakerLiquidations();
}, 0);