@defillama/sdk 5.0.240 → 5.0.242

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 (131) hide show
  1. package/build/Balances.d.ts +139 -0
  2. package/build/Balances.js +363 -0
  3. package/build/ChainApi.d.ts +142 -0
  4. package/build/ChainApi.js +310 -0
  5. package/build/abi/abi2.d.ts +5 -0
  6. package/build/abi/abi2.js +215 -0
  7. package/build/abi/cachedABIs.d.ts +4 -0
  8. package/build/abi/cachedABIs.js +8 -0
  9. package/build/abi/convertResults.d.ts +5 -0
  10. package/build/abi/convertResults.js +60 -0
  11. package/build/abi/index.d.ts +31 -0
  12. package/build/abi/index.js +327 -0
  13. package/build/abi/multicall.d.ts +23 -0
  14. package/build/abi/multicall.js +383 -0
  15. package/build/abi/multicall3.d.ts +7 -0
  16. package/build/abi/multicall3.js +403 -0
  17. package/build/abi/rpcCall.d.ts +4 -0
  18. package/build/abi/rpcCall.js +62 -0
  19. package/build/abi/tron.d.ts +33 -0
  20. package/build/abi/tron.js +116 -0
  21. package/build/api.d.ts +10 -0
  22. package/build/api.js +45 -0
  23. package/build/api2.d.ts +5 -0
  24. package/build/api2.js +41 -0
  25. package/build/chains/algorand.d.ts +277 -0
  26. package/build/chains/algorand.js +618 -0
  27. package/build/chains/aptos.d.ts +269 -0
  28. package/build/chains/aptos.js +700 -0
  29. package/build/chains/cardano.d.ts +242 -0
  30. package/build/chains/cardano.js +407 -0
  31. package/build/chains/cosmos.d.ts +252 -0
  32. package/build/chains/cosmos.js +618 -0
  33. package/build/chains/icp.d.ts +235 -0
  34. package/build/chains/icp.js +1040 -0
  35. package/build/chains/index.d.ts +23 -0
  36. package/build/chains/index.js +59 -0
  37. package/build/chains/near.d.ts +192 -0
  38. package/build/chains/near.js +446 -0
  39. package/build/chains/rpc.d.ts +125 -0
  40. package/build/chains/rpc.js +326 -0
  41. package/build/chains/starknet.d.ts +205 -0
  42. package/build/chains/starknet.js +884 -0
  43. package/build/chains/stellar.d.ts +410 -0
  44. package/build/chains/stellar.js +1082 -0
  45. package/build/chains/substrate.d.ts +333 -0
  46. package/build/chains/substrate.js +1042 -0
  47. package/build/chains/sui.d.ts +365 -0
  48. package/build/chains/sui.js +1135 -0
  49. package/build/chains/svm.d.ts +340 -0
  50. package/build/chains/svm.js +760 -0
  51. package/build/chains/tezos.d.ts +225 -0
  52. package/build/chains/tezos.js +412 -0
  53. package/build/chains/ton.d.ts +353 -0
  54. package/build/chains/ton.js +788 -0
  55. package/build/chains/tron.d.ts +45 -0
  56. package/build/chains/tron.js +110 -0
  57. package/build/chains/utxo.d.ts +210 -0
  58. package/build/chains/utxo.js +678 -0
  59. package/build/chains/xrpl.d.ts +179 -0
  60. package/build/chains/xrpl.js +467 -0
  61. package/build/computeTVL/blocks.d.ts +1 -0
  62. package/build/computeTVL/blocks.js +12 -0
  63. package/build/computeTVL/humanizeNumber.d.ts +1 -0
  64. package/build/computeTVL/humanizeNumber.js +19 -0
  65. package/build/erc20/index.d.ts +31 -0
  66. package/build/erc20/index.js +56 -0
  67. package/build/eth/index.d.ts +22 -0
  68. package/build/eth/index.js +118 -0
  69. package/build/general.d.ts +5 -0
  70. package/build/general.js +20 -0
  71. package/build/generalUtil.d.ts +8 -0
  72. package/build/generalUtil.js +73 -0
  73. package/build/index.d.ts +32 -0
  74. package/build/index.js +74 -0
  75. package/build/providers.json +1 -0
  76. package/build/types.d.ts +76 -0
  77. package/build/types.js +2 -0
  78. package/build/util/LlamaProvider.d.ts +46 -0
  79. package/build/util/LlamaProvider.js +367 -0
  80. package/build/util/blocks.d.ts +36 -0
  81. package/build/util/blocks.js +500 -0
  82. package/build/util/cache.d.ts +45 -0
  83. package/build/util/cache.js +309 -0
  84. package/build/util/chainUtils/data.json +1437 -0
  85. package/build/util/chainUtils/index.d.ts +11 -0
  86. package/build/util/chainUtils/index.js +136 -0
  87. package/build/util/coins.d.ts +19 -0
  88. package/build/util/coins.js +124 -0
  89. package/build/util/common.d.ts +34 -0
  90. package/build/util/common.js +376 -0
  91. package/build/util/computeTVL.d.ts +19 -0
  92. package/build/util/computeTVL.js +102 -0
  93. package/build/util/cosmos.d.ts +1 -0
  94. package/build/util/cosmos.js +8 -0
  95. package/build/util/debugLog.d.ts +4 -0
  96. package/build/util/debugLog.js +18 -0
  97. package/build/util/discord.d.ts +1 -0
  98. package/build/util/discord.js +33 -0
  99. package/build/util/elastic.d.ts +43 -0
  100. package/build/util/elastic.js +113 -0
  101. package/build/util/env.d.ts +20 -0
  102. package/build/util/env.js +176 -0
  103. package/build/util/graph.d.ts +8 -0
  104. package/build/util/graph.js +50 -0
  105. package/build/util/index.d.ts +32 -0
  106. package/build/util/index.js +227 -0
  107. package/build/util/indexer.compatibility.d.ts +6 -0
  108. package/build/util/indexer.compatibility.js +64 -0
  109. package/build/util/indexer.d.ts +92 -0
  110. package/build/util/indexer.js +850 -0
  111. package/build/util/indexerSql.d.ts +9 -0
  112. package/build/util/indexerSql.js +102 -0
  113. package/build/util/internal-cache.d.ts +11 -0
  114. package/build/util/internal-cache.js +77 -0
  115. package/build/util/logs.d.ts +58 -0
  116. package/build/util/logs.decode.shared.d.ts +8 -0
  117. package/build/util/logs.decode.shared.js +289 -0
  118. package/build/util/logs.js +432 -0
  119. package/build/util/promisePool.d.ts +8 -0
  120. package/build/util/promisePool.js +31 -0
  121. package/build/util/r2.d.ts +4 -0
  122. package/build/util/r2.js +52 -0
  123. package/build/util/removeRedundantRPCs.d.ts +1 -0
  124. package/build/util/removeRedundantRPCs.js +49 -0
  125. package/build/util/transactions.d.ts +35 -0
  126. package/build/util/transactions.js +22 -0
  127. package/build/util/updateChainLabels.d.ts +1 -0
  128. package/build/util/updateChainLabels.js +5 -0
  129. package/build/util/updateProviderList.d.ts +1 -0
  130. package/build/util/updateProviderList.js +275 -0
  131. package/package.json +3 -2
