From 5c6edca53e2f0194d137f376220b665155d1c631 Mon Sep 17 00:00:00 2001 From: 0xpeluche <0xpeluche@proton.me> Date: Thu, 2 Jan 2025 16:26:46 +0100 Subject: [PATCH] Find inconsistent tvl (negative..) --- defi/src/utils/findInconsistentTvl.ts | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 defi/src/utils/findInconsistentTvl.ts diff --git a/defi/src/utils/findInconsistentTvl.ts b/defi/src/utils/findInconsistentTvl.ts new file mode 100644 index 0000000000..3ae7c1a622 --- /dev/null +++ b/defi/src/utils/findInconsistentTvl.ts @@ -0,0 +1,68 @@ +import { getLatestProtocolItems, initializeTVLCacheDB } from "../api2/db"; +import protocols from "../protocols/data"; +import treasuries from "../protocols/treasury"; +import { dailyTvl, hourlyTvl } from "./getLastRecord"; +import { importAdapter } from "./imports/importAdapter"; + +type InfoProtocol = { + time: number, + tvl: number +} | null; + +export async function getInconsistentTvl(getLatestTvl: (protocol: any) => Promise) { + const inconsistent = [] as [string, InfoProtocol][]; + const allProtocols = protocols.concat(treasuries); + + await Promise.all( + allProtocols.map(async (protocol) => { + if (protocol.rugged === true || protocol.module === "dummy.js") + return; + + const item = await getLatestTvl(protocol); + if (!item) + return; + + let module; + try { + module = await importAdapter(protocol); + } catch { + return; + } + if (module.deadFrom) { + return; + } + if (item.tvl < 0) { + inconsistent.push([protocol.name, { time: item.SK, tvl: item.tvl }]); + } + }) + ); + + return inconsistent; +} + +export async function findInconsistentTvlPG() { + await initializeTVLCacheDB(); + const latestProtocolItems = await getLatestProtocolItems(hourlyTvl, { + filterLast24Hours: true, + }); + const latestProtocolItemsDaily = await getLatestProtocolItems(dailyTvl); + const latestProtocolItemsMap: Record = {}; + latestProtocolItems.forEach((data: any) => { + latestProtocolItemsMap[data.id] = data.data; + }); + latestProtocolItemsDaily.forEach((data: any) => { + if (!latestProtocolItemsMap[data.id]) { + latestProtocolItemsMap[data.id] = data.data; + } + }); + return getInconsistentTvl(async (protocol: any) => { + return latestProtocolItemsMap[protocol.id]; + }); +} + +export default async function findInconsistentTvl() { + const inconsistent = await findInconsistentTvlPG(); + return inconsistent; +} + +findInconsistentTvl().then(r => console.log(r));