Skip to content

Feat: Utility function to find inconsistent TVL #8972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions defi/src/utils/findInconsistentTvl.ts
Original file line number Diff line number Diff line change
@@ -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<any | undefined>) {
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<string, any> = {};
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));