forked from DefiLlama/DefiLlama-Adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
125 lines (117 loc) · 4 KB
/
test.ts
File metadata and controls
125 lines (117 loc) · 4 KB
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
import * as path from "path";
import * as fs from "fs";
import { ethers } from "ethers";
import { providers } from "./utils/ethers";
import { util } from "@defillama/sdk";
import { TOTAL_BINS, Bins, binResults } from "./utils/binResults";
import { Liq } from "./utils/types";
import { config } from "dotenv";
import { performance } from "perf_hooks";
config();
const f2 = (n: number) => Number(n.toFixed(2));
async function displayDebugInfo(skippedTokens: Set<string>, liqs: Liq[], bins: Bins) {
let sumLiquidable = 0;
const liquidableTable = [] as any[];
Object.entries(bins).map(([symbol, tokenLiqs]) => {
console.log(`${symbol} (current price: ${tokenLiqs.price})`);
const max = Object.values(tokenLiqs.bins).reduce((max, bin) => Math.max(max, bin), 0);
for (let i = 0; i < TOTAL_BINS; i++) {
const amountInBin = tokenLiqs.bins[i] ?? 0;
const range = (n: number) => (tokenLiqs.binSize * n).toFixed(2);
console.log(
`${"#".repeat((amountInBin / max) * 10).padEnd(10)} = ${range(i)}-${range(i + 1)} = ${amountInBin.toFixed(2)}`
);
}
let sumInside = 0;
let sumOutside = 0;
Object.entries(tokenLiqs.bins).map(([i, amount]) => {
if (Number(i) < TOTAL_BINS) {
sumInside += amount;
} else {
sumOutside += amount;
}
});
if (sumOutside > 0) {
const percentLiquidable = (100 * sumOutside) / (sumInside + sumOutside);
const liquidable = tokenLiqs.price * sumOutside;
sumLiquidable += liquidable;
liquidableTable.push({
symbol,
percentLiquidable: f2(percentLiquidable),
totalLiquidableTokens: f2(sumOutside),
totalLiquidableUSD: liquidable,
});
}
console.log("");
});
const skippedTable = await Promise.all(
Array.from(skippedTokens).map(async (tokenAddress) => {
const [chain, address] = tokenAddress.split(":");
if (chain.toLowerCase() === "solana") {
return {
symbol: "UNKNOWN",
address: tokenAddress,
};
}
const tokenContract = new ethers.Contract(address, ["function symbol() view returns (string)"], providers[chain]);
let symbol: string;
try {
symbol = await tokenContract.symbol();
} catch (e) {
symbol = "UNKNOWN";
}
return {
symbol,
address: tokenAddress,
};
})
);
if (skippedTable.length > 0) {
console.log(`The following tokens couldn't be priced and have been skipped:`);
console.table(skippedTable);
}
if (sumLiquidable > 0) {
console.log("\nLiquidable positions:");
console.table(
liquidableTable
.sort((a, b) => b.totalLiquidableUSD - a.totalLiquidableUSD)
.concat([
{
symbol: "Total",
percentLiquidable: "-",
totalLiquidableTokens: "-",
totalLiquidableUSD: sumLiquidable,
},
])
.map((o) => ({
...o,
totalLiquidableUSD: util.humanizeNumber.humanizeNumber(o.totalLiquidableUSD),
}))
);
console.log("If this number is high double check your data!");
}
console.log(liqs);
}
async function main() {
const passedFile = path.resolve(process.cwd(), process.argv[2]);
let module = {} as {
[chain: string]: { liquidations: () => Promise<Liq[]> };
};
try {
module = require(passedFile);
} catch (e) {
console.log(e);
}
const startTime = performance.now();
const liqs = (await Promise.all(Object.values(module).map((m) => m.liquidations()))).flat();
const endTime = performance.now();
// // write liqs to disk as JSON
// fs.writeFileSync(path.resolve(process.cwd(), "liquidations2.json"), JSON.stringify(liqs, null, 2));
const { skippedTokens, bins } = await binResults(liqs);
await displayDebugInfo(skippedTokens, liqs, bins);
//console.log(liqs)
console.log(`\nSize of all liquidation data: ${JSON.stringify(liqs).length / 10 ** 6} MB`);
console.log(`Took ${f2((endTime - startTime) / 1000)} seconds to fetch liquidations`);
process.exit(0);
}
main();