forked from DefiLlama/DefiLlama-Adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
177 lines (167 loc) · 5.96 KB
/
test.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
const path = require("path");
require('dotenv').config();
const { default: computeTVL } = require("@defillama/sdk/build/computeTVL");
const { getCurrentBlocks } = require("@defillama/sdk/build/computeTVL/blocks");
const { humanizeNumber } = require("@defillama/sdk/build/computeTVL/humanizeNumber");
const { util } = require("@defillama/sdk");
async function getBlocks() {
for (let i = 0; i < 5; i++) {
try {
return await getCurrentBlocks();
} catch (e) {
throw new Error("Couln't get block heights", e)
}
}
}
const locks = [];
function getCoingeckoLock() {
return new Promise((resolve) => {
locks.push(resolve);
});
}
function releaseCoingeckoLock() {
const firstLock = locks.shift();
if (firstLock !== undefined) {
firstLock(null);
}
}
// Rate limit is 50 calls/min for coingecko's API
// So we'll release one every 1.2 seconds to match it
setInterval(() => {
releaseCoingeckoLock();
}, 2000);
const maxCoingeckoRetries = 5;
async function getTvl(unixTimestamp, ethBlock, chainBlocks,
usdTvls,
tokensBalances,
usdTokenBalances,
tvlFunction,
isFetchFunction,
storedKey,
knownTokenPrices
) {
if (!isFetchFunction) {
const tvlBalances = await tvlFunction(
unixTimestamp,
ethBlock,
chainBlocks
);
const tvlResults = await computeTVL(
tvlBalances,
"now",
false,
knownTokenPrices,
getCoingeckoLock,
maxCoingeckoRetries
);
usdTvls[storedKey] = tvlResults.usdTvl;
tokensBalances[storedKey] = tvlResults.tokenBalances;
usdTokenBalances[storedKey] = tvlResults.usdTokenBalances;
} else {
usdTvls[storedKey] = Number(await tvlFunction(
unixTimestamp,
ethBlock,
chainBlocks
));
}
if (
typeof usdTvls[storedKey] !== "number" ||
Number.isNaN(usdTvls[storedKey])
) {
throw new Error(
`TVL for key ${storedKey} is not a number, instead it is ${usdTvls[storedKey]}`
);
}
}
function mergeBalances(key, storedKeys, balancesObject) {
if (balancesObject[key] === undefined) {
balancesObject[key] = {}
storedKeys.map(keyToMerge => {
Object.entries(balancesObject[keyToMerge]).forEach((balance) => {
util.sumSingleBalance(balancesObject[key], balance[0], balance[1]);
});
})
}
}
if (process.argv.length < 3) {
console.error(`Missing argument, you need to provide the filename of the adapter to test.
Eg: node test.js projects/myadapter.js`);
process.exit(1);
}
const passedFile = path.resolve(process.cwd(), process.argv[2]);
(async () => {
const module = require(passedFile);
const { timestamp:unixTimestamp, ethereumBlock:ethBlock, chainBlocks } = await getBlocks();
const usdTvls = {};
const tokensBalances = {};
const usdTokenBalances = {};
const chainTvlsToAdd = {};
const knownTokenPrices = {};
let tvlPromises = Object.entries(module).map(async ([chain, value]) => {
if (typeof value !== "object" || value === null) {
return;
}
return Promise.all(Object.entries(value).map(async ([tvlType, tvlFunction]) => {
if (typeof tvlFunction !== "function") {
return
}
let storedKey = `${chain}-${tvlType}`
let tvlFunctionIsFetch = false;
if (tvlType === "tvl") {
storedKey = chain
} else if (tvlType === "fetch") {
storedKey = chain
tvlFunctionIsFetch = true
}
await getTvl(unixTimestamp, ethBlock, chainBlocks, usdTvls, tokensBalances,
usdTokenBalances, tvlFunction, tvlFunctionIsFetch, storedKey, knownTokenPrices)
let keyToAddChainBalances = tvlType;
if (tvlType === "tvl" || tvlType === "fetch") {
keyToAddChainBalances = "tvl"
}
if (chainTvlsToAdd[keyToAddChainBalances] === undefined) {
chainTvlsToAdd[keyToAddChainBalances] = [storedKey]
} else {
chainTvlsToAdd[keyToAddChainBalances].push(storedKey)
}
}))
})
if (module.tvl || module.fetch) {
let mainTvlIsFetch;
if (module.tvl) {
mainTvlIsFetch = false
} else {
mainTvlIsFetch = true
}
const mainTvlPromise = getTvl(unixTimestamp, ethBlock, chainBlocks, usdTvls, tokensBalances,
usdTokenBalances, mainTvlIsFetch ? module.fetch : module.tvl, mainTvlIsFetch, 'tvl', knownTokenPrices)
tvlPromises = tvlPromises.concat([mainTvlPromise])
}
await Promise.all(tvlPromises)
Object.entries(chainTvlsToAdd).map(([tvlType, storedKeys]) => {
if (usdTvls[tvlType] === undefined) {
usdTvls[tvlType] = storedKeys.reduce((total, key) => total + usdTvls[key], 0)
mergeBalances(tvlType, storedKeys, tokensBalances)
mergeBalances(tvlType, storedKeys, usdTokenBalances)
}
})
if(usdTvls.tvl === undefined){
throw new Error("Protocol doesn't have total tvl, make sure to export a tvl key either on the main object or in one of the chains")
}
Object.entries(usdTokenBalances).forEach(([chain, balances]) => {
console.log(`--- ${chain} ---`)
Object.entries(balances).sort((a, b)=>b[1]-a[1]).forEach(([symbol, balance]) => {
console.log(symbol.padEnd(25, " "), humanizeNumber(balance))
})
console.log("Total:", humanizeNumber(usdTvls[chain]), "\n");
})
console.log(`------ TVL ------`)
Object.entries(usdTvls).forEach(([chain, usdTvl]) => {
if(chain !== "tvl"){
console.log(chain.padEnd(25, " "), humanizeNumber(usdTvl))
}
})
console.log("\ntotal".padEnd(25, " "), humanizeNumber(usdTvls.tvl), "\n")
process.exit(0);
})();