-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathutils.js
executable file
·512 lines (451 loc) · 14.1 KB
/
utils.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
const superagent = require('superagent');
const axios = require('axios');
const { request, gql } = require('graphql-request');
const { chunk } = require('lodash');
const sdk = require('@defillama/sdk');
const { default: BigNumber } = require('bignumber.js');
exports.formatChain = (chain) => {
if (chain && chain.toLowerCase() === 'xdai') return 'Gnosis';
if (chain && chain.toLowerCase() === 'kcc') return 'KCC';
if (chain && chain.toLowerCase() === 'okexchain') return 'OKExChain';
if (chain && chain.toLowerCase() === 'bsc') return 'Binance';
if (chain && chain.toLowerCase() === 'milkomeda') return 'Milkomeda C1';
if (chain && chain.toLowerCase() === 'milkomeda_a1') return 'Milkomeda A1';
if (chain && chain.toLowerCase() === 'boba_avax') return 'Boba_Avax';
if (chain && chain.toLowerCase() === 'boba_bnb') return 'Boba_Bnb';
if (chain && chain.toLowerCase() === 'iotaevm') return 'IOTA EVM';
if (
chain &&
(chain.toLowerCase() === 'zksync_era' ||
chain.toLowerCase() === 'zksync era' ||
chain.toLowerCase() === 'era')
)
return 'zkSync Era';
if (chain && chain.toLowerCase() === 'polygon_zkevm') return 'Polygon zkEVM';
if (chain && chain.toLowerCase() === 'real') return 're.al';
return chain.charAt(0).toUpperCase() + chain.slice(1);
};
const getFormatter = (symbol) => {
if (symbol.includes('USD+')) return /[_:\/]/g;
return /[_+:\/]/g;
};
// replace / with - and trim potential whitespace
// set mimatic to mai, uppercase all symbols
exports.formatSymbol = (symbol) => {
return symbol
.replace(getFormatter(symbol), '-')
.replace(/\s/g, '')
.trim()
.toLowerCase()
.replaceAll('mimatic', 'mai')
.toUpperCase();
};
exports.getData = async (url, query = null) => {
if (query !== null) {
res = await superagent.post(url).send(query);
} else {
res = await superagent.get(url);
}
res = res.body;
return res;
};
// retrive block based on unixTimestamp array
exports.getBlocksByTime = async (timestamps, chainString) => {
const chain = chainString === 'avalanche' ? 'avax' : chainString;
const blocks = [];
for (const timestamp of timestamps) {
const response = await superagent.get(
`https://coins.llama.fi/block/${chain}/${timestamp}`
);
blocks.push(response.body.height);
}
return blocks;
};
const getLatestBlockSubgraph = async (url) => {
const queryGraph = gql`
{
_meta {
block {
number
}
}
}
`;
const blockGraph =
url.includes('https://gateway-arbitrum.network.thegraph.com/api') ||
url.includes('metis-graph.maiadao.io') ||
url.includes('babydoge/faas') ||
url.includes('kybernetwork/kyberswap-elastic-cronos') ||
url.includes('kybernetwork/kyberswap-elastic-matic') ||
url.includes('metisapi.0xgraph.xyz/subgraphs/name') ||
url.includes(
'https://subgraph.satsuma-prod.com/09c9cf3574cc/orbital-apes/v3-subgraph/api'
) ||
url.includes('api.goldsky.com') ||
url.includes('api.studio.thegraph.com') ||
url.includes('48211/uniswap-v3-base') ||
url.includes('horizondex/block') ||
url.includes('pancake-swap.workers.dev') ||
url.includes('pancakeswap/exchange-v3-linea') ||
url.includes('exchange-v3-polygon-zkevm/version/latest') ||
url.includes('exchange-v3-zksync/version/latest') ||
url.includes('balancer-base-v2/version/latest') ||
url.includes('horizondex') ||
url.includes('swopfi-units')
? await request(url, queryGraph)
: url.includes('aperture/uniswap-v3')
? await request(
'https://api.goldsky.com/api/public/project_clnz7akg41cv72ntv0uhyd3ai/subgraphs/aperture/manta-pacific-blocks/gn',
queryGraph
)
: await request(
`https://api.thegraph.com/subgraphs/name/${url.split('name/')[1]}`,
queryGraph
);
// return Number(
// blockGraph.indexingStatusForCurrentVersion.chains[0].latestBlock.number
// );
return Number(blockGraph._meta.block.number);
};
// func which queries subgraphs for their latest block nb and compares it against
// the latest block from https://coins.llama.fi/block/, if within a certain bound -> ok, otherwise
// will break as data is stale
exports.getBlocks = async (
chainString,
tsTimeTravel,
urlArray,
offset = 86400
) => {
const timestamp =
tsTimeTravel !== null
? Number(tsTimeTravel)
: Math.floor(Date.now() / 1000);
const timestampPrior = timestamp - offset;
let [block, blockPrior] = await this.getBlocksByTime(
[timestamp, timestampPrior],
chainString
);
// in case of standard run, we ping the subgraph and check its latest block
// ideally its synced with the block from getBlocksByTime. if the delta is too large
// throwing an error
if (tsTimeTravel === null) {
const blocksPromises = [];
for (const url of urlArray.filter((el) => el !== null)) {
blocksPromises.push(getLatestBlockSubgraph(url));
}
blocks = await Promise.all(blocksPromises);
// we use oldest block
blockGraph = Math.min(...blocks);
// calc delta
blockDelta = Math.abs(block - blockGraph);
// check delta (keeping this large for now)
const thr =
chainString === 'ethereum' ? 300 : chainString === 'cronos' ? 6000 : 3000;
if (blockDelta > thr) {
console.log(`block: ${block}, blockGraph: ${blockGraph}`);
throw new Error(`Stale subgraph of ${blockDelta} blocks!`);
}
block = blockGraph;
}
return [block, blockPrior];
};
// calculate tvl in usd based on subgraph data.
// reserveUSD field from subgraphs can be unreliable, using defillama price api instead
exports.tvl = async (dataNow, networkString) => {
// changing the string for avax so it matches the defillama price api
networkString = networkString === 'avalanche' ? 'avax' : networkString;
// make copy
const dataNowCopy = dataNow.map((el) => ({ ...el }));
// extract unique token id's
const ids = [];
for (const e of dataNowCopy) {
ids.push([
`${networkString}:${e.token0.id}`,
`${networkString}:${e.token1.id}`,
]);
}
let idsSet = [...new Set(ids.flat())];
// price endpoint seems to break with too many tokens, splitting it to max 50 per request
const maxSize = 50;
const pages = Math.ceil(idsSet.length / maxSize);
let pricesA = [];
let x = '';
for (const p of [...Array(pages).keys()]) {
x = idsSet
.slice(p * maxSize, maxSize * (p + 1))
.join(',')
.replaceAll('/', '');
pricesA = [
...pricesA,
(await axios.get(`https://coins.llama.fi/prices/current/${x}`)).data
.coins,
];
}
let prices = {};
for (const p of pricesA.flat()) {
prices = { ...prices, ...p };
}
// calc tvl
const precision = 5;
for (const el of dataNowCopy) {
let price0 = prices[`${networkString}:${el.token0.id}`]?.price;
let price1 = prices[`${networkString}:${el.token1.id}`]?.price;
if (price0 !== undefined && price1 !== undefined) {
tvl = Number(el.reserve0) * price0 + Number(el.reserve1) * price1;
} else if (price0 !== undefined && price1 === undefined) {
tvl = Number(el.reserve0) * price0 * 2;
} else if (price0 === undefined && price1 !== undefined) {
tvl = Number(el.reserve1) * price1 * 2;
} else {
tvl = 0;
}
el['totalValueLockedUSD'] = tvl;
el['price0'] = price0;
el['price1'] = price1;
}
return dataNowCopy;
};
exports.aprToApy = (apr, compoundFrequency = 365) => {
return (
((1 + (apr * 0.01) / compoundFrequency) ** compoundFrequency - 1) * 100
);
};
exports.apyToApr = (apy, compoundFrequency = 365) => {
return (
(((apy / 100 + 1) ** (1 / compoundFrequency) - 1) * compoundFrequency) /
0.01
);
};
// calculating apy based on subgraph data
exports.apy = (pool, dataPrior1d, dataPrior7d, version) => {
pool = { ...pool };
// uni v2 forks set feeTier to constant
if (version === 'v2') {
pool['feeTier'] = 3000;
} else if (version === 'stellaswap') {
pool['feeTier'] = 2000;
} else if (version === 'baseswap') {
pool['feeTier'] = 1700;
} else if (version === 'zyberswap') {
pool['feeTier'] = 1500;
} else if (version === 'arbidex') {
pool['feeTier'] = 500;
}
// calc prior volume on 24h offset
pool['volumeUSDPrior1d'] = dataPrior1d.find(
(el) => el.id === pool.id
)?.volumeUSD;
pool['volumeUSDPrior7d'] = dataPrior7d.find(
(el) => el.id === pool.id
)?.volumeUSD;
// calc 24h volume
pool['volumeUSD1d'] = Number(pool.volumeUSD) - Number(pool.volumeUSDPrior1d);
pool['volumeUSD7d'] = Number(pool.volumeUSD) - Number(pool.volumeUSDPrior7d);
// calc fees
pool['feeUSD1d'] = (pool.volumeUSD1d * Number(pool.feeTier)) / 1e6;
pool['feeUSD7d'] = (pool.volumeUSD7d * Number(pool.feeTier)) / 1e6;
// annualise
pool['feeUSDyear1d'] = pool.feeUSD1d * 365;
pool['feeUSDyear7d'] = pool.feeUSD7d * 52;
// calc apy
pool['apy1d'] = (pool.feeUSDyear1d / pool.totalValueLockedUSD) * 100;
pool['apy7d'] = (pool.feeUSDyear7d / pool.totalValueLockedUSD) * 100;
return pool;
};
exports.keepFinite = (p) => {
if (
!['apyBase', 'apyReward', 'apy']
.map((f) => Number.isFinite(p[f]))
.includes(true)
)
return false;
return Number.isFinite(p['tvlUsd']);
};
exports.getPrices = async (addresses, chain) => {
const priceKeys = chain
? addresses.map((address) => `${chain}:${address}`)
: addresses;
const prices = (
await superagent.get(
`https://coins.llama.fi/prices/current/${priceKeys
.join(',')
.toLowerCase()}`
)
).body.coins;
const pricesByAddress = Object.entries(prices).reduce(
(acc, [address, price]) => ({
...acc,
[address.split(':')[1].toLowerCase()]: price.price,
}),
{}
);
const pricesBySymbol = Object.entries(prices).reduce(
(acc, [name, price]) => ({
...acc,
[price.symbol.toLowerCase()]: price.price,
}),
{}
);
return { pricesBySymbol, pricesByAddress };
};
///////// UNISWAP V2
const calculateApy = (
poolInfo,
totalAllocPoint,
rewardPerBlock,
rewardPrice,
reserveUSD,
blocksPerYear
) => {
const poolWeight = poolInfo.allocPoint / totalAllocPoint;
const tokensPerYear = blocksPerYear * rewardPerBlock;
return ((poolWeight * tokensPerYear * rewardPrice) / reserveUSD) * 100;
};
const calculateReservesUSD = (
reserves,
reservesRatio,
token0,
token1,
tokenPrices
) => {
const { decimals: token0Decimals, id: token0Address } = token0;
const { decimals: token1Decimals, id: token1Address } = token1;
const token0Price = tokenPrices[token0Address.toLowerCase()];
const token1Price = tokenPrices[token1Address.toLowerCase()];
const reserve0 = new BigNumber(reserves._reserve0)
.times(reservesRatio)
.times(10 ** (18 - token0Decimals));
const reserve1 = new BigNumber(reserves._reserve1)
.times(reservesRatio)
.times(10 ** (18 - token1Decimals));
if (token0Price) return reserve0.times(token0Price).times(2).div(1e18);
if (token1Price) return reserve1.times(token1Price).times(2).div(1e18);
};
const getPairsInfo = async (pairs, url) => {
const pairQuery = gql`
query pairQuery($id_in: [ID!]) {
pairs(where: { id_in: $id_in }) {
name
id
token0 {
decimals
id
}
token1 {
decimals
id
}
}
}
`;
const pairInfo = await Promise.all(
chunk(pairs, 7).map((tokens) =>
request(url, pairQuery, {
id_in: tokens.map((pair) => pair.toLowerCase()),
})
)
);
return pairInfo
.map(({ pairs }) => pairs)
.flat()
.reduce((acc, pair) => ({ ...acc, [pair.id.toLowerCase()]: pair }), {});
};
exports.uniswap = { calculateApy, calculateReservesUSD, getPairsInfo };
/// MULTICALL
const makeMulticall = async (abi, addresses, chain, params = null) => {
const data = await sdk.api.abi.multiCall({
abi,
calls: addresses.map((address) => ({
target: address,
params,
})),
chain,
permitFailure: true,
});
const res = data.output.map(({ output }) => output);
return res;
};
exports.makeMulticall = makeMulticall;
const capitalizeFirstLetter = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
};
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.removeDuplicates = (pools) => {
const seen = {};
return pools.filter((i) => {
return seen.hasOwnProperty(i.pool) ? false : (seen[i.pool] = true);
});
};
exports.getERC4626Info = async (
address,
chain,
timestamp = Math.floor(Date.now() / 1e3),
{
assetUnit = '100000000000000000',
totalAssetsAbi = 'uint:totalAssets',
convertToAssetsAbi = 'function convertToAssets(uint256 shares) external view returns (uint256)',
} = {}
) => {
const DAY = 24 * 3600;
const [blockNow, blockYesterday] = await Promise.all(
[timestamp, timestamp - DAY].map((time) =>
axios
.get(`https://coins.llama.fi/block/${chain}/${time}`)
.then((r) => r.data.height)
)
);
const [tvl, priceNow, priceYesterday] = await Promise.all([
sdk.api.abi.call({
target: address,
block: blockNow,
abi: totalAssetsAbi,
}),
sdk.api.abi.call({
target: address,
block: blockNow,
abi: convertToAssetsAbi,
params: [assetUnit],
}),
sdk.api.abi.call({
target: address,
block: blockYesterday,
abi: convertToAssetsAbi,
params: [assetUnit],
}),
]);
const apy = (priceNow.output / priceYesterday.output) ** 365 * 100 - 100;
return {
pool: address,
chain,
tvl: tvl.output,
apyBase: apy,
};
};
// solana
exports.getTotalSupply = async (tokenMintAddress) => {
const rpcUrl = 'https://api.mainnet-beta.solana.com';
const requestBody = {
jsonrpc: '2.0',
id: 1,
method: 'getTokenSupply',
params: [
tokenMintAddress,
{
commitment: 'confirmed',
},
],
};
const response = await axios.post(rpcUrl, requestBody, {
headers: {
'Content-Type': 'application/json',
},
});
const data = response.data;
if (data.error) {
throw new Error(`Error fetching total supply: ${data.error.message}`);
}
const totalSupply = data.result.value.amount;
const decimals = data.result.value.decimals;
const supplyInTokens = totalSupply / Math.pow(10, decimals);
return supplyInTokens;
};