@defillama/sdk 2.3.42 → 2.3.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/LICENSE +661 -661
  2. package/README.md +16 -16
  3. package/build/abi/cachedABIs.d.ts +4 -4
  4. package/build/abi/cachedABIs.js +70 -70
  5. package/build/abi/convertResults.d.ts +2 -2
  6. package/build/abi/convertResults.js +56 -56
  7. package/build/abi/index.d.ts +26 -26
  8. package/build/abi/index.js +185 -185
  9. package/build/abi/index.test.d.ts +1 -1
  10. package/build/abi/index.test.js +651 -651
  11. package/build/abi/multicall.d.ts +13 -13
  12. package/build/abi/multicall.js +210 -210
  13. package/build/api.d.ts +10 -10
  14. package/build/api.js +32 -32
  15. package/build/cdp/compoundTokens.d.ts +68 -68
  16. package/build/cdp/compoundTokens.js +69 -69
  17. package/build/cdp/index.d.ts +29 -29
  18. package/build/cdp/index.js +124 -124
  19. package/build/cdp/index.test.d.ts +1 -1
  20. package/build/cdp/index.test.js +82 -82
  21. package/build/cdp/makerTokens.d.ts +10 -10
  22. package/build/cdp/makerTokens.js +244 -244
  23. package/build/cli.d.ts +2 -2
  24. package/build/cli.js +95 -95
  25. package/build/computeTVL/blocks.d.ts +18 -18
  26. package/build/computeTVL/blocks.js +138 -138
  27. package/build/computeTVL/blocks.test.d.ts +1 -1
  28. package/build/computeTVL/blocks.test.js +56 -56
  29. package/build/computeTVL/humanizeNumber.d.ts +1 -1
  30. package/build/computeTVL/humanizeNumber.js +18 -18
  31. package/build/computeTVL/index.d.ts +55 -55
  32. package/build/computeTVL/index.js +366 -366
  33. package/build/computeTVL/index.test.d.ts +1 -1
  34. package/build/computeTVL/index.test.js +107 -107
  35. package/build/computeTVL/prices.d.ts +9 -9
  36. package/build/computeTVL/prices.js +163 -163
  37. package/build/erc20/index.d.ts +31 -31
  38. package/build/erc20/index.js +144 -144
  39. package/build/erc20/index.test.d.ts +1 -1
  40. package/build/erc20/index.test.js +124 -124
  41. package/build/eth/index.d.ts +21 -21
  42. package/build/eth/index.js +90 -90
  43. package/build/eth/index.test.d.ts +1 -1
  44. package/build/eth/index.test.js +122 -122
  45. package/build/general.d.ts +10 -10
  46. package/build/general.js +102 -101
  47. package/build/general.test.d.ts +1 -1
  48. package/build/general.test.js +73 -73
  49. package/build/generalUtil.d.ts +19 -19
  50. package/build/generalUtil.js +117 -117
  51. package/build/generalUtil.test.d.ts +1 -1
  52. package/build/generalUtil.test.js +59 -59
  53. package/build/index.d.ts +2 -2
  54. package/build/index.js +24 -24
  55. package/build/index.test.js +69 -69
  56. package/build/types.d.ts +5 -5
  57. package/build/types.js +2 -2
  58. package/build/util/index.d.ts +57 -57
  59. package/build/util/index.js +354 -354
  60. package/build/util/index.test.d.ts +1 -1
  61. package/build/util/index.test.js +314 -314
  62. package/build/util/tokenList.d.ts +14 -14
  63. package/build/util/tokenList.js +3631 -3631
  64. package/package.json +41 -41