@@ -0,0 +1,309 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ONE_WEEK = exports.currentVersion = void 0;
4
+ exports.readCache = readCache;
5
+ exports.writeCache = writeCache;
6
+ exports.parseCache = parseCache;
7
+ exports.compressCache = compressCache;
8
+ exports.getTempLocalCache = getTempLocalCache;
9
+ exports.deleteCache = deleteCache;
10
+ exports.readExpiringJsonCache = readExpiringJsonCache;
11
+ exports.writeExpiringJsonCache = writeExpiringJsonCache;
12
+ exports.cachedFetch = cachedFetch;
13
+ const debugLog_1 = require("./debugLog");
14
+ const r2_1 = require("./r2");
15
+ const zlib_1 = require("zlib");
16
+ const util_1 = require("util");
17
+ const env_1 = require("./env");
18
+ const common_1 = require("./common");
19
+ const brotliOptions = {
20
+ [zlib_1.constants.BROTLI_PARAM_MODE]: zlib_1.constants.BROTLI_MODE_TEXT,
21
+ [zlib_1.constants.BROTLI_PARAM_QUALITY]: zlib_1.constants.BROTLI_MAX_QUALITY,
22
+ };
23
+ function zipAsync(data) {
24
+ return (0, util_1.promisify)(zlib_1.brotliCompress)(data, brotliOptions);
25
+ }
26
+ function unzipAsync(data) {
27
+ return (0, util_1.promisify)(zlib_1.brotliDecompress)(data, brotliOptions).then(r => r.toString());
28
+ }
29
+ const _fs = require('fs');
30
+ const fs = _fs.promises;
31
+ const path = require('path');
32
+ const foldersCreated = {};
33
+ exports.currentVersion = 'zlib-1.0';
34
+ function getCacheRootFolder() {
35
+ const defaultCacheRootFolder = path.join(__dirname, 'local_cache');
36
+ return path.join((0, env_1.getEnvCacheFolder)(defaultCacheRootFolder), exports.currentVersion);
37
+ }
38
+ function getFilePath(file) {
39
+ return path.join(getCacheRootFolder(), file);
40
+ }
41
+ async function createSubPath(folderPath) {
42
+ const datRoot = getCacheRootFolder();
43
+ folderPath = folderPath.replace(datRoot, '');
44
+ if (foldersCreated[folderPath])
45
+ return;
46
+ try {
47
+ await fs.mkdir(path.join(datRoot, folderPath), { recursive: true });
48
+ }
49
+ catch (error) {
50
+ // sdk.log('Error creating folder path:', error)
51
+ }
52
+ foldersCreated[folderPath] = true;
53
+ }
54
+ const ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
55
+ async function readCache(file, options = {}) {
56
+ if (options.skipCompression)
57
+ file = `${file}-uncompressed`;
58
+ try {
59
+ const data = await readFile();
60
+ return await parseCache(data, options);
61
+ }
62
+ catch (error) {
63
+ // debugLog('Error reading cache:', error)
64
+ return {};
65
+ }
66
+ async function readFile() {
67
+ var _a;
68
+ try {
69
+ const data = await readDataFromLocal((_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false);
70
+ return data;
71
+ }
72
+ catch (error) {
73
+ // debugLog('Error reading cache:', error)
74
+ return readDataFromR2();
75
+ }
76
+ }
77
+ async function readDataFromLocal(readFromR2Cache) {
78
+ const filePath = getFilePath(file);
79
+ if (options.checkIfRecent) {
80
+ const stats = await fs.stat(filePath);
81
+ if (Date.now() - stats.mtimeMs > ONE_DAY_IN_MS)
82
+ throw new Error('File too old, read from R2 instead');
83
+ }
84
+ if (readFromR2Cache)
85
+ throw new Error('Read from R2 cache');
86
+ const data = await fs.readFile(filePath);
87
+ return data;
88
+ }
89
+ async function readDataFromR2() {
90
+ var _a;
91
+ if (options.skipR2Cache)
92
+ return;
93
+ try {
94
+ const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file, { throwErrorOnFail: (_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false });
95
+ if (r2Data) {
96
+ // `file` already carries the `-uncompressed` suffix (added above), so omit skipCompression here to avoid writeCache appending it again
97
+ await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true });
98
+ return r2Data;
99
+ }
100
+ }
101
+ catch (error) {
102
+ if (options.readFromR2Cache) {
103
+ // if we were forced to read from R2 cache, try reading from local cache if R2 fails
104
+ try {
105
+ const data = await readDataFromLocal(false);
106
+ return data;
107
+ }
108
+ catch (_e) { }
109
+ }
110
+ return {};
111
+ }
112
+ }
113
+ }
114
+ async function writeCache(file, data, options = {}) {
115
+ try {
116
+ const fileData = options.alreadyCompressed ? data : await compressCache(data, options);
117
+ if (!data || (typeof data === 'string' && data.length < 20) || fileData.length < 20) {
118
+ (0, debugLog_1.debugLog)('Data too small to cache: ', file);
119
+ return;
120
+ }
121
+ if (!options.skipCompression && isSameData(data, await readCache(file, { skipR2Cache: true, skipCompression: options.skipCompression, })))
122
+ return;
123
+ if (options.skipCompression)
124
+ file = `${file}-uncompressed`;
125
+ const filePath = getFilePath(file);
126
+ await createSubPath(path.dirname(filePath));
127
+ await fs.writeFile(filePath, fileData);
128
+ if (!options.skipR2CacheWrite) {
129
+ await (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData);
130
+ }
131
+ return fileData;
132
+ }
133
+ catch (error) {
134
+ if (typeof data === 'object' && data !== null && Object.keys(data).length === 0)
135
+ return;
136
+ (0, debugLog_1.debugLog)('Error writing cache:', error);
137
+ }
138
+ }
139
+ async function parseCache(dataString, options = {}) {
140
+ var _a;
141
+ let dataBuffer;
142
+ if (typeof dataString === 'string')
143
+ dataBuffer = Buffer.from(dataString, 'base64');
144
+ else {
145
+ dataBuffer = dataString;
146
+ dataString = dataBuffer.toString('utf8');
147
+ }
148
+ let decompressed;
149
+ let _unzipAsync = (str) => options.skipCompression ? dataString : unzipAsync(str); // if skipCompression is true, we don't decompress the data
150
+ try {
151
+ decompressed = await _unzipAsync((_a = dataBuffer) !== null && _a !== void 0 ? _a : dataString);
152
+ }
153
+ catch (e) {
154
+ // dataString = dataString.toString('utf8')
155
+ const convertedDataString = Buffer.from(dataString, 'base64');
156
+ decompressed = await _unzipAsync(convertedDataString);
157
+ }
158
+ return JSON.parse(decompressed).llamaWrapped;
159
+ }
160
+ async function compressCache(data, options = {}) {
161
+ const dataString = JSON.stringify({ version: exports.currentVersion, llamaWrapped: data });
162
+ if (options.skipCompression)
163
+ return dataString;
164
+ const comressedCache = await zipAsync(dataString);
165
+ return comressedCache;
166
+ }
167
+ function isSameData(data1, data2) {
168
+ return JSON.stringify(data1) === JSON.stringify(data2);
169
+ }
170
+ exports.ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
171
+ function getTempLocalCache({ file, defaultData = {}, clearAfter = exports.ONE_WEEK, returnWithSaveFunction = false }) {
172
+ const now = Math.floor(Date.now() / 1e3);
173
+ let emptyFile = { created: now, data: defaultData };
174
+ let fileData = emptyFile;
175
+ const filePath = getFilePath(file);
176
+ let saveCacheFlag = false; // to ensure that we save only once on exit
177
+ createSubPath(path.dirname(filePath)); // create folder if not exists, this is async but we don't care
178
+ try {
179
+ const data = _fs.readFileSync(filePath);
180
+ fileData = JSON.parse(data.toString());
181
+ if (now - fileData.created > clearAfter)
182
+ fileData = emptyFile; // clear cache
183
+ }
184
+ catch (error) { }
185
+ // save cache on exit
186
+ process.on('exit', _save);
187
+ process.on('SIGTERM', _save);
188
+ process.on('SIGINT', _save);
189
+ if (returnWithSaveFunction) {
190
+ return {
191
+ data: fileData.data,
192
+ saveCacheFile: () => saveCache(true)
193
+ };
194
+ }
195
+ return fileData.data;
196
+ function _save() {
197
+ saveCache(false);
198
+ }
199
+ function saveCache(skipCacheFlagCheck = false) {
200
+ if (!skipCacheFlagCheck) {
201
+ if (saveCacheFlag)
202
+ return;
203
+ saveCacheFlag = true;
204
+ }
205
+ try {
206
+ // debugLog('Saving cache to file:', filePath)
207
+ removePromisesAndFunctions(fileData);
208
+ _fs.writeFileSync(filePath, JSON.stringify(fileData));
209
+ }
210
+ catch (error) {
211
+ try {
212
+ (0, debugLog_1.debugLog)('Error saving cache:', error === null || error === void 0 ? void 0 : error.toString());
213
+ }
214
+ catch (error) { }
215
+ }
216
+ }
217
+ function removePromisesAndFunctions(data) {
218
+ if (typeof data === 'object') {
219
+ for (const key in data) {
220
+ if (typeof data[key] === 'function')
221
+ delete data[key];
222
+ if (data[key] instanceof Promise)
223
+ delete data[key];
224
+ else
225
+ removePromisesAndFunctions(data[key]);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ // this removes data only from the file cache, not from the R2 cache
231
+ async function deleteCache(file) {
232
+ const filePath = getFilePath(file);
233
+ await fs.unlink(filePath).catch(() => {
234
+ // ignore errors
235
+ });
236
+ }
237
+ async function readExpiringJsonCache(file) {
238
+ const options = { readFromR2Cache: false, skipR2Cache: true, skipCompression: true };
239
+ file = 'expiring/' + file;
240
+ const data = await readCache(file, options);
241
+ if (!data || Object.keys(data).length === 0)
242
+ return null;
243
+ if (data.expiryTimestamp < (Date.now() / 1e3)) {
244
+ await deleteCache(`${file}-uncompressed`);
245
+ return null;
246
+ }
247
+ return data.data;
248
+ }
249
+ async function writeExpiringJsonCache(file, data, { expireAfter = 60 * 60 * 24, // cache for 1 day by default
250
+ expiryTimestamp, } = {}) {
251
+ file = 'expiring/' + file;
252
+ if (!expiryTimestamp)
253
+ expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
254
+ const options = { skipR2CacheWrite: true, skipCompression: true };
255
+ await writeCache(file, { data, expiryTimestamp }, options);
256
+ }
257
+ async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache = true, cacheMaxAge = 0, writeCacheOptions = {
258
+ skipCompression: true,
259
+ skipR2CacheWrite: true,
260
+ }, readCacheOptions = {
261
+ skipCompression: true,
262
+ skipR2Cache: true,
263
+ }, }) {
264
+ if (!key) {
265
+ if (!endpoint || postData || fetcher)
266
+ throw new Error('Key is required for cachedFetch');
267
+ key = (0, common_1.getHash)(endpoint);
268
+ }
269
+ const fileKey = 'cached-fetch/' + key;
270
+ // check local cache first
271
+ if (cacheMaxAge > 0) {
272
+ try {
273
+ const filePath = getFilePath(readCacheOptions.skipCompression ? `${fileKey}-uncompressed` : fileKey);
274
+ const stats = await fs.stat(filePath);
275
+ if (Date.now() - stats.mtimeMs < cacheMaxAge) {
276
+ const cachedData = await readCache(fileKey, readCacheOptions);
277
+ if (cachedData && Object.keys(cachedData).length > 0)
278
+ return cachedData;
279
+ }
280
+ }
281
+ catch (error) {
282
+ // cache miss or stat error, proceed to fetch
283
+ }
284
+ }
285
+ try {
286
+ let data;
287
+ if (fetcher)
288
+ data = await fetcher();
289
+ else {
290
+ if (!endpoint)
291
+ throw new Error('Either fetcher or endpoint is required for cachedFetch');
292
+ if (postData)
293
+ data = await (0, common_1.postJson)(endpoint, postData, fetchOptions);
294
+ else
295
+ data = await (0, common_1.fetchJson)(endpoint, fetchOptions);
296
+ }
297
+ await writeCache(fileKey, data, writeCacheOptions);
298
+ return data;
299
+ }
300
+ catch (error) {
301
+ const errorMessage = (0, common_1.formError)(error).message;
302
+ (0, debugLog_1.debugLog)('Error in cachedFetch:', errorMessage);
303
+ // if fetch fails, read from cache (regardless of age)
304
+ const cachedData = await readCache(fileKey, readCacheOptions);
305
+ if (!cachedData && errorOnNoCache)
306
+ throw new Error('Fetch failed and no cache available');
307
+ return cachedData;
308
+ }
309
+ }