@defillama/sdk 5.0.146 → 5.0.147

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.
@@ -4,15 +4,17 @@ export declare function readCache(file: string, options?: ReadCacheOptions): Pro
4
4
  interface WriteCacheOptions {
5
5
  skipR2CacheWrite?: boolean;
6
6
  alreadyCompressed?: boolean;
7
+ skipCompression?: boolean;
7
8
  }
8
9
  interface ReadCacheOptions {
9
10
  checkIfRecent?: boolean;
10
11
  readFromR2Cache?: boolean;
11
12
  skipR2Cache?: boolean;
13
+ skipCompression?: boolean;
12
14
  }
13
15
  export declare function writeCache(file: string, data: any, options?: WriteCacheOptions): Promise<string | undefined>;
14
- export declare function parseCache(dataString: string | Buffer): Promise<any>;
15
- export declare function compressCache(data: any): Promise<Buffer>;
16
+ export declare function parseCache(dataString: string | Buffer, options?: ReadCacheOptions): Promise<any>;
17
+ export declare function compressCache(data: any, options?: WriteCacheOptions): Promise<string | Buffer>;
16
18
  export declare const ONE_WEEK: number;
17
19
  export declare function getTempLocalCache({ file, defaultData, clearAfter, returnWithSaveFunction }: {
18
20
  file: string;
@@ -43,21 +43,23 @@ async function createSubPath(folderPath) {
43
43
  }
44
44
  const ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
45
45
  async function readCache(file, options = {}) {
46
+ if (options.skipCompression)
47
+ file = `${file}-uncompressed`;
46
48
  try {
47
- const data = await readFile(file, options);
48
- return await parseCache(data);
49
+ const data = await readFile();
50
+ return await parseCache(data, options);
49
51
  }
50
52
  catch (error) {
51
53
  // debugLog('Error reading cache:', error)
52
54
  return {};
53
55
  }
54
- async function readFile(file, options = {}) {
56
+ async function readFile() {
55
57
  const filePath = getFilePath(file);
56
58
  try {
57
59
  if (options.checkIfRecent) {
58
60
  const stats = await fs.stat(filePath);
59
61
  if (Date.now() - stats.mtimeMs > ONE_DAY_IN_MS)
60
- throw new Error('File too old, read from R2 isntead');
62
+ throw new Error('File too old, read from R2 instead');
61
63
  }
62
64
  if (options.readFromR2Cache)
63
65
  throw new Error('Read from R2 cache');
@@ -70,7 +72,7 @@ async function readCache(file, options = {}) {
70
72
  return;
71
73
  const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file);
72
74
  if (r2Data) {
73
- await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true });
75
+ await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true, skipCompression: options.skipCompression });
74
76
  return r2Data;
75
77
  }
76
78
  }
@@ -79,13 +81,15 @@ async function readCache(file, options = {}) {
79
81
  exports.readCache = readCache;
80
82
  async function writeCache(file, data, options = {}) {
81
83
  try {
82
- const fileData = options.alreadyCompressed ? data : await compressCache(data);
84
+ const fileData = options.alreadyCompressed ? data : await compressCache(data, options);
83
85
  if (!data || (typeof data === 'string' && data.length < 20) || fileData.length < 20) {
84
86
  (0, debugLog_1.debugLog)('Data too small to cache: ', file);
85
87
  return;
86
88
  }
87
- if (isSameData(data, await readCache(file, { skipR2Cache: true })))
89
+ if (isSameData(data, await readCache(file, { skipR2Cache: true, skipCompression: options.skipCompression, })))
88
90
  return;
91
+ if (options.skipCompression)
92
+ file = `${file}-uncompressed`;
89
93
  const filePath = getFilePath(file);
90
94
  await createSubPath(path.dirname(filePath));
91
95
  await fs.writeFile(filePath, fileData);
@@ -99,23 +103,33 @@ async function writeCache(file, data, options = {}) {
99
103
  }
100
104
  }
101
105
  exports.writeCache = writeCache;
102
- async function parseCache(dataString) {
106
+ async function parseCache(dataString, options = {}) {
107
+ var _a;
108
+ let dataBuffer;
103
109
  if (typeof dataString === 'string')
104
- dataString = Buffer.from(dataString, 'base64');
110
+ dataBuffer = Buffer.from(dataString, 'base64');
111
+ else {
112
+ dataBuffer = dataString;
113
+ dataString = dataBuffer.toString('utf8');
114
+ }
105
115
  let decompressed;
116
+ let _unzipAsync = (str) => options.skipCompression ? dataString : unzipAsync(str); // if skipCompression is true, we don't decompress the data
106
117
  try {
107
- decompressed = await unzipAsync(dataString);
118
+ decompressed = await _unzipAsync((_a = dataBuffer) !== null && _a !== void 0 ? _a : dataString);
108
119
  }
109
120
  catch (e) {
110
- dataString = dataString.toString('utf8');
111
- dataString = Buffer.from(dataString, 'base64');
112
- decompressed = await unzipAsync(dataString);
121
+ // dataString = dataString.toString('utf8')
122
+ const convertedDataString = Buffer.from(dataString, 'base64');
123
+ decompressed = await _unzipAsync(convertedDataString);
113
124
  }
114
125
  return JSON.parse(decompressed).llamaWrapped;
115
126
  }
116
127
  exports.parseCache = parseCache;
117
- async function compressCache(data) {
118
- const comressedCache = await zipAsync(JSON.stringify({ version: exports.currentVersion, llamaWrapped: data }));
128
+ async function compressCache(data, options = {}) {
129
+ const dataString = JSON.stringify({ version: exports.currentVersion, llamaWrapped: data });
130
+ if (options.skipCompression)
131
+ return dataString;
132
+ const comressedCache = await zipAsync(dataString);
119
133
  return comressedCache;
120
134
  }
121
135
  exports.compressCache = compressCache;
@@ -207,7 +221,7 @@ expiryTimestamp, }) {
207
221
  file = 'expiring/' + file;
208
222
  if (!expiryTimestamp)
209
223
  expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
210
- const options = { skipR2CacheWrite: true };
224
+ const options = { skipR2CacheWrite: true, skipCompression: true };
211
225
  await writeCache(file, { data, expiryTimestamp }, options);
212
226
  }
213
227
  exports.writeExpiringJsonCache = writeExpiringJsonCache;
@@ -26,3 +26,4 @@ export declare function normalizeBalances(balances: {
26
26
  }): {
27
27
  [address: string]: string;
28
28
  };
29
+ export declare function tableToString(data: any, columns?: any): string;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.convertToBigInt = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
6
+ exports.tableToString = exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.convertToBigInt = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
7
7
  const general_1 = require("../general");
8
8
  const ethers_1 = require("ethers");
9
9
  const generalUtil_1 = require("../generalUtil");
@@ -162,3 +162,34 @@ function normalizeBalances(balances) {
162
162
  return balances;
163
163
  }
164
164
  exports.normalizeBalances = normalizeBalances;
165
+ function tableToString(data, columns) {
166
+ let tableString = '';
167
+ if (!data || !Array.isArray(data) || data.length === 0) {
168
+ return '';
169
+ }
170
+ if (!columns || !Array.isArray(columns) || columns.length === 0) {
171
+ columns = Object.keys(data[0] || {});
172
+ }
173
+ // Add the header row
174
+ const headerObject = {};
175
+ const headerObject1 = {};
176
+ columns.forEach((col) => {
177
+ headerObject[col] = col;
178
+ headerObject1[col] = '---';
179
+ });
180
+ data.unshift(headerObject1);
181
+ data.unshift(headerObject);
182
+ // Calculate the maximum width for each column
183
+ const columnWidths = columns.map((col) => Math.max(col.length, ...data.map((row) => (row[col] !== undefined ? String(row[col]).length : 0))));
184
+ // Add the data rows
185
+ data.forEach((row) => {
186
+ // Format the row with padded values
187
+ const tableRow = columns.map((col, index) => {
188
+ const cell = row[col] !== undefined ? String(row[col]) : '';
189
+ return cell.padEnd(columnWidths[index], ' ');
190
+ }).join(' | ');
191
+ tableString += tableRow + '\n';
192
+ });
193
+ return tableString;
194
+ }
195
+ exports.tableToString = tableToString;
@@ -6,15 +6,6 @@ declare enum TokenTypes {
6
6
  ERC20 = "erc20",
7
7
  ERC721 = "erc721"
8
8
  }
9
- export declare function getTokens(address: string | string[], { onlyWhitelisted, skipCacheRead, skipCache, chain, tokenType, }?: {
10
- onlyWhitelisted?: boolean;
11
- skipCacheRead?: boolean;
12
- skipCache?: boolean;
13
- chain?: string;
14
- tokenType?: TokenTypes;
15
- }): Promise<{
16
- [chain: string]: string[];
17
- }>;
18
9
  export type IndexerGetLogsOptions = {
19
10
  target?: Address;
20
11
  topic?: string;
@@ -37,6 +28,9 @@ export type IndexerGetLogsOptions = {
37
28
  debugMode?: boolean;
38
29
  noTarget?: boolean;
39
30
  parseLog?: boolean;
31
+ processor?: (logs: any[]) => Promise<void> | void;
32
+ maxBlockRange?: number;
33
+ allowParseFailure?: boolean;
40
34
  };
41
35
  export type IndexerGetTokenTransfersOptions = {
42
36
  target?: Address;
@@ -52,13 +46,22 @@ export type IndexerGetTokenTransfersOptions = {
52
46
  offset?: number;
53
47
  debugMode?: boolean;
54
48
  fromAddressFilter?: string | string[];
55
- transferType?: 'in' | 'out' | 'all';
49
+ transferType?: "in" | "out" | "all";
56
50
  tokens?: string | string[];
57
51
  token?: string;
58
52
  };
59
- export declare function getLogs({ chain, topic, topics, fromBlock, toBlock, all, limit, offset, target, targets, eventAbi, entireLog, flatten, extraTopics, fromTimestamp, toTimestamp, debugMode, noTarget, parseLog, }: IndexerGetLogsOptions): Promise<any[]>;
53
+ export declare function getTokens(address: string | string[], { onlyWhitelisted, skipCacheRead, skipCache, chain, tokenType, }?: {
54
+ onlyWhitelisted?: boolean;
55
+ skipCacheRead?: boolean;
56
+ skipCache?: boolean;
57
+ chain?: string;
58
+ tokenType?: TokenTypes;
59
+ }): Promise<{
60
+ [chain: string]: string[];
61
+ }>;
62
+ export declare function getLogs(options: IndexerGetLogsOptions): Promise<any[]>;
63
+ export declare function getTokenTransfers({ chain, fromAddressFilter, fromBlock, toBlock, all, limit, offset, target, targets, flatten, fromTimestamp, toTimestamp, debugMode, transferType, token, tokens, }: IndexerGetTokenTransfersOptions): Promise<any[]>;
64
+ export declare function getTransactions({ chain, addresses, transaction_hashes, from_block, to_block, all, limit, offset, debugMode, transactionType, }: GetTransactionOptions): Promise<any>;
60
65
  export declare function isIndexerEnabled(chain?: string): boolean;
61
66
  export declare function isIndexer2Enabled(chain?: string): boolean;
62
- export declare function getTokenTransfers({ chain, fromAddressFilter, fromBlock, toBlock, all, limit, offset, target, targets, flatten, fromTimestamp, toTimestamp, debugMode, transferType, token, tokens, }: IndexerGetTokenTransfersOptions): Promise<any[]>;
63
- export declare function getTransactions({ chain, addresses, transaction_hashes, from_block, to_block, all, limit, offset, debugMode, transactionType }: GetTransactionOptions): Promise<any>;
64
67
  export {};