@defillama/sdk 2.3.31 → 2.3.32

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 +25 -25
  8. package/build/abi/index.js +166 -166
  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 +15 -15
  26. package/build/computeTVL/blocks.js +136 -136
  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 +50 -50
  32. package/build/computeTVL/index.js +371 -371
  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 +7 -7
  46. package/build/general.js +94 -93
  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 +351 -351
  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,371 +1,371 @@
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
- };
107
- function currentCoingeckoUrls(chain) {
108
- if (chain === 'coingecko') {
109
- return "v3/simple/price?ids";
110
- }
111
- var platformId = exports.chainToCoingeckoId[chain];
112
- if (platformId !== undefined) {
113
- return "v3/simple/token_price/" + platformId + "?contract_addresses";
114
- }
115
- throw new Error("Chain not supported");
116
- }
117
- var chains = Object.keys(exports.chainToCoingeckoId);
118
- function getHistoricalChainPrices(ids, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
119
- return __awaiter(this, void 0, void 0, function () {
120
- var chainPrices, _i, _a, chain, _b, _c;
121
- return __generator(this, function (_d) {
122
- switch (_d.label) {
123
- case 0:
124
- chainPrices = {};
125
- _i = 0, _a = chains.concat(["coingecko"]);
126
- _d.label = 1;
127
- case 1:
128
- if (!(_i < _a.length)) return [3 /*break*/, 5];
129
- chain = _a[_i];
130
- if (!(ids[chain].length === 0)) return [3 /*break*/, 2];
131
- chainPrices[chain] = {};
132
- return [3 /*break*/, 4];
133
- case 2:
134
- _b = chainPrices;
135
- _c = chain;
136
- return [4 /*yield*/, prices_1.getHistoricalTokenPrices(ids[chain], historicalCoingeckoUrls(chain), timestamp, getCoingeckoLock, coingeckoMaxRetries)];
137
- case 3:
138
- _b[_c] = _d.sent();
139
- _d.label = 4;
140
- case 4:
141
- _i++;
142
- return [3 /*break*/, 1];
143
- case 5: return [2 /*return*/, chainPrices];
144
- }
145
- });
146
- });
147
- }
148
- function getChainSymbolsAndDecimals(ids, maxRetries) {
149
- return __awaiter(this, void 0, void 0, function () {
150
- var allCoins, i, response;
151
- return __generator(this, function (_a) {
152
- switch (_a.label) {
153
- case 0:
154
- allCoins = Object.entries(ids).map(function (chain) {
155
- return chain[1].map(function (coin) { return chain[0] === "coingecko" ? coin.toLowerCase() : chain[0] + ":" + coin.toLowerCase(); });
156
- })
157
- .reduce(function (acc, coins) {
158
- coins.forEach(function (coin) {
159
- acc.add(coin);
160
- });
161
- return acc;
162
- }, new Set([]));
163
- i = 0;
164
- _a.label = 1;
165
- case 1:
166
- if (!(i < maxRetries)) return [3 /*break*/, 4];
167
- return [4 /*yield*/, node_fetch_1.default('https://api.llama.fi/coins', {
168
- method: 'POST',
169
- body: JSON.stringify({
170
- coins: Array.from(allCoins)
171
- })
172
- }).then(function (response) { return response.json(); })];
173
- case 2:
174
- response = _a.sent();
175
- if (Array.isArray(response)) {
176
- return [2 /*return*/, response];
177
- }
178
- _a.label = 3;
179
- case 3:
180
- i++;
181
- return [3 /*break*/, 1];
182
- case 4: throw new Error("api.llama.fi/coins failed");
183
- }
184
- });
185
- });
186
- }
187
- function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
188
- if (verbose === void 0) { verbose = false; }
189
- if (knownTokenPrices === void 0) { knownTokenPrices = {}; }
190
- if (getCoingeckoLock === void 0) { getCoingeckoLock = function () { return Promise.resolve(); }; }
191
- if (coingeckoMaxRetries === void 0) { coingeckoMaxRetries = 3; }
192
- return __awaiter(this, void 0, void 0, function () {
193
- var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, usdAmounts, usdTvl;
194
- var _this = this;
195
- return __generator(this, function (_c) {
196
- switch (_c.label) {
197
- case 0:
198
- if (!(rawBalances instanceof Array)) return [3 /*break*/, 2];
199
- return [4 /*yield*/, abi_1.multiCall({
200
- abi: "erc20:decimals",
201
- calls: rawBalances.map(function (token) { return ({
202
- target: token.address,
203
- params: [],
204
- }); }),
205
- })];
206
- case 1:
207
- callTokenDecimals_1 = (_c.sent()).output;
208
- balances = rawBalances.reduce(function (acc, token, index) {
209
- var dec;
210
- if (callTokenDecimals_1[index].success) {
211
- dec = Number(callTokenDecimals_1[index].output);
212
- }
213
- else {
214
- if (token.address === "0x0000000000000000000000000000000000000000") {
215
- dec = 18;
216
- }
217
- else {
218
- dec = NaN;
219
- }
220
- }
221
- acc[token.address] = (Number(token.balance) * Math.pow(10, dec)).toString();
222
- return acc;
223
- }, {});
224
- return [3 /*break*/, 3];
225
- case 2:
226
- balances = rawBalances;
227
- _c.label = 3;
228
- case 3:
229
- chainIds = {
230
- coingecko: [],
231
- };
232
- for (_i = 0, chains_1 = chains; _i < chains_1.length; _i++) {
233
- chain = chains_1[_i];
234
- chainIds[chain] = [];
235
- }
236
- normalizedBalances = {};
237
- for (_a = 0, _b = Object.keys(balances); _a < _b.length; _a++) {
238
- tokenAddressOrName = _b[_a];
239
- normalizedAddressOrName = tokenAddressOrName;
240
- normalizedBalance = balances[tokenAddressOrName];
241
- if (tokenAddressOrName === "0x0000000000000000000000000000000000000000") {
242
- normalizedAddressOrName = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; // Normalize ETH to WETH
243
- }
244
- if (typeof normalizedBalance === "object") {
245
- normalizedBalances[normalizedAddressOrName] = normalizedBalance.toFixed(); // Some adapters return a BigNumber from bignumber.js so the results must be normalized
246
- }
247
- else {
248
- generalUtil_1.sumSingleBalance(normalizedBalances, normalizedAddressOrName, normalizedBalance);
249
- }
250
- if (normalizedAddressOrName.startsWith("0x")) {
251
- chainIds.ethereum.push(normalizedAddressOrName);
252
- }
253
- else if (normalizedAddressOrName.includes(":")) {
254
- chain = normalizedAddressOrName.split(':')[0];
255
- chainIds[chain].push(normalizedAddressOrName.slice(chain.length + 1));
256
- }
257
- else {
258
- chainIds.coingecko.push(normalizedAddressOrName);
259
- }
260
- }
261
- return [4 /*yield*/, getChainSymbolsAndDecimals(chainIds, coingeckoMaxRetries)];
262
- case 4:
263
- symbolsAndDecimals = _c.sent();
264
- if (!(timestamp === "now")) return [3 /*break*/, 5];
265
- allChainTokenPrices = symbolsAndDecimals.reduce(function (prices, item) {
266
- var chain, address;
267
- if (item.coin.includes(":")) {
268
- chain = item.coin.split(':')[0];
269
- address = item.coin.split(':')[1];
270
- }
271
- else {
272
- chain = "coingecko";
273
- address = item.coin;
274
- }
275
- if (prices[chain] === undefined) {
276
- prices[chain] = {};
277
- }
278
- prices[chain][address] = { usd: item.price };
279
- return prices;
280
- }, {});
281
- return [3 /*break*/, 7];
282
- case 5: return [4 /*yield*/, getHistoricalChainPrices(chainIds, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries)];
283
- case 6:
284
- allChainTokenPrices = _c.sent();
285
- _c.label = 7;
286
- case 7:
287
- usdTokenBalances = {};
288
- tokenBalances = {};
289
- usdAmounts = Object.entries(normalizedBalances).map(function (_a) {
290
- var address = _a[0], balance = _a[1];
291
- return __awaiter(_this, void 0, void 0, function () {
292
- var amount, price, tokenSymbol, normalizedAddress_1, chainSelector_1, chainTokenPrices, chainAddress_1, coinData, tokenDecimals, usdAmount;
293
- var _b, _c, _d, _e;
294
- return __generator(this, function (_f) {
295
- try {
296
- if (address.startsWith("0x") || address.includes(":")) {
297
- normalizedAddress_1 = address;
298
- chainSelector_1 = "ethereum";
299
- chains.forEach(function (chain) {
300
- if (address.startsWith(chain)) {
301
- chainSelector_1 = chain;
302
- normalizedAddress_1 = address.slice(chain.length + 1);
303
- }
304
- });
305
- chainTokenPrices = (_b = allChainTokenPrices[chainSelector_1]) !== null && _b !== void 0 ? _b : {};
306
- chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1.toLowerCase();
307
- coinData = symbolsAndDecimals.find(function (coin) { return coin.coin === chainAddress_1; });
308
- tokenSymbol = (_c = coinData === null || coinData === void 0 ? void 0 : coinData.symbol) === null || _c === void 0 ? void 0 : _c.toUpperCase();
309
- if (tokenSymbol === undefined || tokenSymbol === null) {
310
- tokenSymbol = "UNKNOWN (" + address + ")";
311
- }
312
- tokenDecimals = coinData === null || coinData === void 0 ? void 0 : coinData.decimals;
313
- if (tokenDecimals === undefined) {
314
- amount = 0;
315
- }
316
- else {
317
- amount = Number(balance) / Math.pow(10, Number(tokenDecimals));
318
- }
319
- price = (_d = chainTokenPrices[normalizedAddress_1.toLowerCase()]) === null || _d === void 0 ? void 0 : _d.usd;
320
- }
321
- else {
322
- tokenSymbol = address;
323
- price = (_e = allChainTokenPrices["coingecko"][address.toLowerCase()]) === null || _e === void 0 ? void 0 : _e.usd;
324
- amount = Number(balance);
325
- }
326
- if (price === undefined) {
327
- if (verbose) {
328
- console.log("Token " + address + " is not on coingecko, it'll be ignored");
329
- }
330
- price = 0;
331
- }
332
- addTokenBalance(tokenBalances, tokenSymbol, amount);
333
- usdAmount = amount * price;
334
- addTokenBalance(usdTokenBalances, tokenSymbol, usdAmount);
335
- return [2 /*return*/, { usdAmount: usdAmount, tokenSymbol: tokenSymbol }];
336
- }
337
- catch (e) {
338
- console.error("Error on token " + address + ", we'll just assume it's price is 0...", e);
339
- return [2 /*return*/, {
340
- usdAmount: 0,
341
- tokenSymbol: "ERROR " + address,
342
- }];
343
- }
344
- return [2 /*return*/];
345
- });
346
- });
347
- });
348
- if (!verbose) return [3 /*break*/, 9];
349
- return [4 /*yield*/, Promise.all(usdAmounts)];
350
- case 8:
351
- (_c.sent())
352
- .sort(function (a, b) { return b.usdAmount - a.usdAmount; })
353
- .map(function (token) {
354
- console.log(token.tokenSymbol.padEnd(25, " "), humanizeNumber_1.humanizeNumber(token.usdAmount));
355
- });
356
- _c.label = 9;
357
- case 9: return [4 /*yield*/, Promise.all(usdAmounts)];
358
- case 10:
359
- usdTvl = (_c.sent()).reduce(function (sum, token) {
360
- return sum + token.usdAmount;
361
- }, 0);
362
- return [2 /*return*/, {
363
- usdTvl: usdTvl,
364
- usdTokenBalances: usdTokenBalances,
365
- tokenBalances: tokenBalances,
366
- }];
367
- }
368
- });
369
- });
370
- }
371
- 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
+ };
107
+ function currentCoingeckoUrls(chain) {
108
+ if (chain === 'coingecko') {
109
+ return "v3/simple/price?ids";
110
+ }
111
+ var platformId = exports.chainToCoingeckoId[chain];
112
+ if (platformId !== undefined) {
113
+ return "v3/simple/token_price/" + platformId + "?contract_addresses";
114
+ }
115
+ throw new Error("Chain not supported");
116
+ }
117
+ var chains = Object.keys(exports.chainToCoingeckoId);
118
+ function getHistoricalChainPrices(ids, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
119
+ return __awaiter(this, void 0, void 0, function () {
120
+ var chainPrices, _i, _a, chain, _b, _c;
121
+ return __generator(this, function (_d) {
122
+ switch (_d.label) {
123
+ case 0:
124
+ chainPrices = {};
125
+ _i = 0, _a = chains.concat(["coingecko"]);
126
+ _d.label = 1;
127
+ case 1:
128
+ if (!(_i < _a.length)) return [3 /*break*/, 5];
129
+ chain = _a[_i];
130
+ if (!(ids[chain].length === 0)) return [3 /*break*/, 2];
131
+ chainPrices[chain] = {};
132
+ return [3 /*break*/, 4];
133
+ case 2:
134
+ _b = chainPrices;
135
+ _c = chain;
136
+ return [4 /*yield*/, prices_1.getHistoricalTokenPrices(ids[chain], historicalCoingeckoUrls(chain), timestamp, getCoingeckoLock, coingeckoMaxRetries)];
137
+ case 3:
138
+ _b[_c] = _d.sent();
139
+ _d.label = 4;
140
+ case 4:
141
+ _i++;
142
+ return [3 /*break*/, 1];
143
+ case 5: return [2 /*return*/, chainPrices];
144
+ }
145
+ });
146
+ });
147
+ }
148
+ function getChainSymbolsAndDecimals(ids, maxRetries) {
149
+ return __awaiter(this, void 0, void 0, function () {
150
+ var allCoins, i, response;
151
+ return __generator(this, function (_a) {
152
+ switch (_a.label) {
153
+ case 0:
154
+ allCoins = Object.entries(ids).map(function (chain) {
155
+ return chain[1].map(function (coin) { return chain[0] === "coingecko" ? coin.toLowerCase() : chain[0] + ":" + coin.toLowerCase(); });
156
+ })
157
+ .reduce(function (acc, coins) {
158
+ coins.forEach(function (coin) {
159
+ acc.add(coin);
160
+ });
161
+ return acc;
162
+ }, new Set([]));
163
+ i = 0;
164
+ _a.label = 1;
165
+ case 1:
166
+ if (!(i < maxRetries)) return [3 /*break*/, 4];
167
+ return [4 /*yield*/, node_fetch_1.default('https://api.llama.fi/coins', {
168
+ method: 'POST',
169
+ body: JSON.stringify({
170
+ coins: Array.from(allCoins)
171
+ })
172
+ }).then(function (response) { return response.json(); })];
173
+ case 2:
174
+ response = _a.sent();
175
+ if (Array.isArray(response)) {
176
+ return [2 /*return*/, response];
177
+ }
178
+ _a.label = 3;
179
+ case 3:
180
+ i++;
181
+ return [3 /*break*/, 1];
182
+ case 4: throw new Error("api.llama.fi/coins failed");
183
+ }
184
+ });
185
+ });
186
+ }
187
+ function default_1(rawBalances, timestamp, verbose, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries) {
188
+ if (verbose === void 0) { verbose = false; }
189
+ if (knownTokenPrices === void 0) { knownTokenPrices = {}; }
190
+ if (getCoingeckoLock === void 0) { getCoingeckoLock = function () { return Promise.resolve(); }; }
191
+ if (coingeckoMaxRetries === void 0) { coingeckoMaxRetries = 3; }
192
+ return __awaiter(this, void 0, void 0, function () {
193
+ var balances, callTokenDecimals_1, chainIds, _i, chains_1, chain, normalizedBalances, _a, _b, tokenAddressOrName, normalizedAddressOrName, normalizedBalance, chain, symbolsAndDecimals, allChainTokenPrices, usdTokenBalances, tokenBalances, usdAmounts, usdTvl;
194
+ var _this = this;
195
+ return __generator(this, function (_c) {
196
+ switch (_c.label) {
197
+ case 0:
198
+ if (!(rawBalances instanceof Array)) return [3 /*break*/, 2];
199
+ return [4 /*yield*/, abi_1.multiCall({
200
+ abi: "erc20:decimals",
201
+ calls: rawBalances.map(function (token) { return ({
202
+ target: token.address,
203
+ params: [],
204
+ }); }),
205
+ })];
206
+ case 1:
207
+ callTokenDecimals_1 = (_c.sent()).output;
208
+ balances = rawBalances.reduce(function (acc, token, index) {
209
+ var dec;
210
+ if (callTokenDecimals_1[index].success) {
211
+ dec = Number(callTokenDecimals_1[index].output);
212
+ }
213
+ else {
214
+ if (token.address === "0x0000000000000000000000000000000000000000") {
215
+ dec = 18;
216
+ }
217
+ else {
218
+ dec = NaN;
219
+ }
220
+ }
221
+ acc[token.address] = (Number(token.balance) * Math.pow(10, dec)).toString();
222
+ return acc;
223
+ }, {});
224
+ return [3 /*break*/, 3];
225
+ case 2:
226
+ balances = rawBalances;
227
+ _c.label = 3;
228
+ case 3:
229
+ chainIds = {
230
+ coingecko: [],
231
+ };
232
+ for (_i = 0, chains_1 = chains; _i < chains_1.length; _i++) {
233
+ chain = chains_1[_i];
234
+ chainIds[chain] = [];
235
+ }
236
+ normalizedBalances = {};
237
+ for (_a = 0, _b = Object.keys(balances); _a < _b.length; _a++) {
238
+ tokenAddressOrName = _b[_a];
239
+ normalizedAddressOrName = tokenAddressOrName;
240
+ normalizedBalance = balances[tokenAddressOrName];
241
+ if (tokenAddressOrName === "0x0000000000000000000000000000000000000000") {
242
+ normalizedAddressOrName = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; // Normalize ETH to WETH
243
+ }
244
+ if (typeof normalizedBalance === "object") {
245
+ normalizedBalances[normalizedAddressOrName] = normalizedBalance.toFixed(); // Some adapters return a BigNumber from bignumber.js so the results must be normalized
246
+ }
247
+ else {
248
+ generalUtil_1.sumSingleBalance(normalizedBalances, normalizedAddressOrName, normalizedBalance);
249
+ }
250
+ if (normalizedAddressOrName.startsWith("0x")) {
251
+ chainIds.ethereum.push(normalizedAddressOrName);
252
+ }
253
+ else if (normalizedAddressOrName.includes(":")) {
254
+ chain = normalizedAddressOrName.split(':')[0];
255
+ chainIds[chain].push(normalizedAddressOrName.slice(chain.length + 1));
256
+ }
257
+ else {
258
+ chainIds.coingecko.push(normalizedAddressOrName);
259
+ }
260
+ }
261
+ return [4 /*yield*/, getChainSymbolsAndDecimals(chainIds, coingeckoMaxRetries)];
262
+ case 4:
263
+ symbolsAndDecimals = _c.sent();
264
+ if (!(timestamp === "now")) return [3 /*break*/, 5];
265
+ allChainTokenPrices = symbolsAndDecimals.reduce(function (prices, item) {
266
+ var chain, address;
267
+ if (item.coin.includes(":")) {
268
+ chain = item.coin.split(':')[0];
269
+ address = item.coin.split(':')[1];
270
+ }
271
+ else {
272
+ chain = "coingecko";
273
+ address = item.coin;
274
+ }
275
+ if (prices[chain] === undefined) {
276
+ prices[chain] = {};
277
+ }
278
+ prices[chain][address] = { usd: item.price };
279
+ return prices;
280
+ }, {});
281
+ return [3 /*break*/, 7];
282
+ case 5: return [4 /*yield*/, getHistoricalChainPrices(chainIds, timestamp, knownTokenPrices, getCoingeckoLock, coingeckoMaxRetries)];
283
+ case 6:
284
+ allChainTokenPrices = _c.sent();
285
+ _c.label = 7;
286
+ case 7:
287
+ usdTokenBalances = {};
288
+ tokenBalances = {};
289
+ usdAmounts = Object.entries(normalizedBalances).map(function (_a) {
290
+ var address = _a[0], balance = _a[1];
291
+ return __awaiter(_this, void 0, void 0, function () {
292
+ var amount, price, tokenSymbol, normalizedAddress_1, chainSelector_1, chainTokenPrices, chainAddress_1, coinData, tokenDecimals, usdAmount;
293
+ var _b, _c, _d, _e;
294
+ return __generator(this, function (_f) {
295
+ try {
296
+ if (address.startsWith("0x") || address.includes(":")) {
297
+ normalizedAddress_1 = address;
298
+ chainSelector_1 = "ethereum";
299
+ chains.forEach(function (chain) {
300
+ if (address.startsWith(chain)) {
301
+ chainSelector_1 = chain;
302
+ normalizedAddress_1 = address.slice(chain.length + 1);
303
+ }
304
+ });
305
+ chainTokenPrices = (_b = allChainTokenPrices[chainSelector_1]) !== null && _b !== void 0 ? _b : {};
306
+ chainAddress_1 = chainSelector_1 + ":" + normalizedAddress_1.toLowerCase();
307
+ coinData = symbolsAndDecimals.find(function (coin) { return coin.coin === chainAddress_1; });
308
+ tokenSymbol = (_c = coinData === null || coinData === void 0 ? void 0 : coinData.symbol) === null || _c === void 0 ? void 0 : _c.toUpperCase();
309
+ if (tokenSymbol === undefined || tokenSymbol === null) {
310
+ tokenSymbol = "UNKNOWN (" + address + ")";
311
+ }
312
+ tokenDecimals = coinData === null || coinData === void 0 ? void 0 : coinData.decimals;
313
+ if (tokenDecimals === undefined) {
314
+ amount = 0;
315
+ }
316
+ else {
317
+ amount = Number(balance) / Math.pow(10, Number(tokenDecimals));
318
+ }
319
+ price = (_d = chainTokenPrices[normalizedAddress_1.toLowerCase()]) === null || _d === void 0 ? void 0 : _d.usd;
320
+ }
321
+ else {
322
+ tokenSymbol = address;
323
+ price = (_e = allChainTokenPrices["coingecko"][address.toLowerCase()]) === null || _e === void 0 ? void 0 : _e.usd;
324
+ amount = Number(balance);
325
+ }
326
+ if (price === undefined) {
327
+ if (verbose) {
328
+ console.log("Token " + address + " is not on coingecko, it'll be ignored");
329
+ }
330
+ price = 0;
331
+ }
332
+ addTokenBalance(tokenBalances, tokenSymbol, amount);
333
+ usdAmount = amount * price;
334
+ addTokenBalance(usdTokenBalances, tokenSymbol, usdAmount);
335
+ return [2 /*return*/, { usdAmount: usdAmount, tokenSymbol: tokenSymbol }];
336
+ }
337
+ catch (e) {
338
+ console.error("Error on token " + address + ", we'll just assume it's price is 0...", e);
339
+ return [2 /*return*/, {
340
+ usdAmount: 0,
341
+ tokenSymbol: "ERROR " + address,
342
+ }];
343
+ }
344
+ return [2 /*return*/];
345
+ });
346
+ });
347
+ });
348
+ if (!verbose) return [3 /*break*/, 9];
349
+ return [4 /*yield*/, Promise.all(usdAmounts)];
350
+ case 8:
351
+ (_c.sent())
352
+ .sort(function (a, b) { return b.usdAmount - a.usdAmount; })
353
+ .map(function (token) {
354
+ console.log(token.tokenSymbol.padEnd(25, " "), humanizeNumber_1.humanizeNumber(token.usdAmount));
355
+ });
356
+ _c.label = 9;
357
+ case 9: return [4 /*yield*/, Promise.all(usdAmounts)];
358
+ case 10:
359
+ usdTvl = (_c.sent()).reduce(function (sum, token) {
360
+ return sum + token.usdAmount;
361
+ }, 0);
362
+ return [2 /*return*/, {
363
+ usdTvl: usdTvl,
364
+ usdTokenBalances: usdTokenBalances,
365
+ tokenBalances: tokenBalances,
366
+ }];
367
+ }
368
+ });
369
+ });
370
+ }
371
+ exports.default = default_1;