@@ -1,366 +1,366 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.chainToCoingeckoId = void 0;
43
- var abi_1 = require("../abi");
44
- var humanizeNumber_1 = require("./humanizeNumber");
45
- var prices_1 = require("./prices");
46
- var node_fetch_1 = __importDefault(require("node-fetch"));
47
- var generalUtil_1 = require("../generalUtil");
48
- function tokenMulticall(addresses, abi, chain) {
49
- return abi_1.multiCall({
50
- abi: abi,
51
- calls: addresses.map(function (address) { return ({
52
- target: address,
53
- params: [],
54
- }); }),
55
- chain: chain,
56
- });
57
- }
58
- function addTokenBalance(balances, symbol, amount) {
59
- balances[symbol] = (balances[symbol] || 0) + amount;
60
- }
61
- function historicalCoingeckoUrls(chain) {
62
- if (chain === 'coingecko') {
63
- return "https://api.coingecko.com/api/v3/coins";
64
- }
65
- var platformId = exports.chainToCoingeckoId[chain];
66
- if (platformId !== undefined) {
67
- return "https://api.coingecko.com/api/v3/coins/" + platformId + "/contract";
68
- }
69
- throw new Error("Chain not supported");
70
- }
71
- exports.chainToCoingeckoId = {
72
- bsc: "binance-smart-chain",
73
- ethereum: "ethereum",
74
- polygon: "polygon-pos",
75
- avax: "avalanche",
76
- fantom: "fantom",
77
- xdai: "xdai",
78
- heco: "huobi-token",
79
- okexchain: "okex-chain",
80
- harmony: "harmony-shard-0",
81
- kcc: "kucoin-community-chain",
82
- celo: "celo",
83
- arbitrum: "arbitrum-one",
84
- iotex: "iotex",
85
- moonriver: "moonriver",
86
- solana: "solana",
87
- terra: "terra",
88
- tron: "tron",
89
- waves: "waves",
90
- klaytn: "klay-token",
91
- osmosis: "osmosis",
92
- kava: "kava",
93
- icon: "icon",
94
- optimism: "optimistic-ethereum",
95
- eos: "eos",
96
- secret: "secret",
97
- rsk: "rootstock",
98
- neo: "neo",
99
- tezos: "tezos",
100
- wan: "wanchain",
101
- ontology: "ontology",
102
- algorand: "algorand",
103
- zilliqa: "zilliqa",
104
- kardia: "kardiachain",
105
- cronos: "cronos",
106
- aurora: "aurora",
107
- boba: "boba",
108
- metis: "metis-andromeda",
109
- telos: "telos",
110
- moonbeam: "moonbeam",
111
- };
112
- var chains = Object.keys(exports.chainToCoingeckoId);
113
- function getHistoricalChainPrices(ids, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
114
- return __awaiter(this, void 0, void 0, function () {
115
- var chainPrices, _i, _a, chain, _b, _c;
116
- return __generator(this, function (_d) {
117
- switch (_d.label) {
118
- case 0:
119
- chainPrices = {};
120
- _i = 0, _a = chains.concat(["coingecko"]);
121
- _d.label = 1;
122
- case 1:
123
- if (!(_i < _a.length)) return [3 /*break*/, 5];
124
- chain = _a[_i];
125
- if (!(ids[chain].length === 0)) return [3 /*break*/, 2];
126
- chainPrices[chain] = {};
127
- return [3 /*break*/, 4];
128
- case 2:
129
- _b = chainPrices;
130
- _c = chain;
131
- return [4 /*yield*/, prices_1.getHistoricalTokenPrices(ids[chain], historicalCoingeckoUrls(chain), timestamp, getCoingeckoLock, coingeckoMaxRetries)];
132
- case 3:
133
- _b[_c] = _d.sent();
134
- _d.label = 4;
135
- case 4:
136
- _i++;
137
- return [3 /*break*/, 1];
138
- case 5: return [2 /*return*/, chainPrices];
139
- }
140
- });
141
- });
142
- }
143
- function getChainSymbolsAndDecimals(ids, maxRetries) {
144
- return __awaiter(this, void 0, void 0, function () {
145
- var allCoins, i, response;
146
- return __generator(this, function (_a) {
147
- switch (_a.label) {
148
- case 0:
149
- allCoins = Object.entries(ids).map(function (chain) {
150
- return chain[1].map(function (coin) { return chain[0] === "coingecko" ? coin.toLowerCase() : chain[0] + ":" + coin.toLowerCase(); });
151
- })
152
- .reduce(function (acc, coins) {
153
- coins.forEach(function (coin) {
154
- acc.add(coin);
155
- });
156
- return acc;
157
- }, new Set([]));
158
- i = 0;
159
- _a.label = 1;
160
- case 1:
161
- if (!(i < maxRetries)) return [3 /*break*/, 4];
162
- return [4 /*yield*/, node_fetch_1.default('https://api.llama.fi/coins', {
163
- method: 'POST',
164
- body: JSON.stringify({
165
- coins: Array.from(allCoins)
166
- })
167
- }).then(function (response) { return response.json(); })];
168
- case 2:
169
- response = _a.sent();
170
- if (Array.isArray(response)) {
171
- return [2 /*return*/, response];
172
- }
173
- _a.label = 3;
174
- case 3:
175
- i++;
176
- return [3 /*break*/, 1];
177
- case 4: throw new Error("api.llama.fi/coins failed");
178
- }
179
- });
180
- });
181
- }
182
- function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
183
- if (verbose === void 0) { verbose = false; }
184
- if (knownTokenPrices === void 0) { knownTokenPrices = {}; }
185
- if (getCoingeckoLock === void 0) { getCoingeckoLock = function () { return Promise.resolve(); }; }
186
- if (coingeckoMaxRetries === void 0) { coingeckoMaxRetries = 3; }
187
- return __awaiter(this, void 0, void 0, function () {
188
- var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, usdAmounts, usdTvl;
189
- var _this = this;
190
- return __generator(this, function (_c) {
191
- switch (_c.label) {
192
- case 0:
193
- if (!(rawBalances instanceof Array)) return [3 /*break*/, 2];
194
- return [4 /*yield*/, abi_1.multiCall({
195
- abi: "erc20:decimals",
196
- calls: rawBalances.map(function (token) { return ({
197
- target: token.address,
198
- params: [],
199
- }); }),
200
- })];
201
- case 1:
202
- callTokenDecimals_1 = (_c.sent()).output;
203
- balances = rawBalances.reduce(function (acc, token, index) {
204
- var dec;
205
- if (callTokenDecimals_1[index].success) {
206
- dec = Number(callTokenDecimals_1[index].output);
207
- }
208
- else {
209
- if (token.address === "0x0000000000000000000000000000000000000000") {
210
- dec = 18;
211
- }
212
- else {
213
- dec = NaN;
214
- }
215
- }
216
- acc[token.address] = (Number(token.balance) * Math.pow(10, dec)).toString();
217
- return acc;
218
- }, {});
219
- return [3 /*break*/, 3];
220
- case 2:
221
- balances = rawBalances;
222
- _c.label = 3;
223
- case 3:
224
- chainIds = {
225
- coingecko: [],
226
- };
227
- for (_i = 0, chains_1 = chains; _i < chains_1.length; _i++) {
228
- chain = chains_1[_i];
229
- chainIds[chain] = [];
230
- }
231
- normalizedBalances = {};
232
- for (_a = 0, _b = Object.keys(balances); _a < _b.length; _a++) {
233
- tokenAddressOrName = _b[_a];
234
- normalizedAddressOrName = tokenAddressOrName;
235
- normalizedBalance = balances[tokenAddressOrName];
236
- if (tokenAddressOrName === "0x0000000000000000000000000000000000000000") {
237
- normalizedAddressOrName = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; // Normalize ETH to WETH
238
- }
239
- if (typeof normalizedBalance === "object") {
240
- normalizedBalances[normalizedAddressOrName] = normalizedBalance.toFixed(); // Some adapters return a BigNumber from bignumber.js so the results must be normalized
241
- }
242
- else {
243
- generalUtil_1.sumSingleBalance(normalizedBalances, normalizedAddressOrName, normalizedBalance);
244
- }
245
- if (normalizedAddressOrName.startsWith("0x")) {
246
- chainIds.ethereum.push(normalizedAddressOrName);
247
- }
248
- else if (normalizedAddressOrName.includes(":")) {
249
- chain = normalizedAddressOrName.split(':')[0];
250
- chainIds[chain].push(normalizedAddressOrName.slice(chain.length + 1));
251
- }
252
- else {
253
- chainIds.coingecko.push(normalizedAddressOrName);
254
- }
255
- }
256
- return [4 /*yield*/, getChainSymbolsAndDecimals(chainIds, coingeckoMaxRetries)];
257
- case 4:
258
- symbolsAndDecimals = _c.sent();
259
- if (!(timestamp === "now")) return [3 /*break*/, 5];
260
- allChainTokenPrices = symbolsAndDecimals.reduce(function (prices, item) {
261
- var chain, address;
262
- if (item.coin.includes(":")) {
263
- chain = item.coin.split(':')[0];
264
- address = item.coin.split(':')[1];
265
- }
266
- else {
267
- chain = "coingecko";
268
- address = item.coin;
269
- }
270
- if (prices[chain] === undefined) {
271
- prices[chain] = {};
272
- }
273
- prices[chain][address] = { usd: item.price };
274
- return prices;
275
- }, {});
276
- return [3 /*break*/, 7];
277
- case 5: return [4 /*yield*/, getHistoricalChainPrices(chainIds, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries)];
278
- case 6:
279
- allChainTokenPrices = _c.sent();
280
- _c.label = 7;
281
- case 7:
282
- usdTokenBalances = {};
283
- tokenBalances = {};
284
- usdAmounts = Object.entries(normalizedBalances).map(function (_a) {
285
- var address = _a[0], balance = _a[1];
286
- return __awaiter(_this, void 0, void 0, function () {
287
- var amount, price, tokenSymbol, normalizedAddress_1, chainSelector_1, chainTokenPrices, chainAddress_1, coinData, tokenDecimals, usdAmount;
288
- var _b, _c, _d, _e;
289
- return __generator(this, function (_f) {
290
- try {
291
- if (address.startsWith("0x") || address.includes(":")) {
292
- normalizedAddress_1 = address;
293
- chainSelector_1 = "ethereum";
294
- chains.forEach(function (chain) {
295
- if (address.startsWith(chain)) {
296
- chainSelector_1 = chain;
297
- normalizedAddress_1 = address.slice(chain.length + 1);
298
- }
299
- });
300
- chainTokenPrices = (_b = allChainTokenPrices[chainSelector_1]) !== null && _b !== void 0 ? _b : {};
301
- chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1.toLowerCase();
302
- coinData = symbolsAndDecimals.find(function (coin) { return coin.coin === chainAddress_1; });
303
- tokenSymbol = (_c = coinData === null || coinData === void 0 ? void 0 : coinData.symbol) === null || _c === void 0 ? void 0 : _c.toUpperCase();
304
- if (tokenSymbol === undefined || tokenSymbol === null) {
305
- tokenSymbol = "UNKNOWN (" + address + ")";
306
- }
307
- tokenDecimals = coinData === null || coinData === void 0 ? void 0 : coinData.decimals;
308
- if (tokenDecimals === undefined) {
309
- amount = 0;
310
- }
311
- else {
312
- amount = Number(balance) / Math.pow(10, Number(tokenDecimals));
313
- }
314
- price = (_d = chainTokenPrices[normalizedAddress_1.toLowerCase()]) === null || _d === void 0 ? void 0 : _d.usd;
315
- }
316
- else {
317
- tokenSymbol = address;
318
- price = (_e = allChainTokenPrices["coingecko"][address.toLowerCase()]) === null || _e === void 0 ? void 0 : _e.usd;
319
- amount = Number(balance);
320
- }
321
- if (price === undefined) {
322
- if (verbose) {
323
- console.log("Token " + address + " is not on coingecko, it'll be ignored");
324
- }
325
- price = 0;
326
- }
327
- addTokenBalance(tokenBalances, tokenSymbol, amount);
328
- usdAmount = amount * price;
329
- addTokenBalance(usdTokenBalances, tokenSymbol, usdAmount);
330
- return [2 /*return*/, { usdAmount: usdAmount, tokenSymbol: tokenSymbol }];
331
- }
332
- catch (e) {
333
- console.error("Error on token " + address + ", we'll just assume it's price is 0...", e);
334
- return [2 /*return*/, {
335
- usdAmount: 0,
336
- tokenSymbol: "ERROR " + address,
337
- }];
338
- }
339
- return [2 /*return*/];
340
- });
341
- });
342
- });
343
- if (!verbose) return [3 /*break*/, 9];
344
- return [4 /*yield*/, Promise.all(usdAmounts)];
345
- case 8:
346
- (_c.sent())
347
- .sort(function (a, b) { return b.usdAmount - a.usdAmount; })
348
- .map(function (token) {
349
- console.log(token.tokenSymbol.padEnd(25, " "), humanizeNumber_1.humanizeNumber(token.usdAmount));
350
- });
351
- _c.label = 9;
352
- case 9: return [4 /*yield*/, Promise.all(usdAmounts)];
353
- case 10:
354
- usdTvl = (_c.sent()).reduce(function (sum, token) {
355
- return sum + token.usdAmount;
356
- }, 0);
357
- return [2 /*return*/, {
358
- usdTvl: usdTvl,
359
- usdTokenBalances: usdTokenBalances,
360
- tokenBalances: tokenBalances,
361
- }];
362
- }
363
- });
364
- });
365
- }
366
- exports.default = default_1;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.chainToCoingeckoId = void 0;
43
+ var abi_1 = require("../abi");
44
+ var humanizeNumber_1 = require("./humanizeNumber");
45
+ var prices_1 = require("./prices");
46
+ var node_fetch_1 = __importDefault(require("node-fetch"));
47
+ var generalUtil_1 = require("../generalUtil");
48
+ function tokenMulticall(addresses, abi, chain) {
49
+ return abi_1.multiCall({
50
+ abi: abi,
51
+ calls: addresses.map(function (address) { return ({
52
+ target: address,
53
+ params: [],
54
+ }); }),
55
+ chain: chain,
56
+ });
57
+ }
58
+ function addTokenBalance(balances, symbol, amount) {
59
+ balances[symbol] = (balances[symbol] || 0) + amount;
60
+ }
61
+ function historicalCoingeckoUrls(chain) {
62
+ if (chain === 'coingecko') {
63
+ return "https://api.coingecko.com/api/v3/coins";
64
+ }
65
+ var platformId = exports.chainToCoingeckoId[chain];
66
+ if (platformId !== undefined) {
67
+ return "https://api.coingecko.com/api/v3/coins/" + platformId + "/contract";
68
+ }
69
+ throw new Error("Chain not supported");
70
+ }
71
+ exports.chainToCoingeckoId = {
72
+ bsc: "binance-smart-chain",
73
+ ethereum: "ethereum",
74
+ polygon: "polygon-pos",
75
+ avax: "avalanche",
76
+ fantom: "fantom",
77
+ xdai: "xdai",
78
+ heco: "huobi-token",
79
+ okexchain: "okex-chain",
80
+ harmony: "harmony-shard-0",
81
+ kcc: "kucoin-community-chain",
82
+ celo: "celo",
83
+ arbitrum: "arbitrum-one",
84
+ iotex: "iotex",
85
+ moonriver: "moonriver",
86
+ solana: "solana",
87
+ terra: "terra",
88
+ tron: "tron",
89
+ waves: "waves",
90
+ klaytn: "klay-token",
91
+ osmosis: "osmosis",
92
+ kava: "kava",
93
+ icon: "icon",
94
+ optimism: "optimistic-ethereum",
95
+ eos: "eos",
96
+ secret: "secret",
97
+ rsk: "rootstock",
98
+ neo: "neo",
99
+ tezos: "tezos",
100
+ wan: "wanchain",
101
+ ontology: "ontology",
102
+ algorand: "algorand",
103
+ zilliqa: "zilliqa",
104
+ kardia: "kardiachain",
105
+ cronos: "cronos",
106
+ aurora: "aurora",
107
+ boba: "boba",
108
+ metis: "metis-andromeda",
109
+ telos: "telos",
110
+ moonbeam: "moonbeam",
111
+ };
112
+ var chains = Object.keys(exports.chainToCoingeckoId);
113
+ function getHistoricalChainPrices(ids, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
114
+ return __awaiter(this, void 0, void 0, function () {
115
+ var chainPrices, _i, _a, chain, _b, _c;
116
+ return __generator(this, function (_d) {
117
+ switch (_d.label) {
118
+ case 0:
119
+ chainPrices = {};
120
+ _i = 0, _a = chains.concat(["coingecko"]);
121
+ _d.label = 1;
122
+ case 1:
123
+ if (!(_i < _a.length)) return [3 /*break*/, 5];
124
+ chain = _a[_i];
125
+ if (!(ids[chain].length === 0)) return [3 /*break*/, 2];
126
+ chainPrices[chain] = {};
127
+ return [3 /*break*/, 4];
128
+ case 2:
129
+ _b = chainPrices;
130
+ _c = chain;
131
+ return [4 /*yield*/, prices_1.getHistoricalTokenPrices(ids[chain], historicalCoingeckoUrls(chain), timestamp, getCoingeckoLock, coingeckoMaxRetries)];
132
+ case 3:
133
+ _b[_c] = _d.sent();
134
+ _d.label = 4;
135
+ case 4:
136
+ _i++;
137
+ return [3 /*break*/, 1];
138
+ case 5: return [2 /*return*/, chainPrices];
139
+ }
140
+ });
141
+ });
142
+ }
143
+ function getChainSymbolsAndDecimals(ids, maxRetries) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ var allCoins, i, response;
146
+ return __generator(this, function (_a) {
147
+ switch (_a.label) {
148
+ case 0:
149
+ allCoins = Object.entries(ids).map(function (chain) {
150
+ return chain[1].map(function (coin) { return chain[0] === "coingecko" ? coin.toLowerCase() : chain[0] + ":" + coin.toLowerCase(); });
151
+ })
152
+ .reduce(function (acc, coins) {
153
+ coins.forEach(function (coin) {
154
+ acc.add(coin);
155
+ });
156
+ return acc;
157
+ }, new Set([]));
158
+ i = 0;
159
+ _a.label = 1;
160
+ case 1:
161
+ if (!(i < maxRetries)) return [3 /*break*/, 4];
162
+ return [4 /*yield*/, node_fetch_1.default('https://api.llama.fi/coins', {
163
+ method: 'POST',
164
+ body: JSON.stringify({
165
+ coins: Array.from(allCoins)
166
+ })
167
+ }).then(function (response) { return response.json(); })];
168
+ case 2:
169
+ response = _a.sent();
170
+ if (Array.isArray(response)) {
171
+ return [2 /*return*/, response];
172
+ }
173
+ _a.label = 3;
174
+ case 3:
175
+ i++;
176
+ return [3 /*break*/, 1];
177
+ case 4: throw new Error("api.llama.fi/coins failed");
178
+ }
179
+ });
180
+ });
181
+ }
182
+ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
183
+ if (verbose === void 0) { verbose = false; }
184
+ if (knownTokenPrices === void 0) { knownTokenPrices = {}; }
185
+ if (getCoingeckoLock === void 0) { getCoingeckoLock = function () { return Promise.resolve(); }; }
186
+ if (coingeckoMaxRetries === void 0) { coingeckoMaxRetries = 3; }
187
+ return __awaiter(this, void 0, void 0, function () {
188
+ var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, usdAmounts, usdTvl;
189
+ var _this = this;
190
+ return __generator(this, function (_c) {
191
+ switch (_c.label) {
192
+ case 0:
193
+ if (!(rawBalances instanceof Array)) return [3 /*break*/, 2];
194
+ return [4 /*yield*/, abi_1.multiCall({
195
+ abi: "erc20:decimals",
196
+ calls: rawBalances.map(function (token) { return ({
197
+ target: token.address,
198
+ params: [],
199
+ }); }),
200
+ })];
201
+ case 1:
202
+ callTokenDecimals_1 = (_c.sent()).output;
203
+ balances = rawBalances.reduce(function (acc, token, index) {
204
+ var dec;
205
+ if (callTokenDecimals_1[index].success) {
206
+ dec = Number(callTokenDecimals_1[index].output);
207
+ }
208
+ else {
209
+ if (token.address === "0x0000000000000000000000000000000000000000") {
210
+ dec = 18;
211
+ }
212
+ else {
213
+ dec = NaN;
214
+ }
215
+ }
216
+ acc[token.address] = (Number(token.balance) * Math.pow(10, dec)).toString();
217
+ return acc;
218
+ }, {});
219
+ return [3 /*break*/, 3];
220
+ case 2:
221
+ balances = rawBalances;
222
+ _c.label = 3;
223
+ case 3:
224
+ chainIds = {
225
+ coingecko: [],
226
+ };
227
+ for (_i = 0, chains_1 = chains; _i < chains_1.length; _i++) {
228
+ chain = chains_1[_i];
229
+ chainIds[chain] = [];
230
+ }
231
+ normalizedBalances = {};
232
+ for (_a = 0, _b = Object.keys(balances); _a < _b.length; _a++) {
233
+ tokenAddressOrName = _b[_a];
234
+ normalizedAddressOrName = tokenAddressOrName;
235
+ normalizedBalance = balances[tokenAddressOrName];
236
+ if (tokenAddressOrName === "0x0000000000000000000000000000000000000000") {
237
+ normalizedAddressOrName = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; // Normalize ETH to WETH
238
+ }
239
+ if (typeof normalizedBalance === "object") {
240
+ normalizedBalances[normalizedAddressOrName] = normalizedBalance.toFixed(); // Some adapters return a BigNumber from bignumber.js so the results must be normalized
241
+ }
242
+ else {
243
+ generalUtil_1.sumSingleBalance(normalizedBalances, normalizedAddressOrName, normalizedBalance);
244
+ }
245
+ if (normalizedAddressOrName.startsWith("0x")) {
246
+ chainIds.ethereum.push(normalizedAddressOrName);
247
+ }
248
+ else if (normalizedAddressOrName.includes(":")) {
249
+ chain = normalizedAddressOrName.split(':')[0];
250
+ chainIds[chain].push(normalizedAddressOrName.slice(chain.length + 1));
251
+ }
252
+ else {
253
+ chainIds.coingecko.push(normalizedAddressOrName);
254
+ }
255
+ }
256
+ return [4 /*yield*/, getChainSymbolsAndDecimals(chainIds, coingeckoMaxRetries)];
257
+ case 4:
258
+ symbolsAndDecimals = _c.sent();
259
+ if (!(timestamp === "now")) return [3 /*break*/, 5];
260
+ allChainTokenPrices = symbolsAndDecimals.reduce(function (prices, item) {
261
+ var chain, address;
262
+ if (item.coin.includes(":")) {
263
+ chain = item.coin.split(':')[0];
264
+ address = item.coin.split(':')[1];
265
+ }
266
+ else {
267
+ chain = "coingecko";
268
+ address = item.coin;
269
+ }
270
+ if (prices[chain] === undefined) {
271
+ prices[chain] = {};
272
+ }
273
+ prices[chain][address] = { usd: item.price };
274
+ return prices;
275
+ }, {});
276
+ return [3 /*break*/, 7];
277
+ case 5: return [4 /*yield*/, getHistoricalChainPrices(chainIds, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries)];
278
+ case 6:
279
+ allChainTokenPrices = _c.sent();
280
+ _c.label = 7;
281
+ case 7:
282
+ usdTokenBalances = {};
283
+ tokenBalances = {};
284
+ usdAmounts = Object.entries(normalizedBalances).map(function (_a) {
285
+ var address = _a[0], balance = _a[1];
286
+ return __awaiter(_this, void 0, void 0, function () {
287
+ var amount, price, tokenSymbol, normalizedAddress_1, chainSelector_1, chainTokenPrices, chainAddress_1, coinData, tokenDecimals, usdAmount;
288
+ var _b, _c, _d, _e;
289
+ return __generator(this, function (_f) {
290
+ try {
291
+ if (address.startsWith("0x") || address.includes(":")) {
292
+ normalizedAddress_1 = address;
293
+ chainSelector_1 = "ethereum";
294
+ chains.forEach(function (chain) {
295
+ if (address.startsWith(chain)) {
296
+ chainSelector_1 = chain;
297
+ normalizedAddress_1 = address.slice(chain.length + 1);
298
+ }
299
+ });
300
+ chainTokenPrices = (_b = allChainTokenPrices[chainSelector_1]) !== null && _b !== void 0 ? _b : {};
301
+ chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1.toLowerCase();
302
+ coinData = symbolsAndDecimals.find(function (coin) { return coin.coin === chainAddress_1; });
303
+ tokenSymbol = (_c = coinData === null || coinData === void 0 ? void 0 : coinData.symbol) === null || _c === void 0 ? void 0 : _c.toUpperCase();
304
+ if (tokenSymbol === undefined || tokenSymbol === null) {
305
+ tokenSymbol = "UNKNOWN (" + address + ")";
306
+ }
307
+ tokenDecimals = coinData === null || coinData === void 0 ? void 0 : coinData.decimals;
308
+ if (tokenDecimals === undefined) {
309
+ amount = 0;
310
+ }
311
+ else {
312
+ amount = Number(balance) / Math.pow(10, Number(tokenDecimals));
313
+ }
314
+ price = (_d = chainTokenPrices[normalizedAddress_1.toLowerCase()]) === null || _d === void 0 ? void 0 : _d.usd;
315
+ }
316
+ else {
317
+ tokenSymbol = address;
318
+ price = (_e = allChainTokenPrices["coingecko"][address.toLowerCase()]) === null || _e === void 0 ? void 0 : _e.usd;
319
+ amount = Number(balance);
320
+ }
321
+ if (price === undefined) {
322
+ if (verbose) {
323
+ console.log("Token " + address + " is not on coingecko, it'll be ignored");
324
+ }
325
+ price = 0;
326
+ }
327
+ addTokenBalance(tokenBalances, tokenSymbol, amount);
328
+ usdAmount = amount * price;
329
+ addTokenBalance(usdTokenBalances, tokenSymbol, usdAmount);
330
+ return [2 /*return*/, { usdAmount: usdAmount, tokenSymbol: tokenSymbol }];
331
+ }
332
+ catch (e) {
333
+ console.error("Error on token " + address + ", we'll just assume it's price is 0...", e);
334
+ return [2 /*return*/, {
335
+ usdAmount: 0,
336
+ tokenSymbol: "ERROR " + address,
337
+ }];
338
+ }
339
+ return [2 /*return*/];
340
+ });
341
+ });
342
+ });
343
+ if (!verbose) return [3 /*break*/, 9];
344
+ return [4 /*yield*/, Promise.all(usdAmounts)];
345
+ case 8:
346
+ (_c.sent())
347
+ .sort(function (a, b) { return b.usdAmount - a.usdAmount; })
348
+ .map(function (token) {
349
+ console.log(token.tokenSymbol.padEnd(25, " "), humanizeNumber_1.humanizeNumber(token.usdAmount));
350
+ });
351
+ _c.label = 9;
352
+ case 9: return [4 /*yield*/, Promise.all(usdAmounts)];
353
+ case 10:
354
+ usdTvl = (_c.sent()).reduce(function (sum, token) {
355
+ return sum + token.usdAmount;
356
+ }, 0);
357
+ return [2 /*return*/, {
358
+ usdTvl: usdTvl,
359
+ usdTokenBalances: usdTokenBalances,
360
+ tokenBalances: tokenBalances,
361
+ }];
362
+ }
363
+ });
364
+ });
365
+ }
366
+ exports.default = default_1;