@defillama/sdk 5.0.3 → 5.0.5

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.
@@ -2,6 +2,7 @@ import { Block, CallOptions, MulticallOptions, FetchListOptions, ByteCodeCallOpt
2
2
  import Balances from "./Balances";
3
3
  import { Chain } from "./general";
4
4
  import { Provider } from "ethers";
5
+ import { GetLogsOptions } from "./util/logs";
5
6
  export declare class ChainApi {
6
7
  block?: Block;
7
8
  chain: Chain | string;
@@ -55,5 +56,6 @@ export declare class ChainApi {
55
56
  usdTokenBalances: BalancesV1;
56
57
  rawTokenBalances: BalancesV1;
57
58
  }>;
59
+ getLogs(params: GetLogsOptions): Promise<any[] | import("ethers").EventLog[] | import("ethers").EventLog[][]>;
58
60
  }
59
61
  export default ChainApi;
package/build/ChainApi.js CHANGED
@@ -60,6 +60,7 @@ var debugLog_1 = require("./util/debugLog");
60
60
  var generalUtil_1 = require("./generalUtil");
61
61
  var multicall3_1 = require("./abi/multicall3");
62
62
  var eth_1 = require("./eth");
63
+ var logs_1 = __importDefault(require("./util/logs"));
63
64
  var nullAddress = '0x0000000000000000000000000000000000000000';
64
65
  var ChainApi = /** @class */ (function () {
65
66
  function ChainApi(params) {
@@ -257,6 +258,14 @@ var ChainApi = /** @class */ (function () {
257
258
  });
258
259
  });
259
260
  };
261
+ ChainApi.prototype.getLogs = function (params) {
262
+ return __awaiter(this, void 0, void 0, function () {
263
+ return __generator(this, function (_a) {
264
+ params.chain = this.chain;
265
+ return [2 /*return*/, (0, logs_1.default)(params)];
266
+ });
267
+ });
268
+ };
260
269
  return ChainApi;
261
270
  }());
262
271
  exports.ChainApi = ChainApi;
package/build/general.js CHANGED
@@ -20,7 +20,7 @@ function createProvider(name, rpcString, chainId) {
20
20
  }
21
21
  else {
22
22
  try {
23
- return new ethers_1.ethers.FallbackProvider(rpcList.map(function (url, i) { return ({ provider: getProviderObject(url), priority: i, chainId: chainId, }); }), networkish, { cacheTimeout: 5 * 1000 });
23
+ return new ethers_1.ethers.FallbackProvider(rpcList.map(function (url, i) { return ({ provider: getProviderObject(url), priority: i, chainId: chainId, }); }), networkish, { cacheTimeout: 5 * 1000, quorum: 1, eventWorkers: 1, eventQuorum: 1, });
24
24
  }
25
25
  catch (e) {
26
26
  // debugLog(`Error creating provider for ${name} with RPCs: ${rpcList.join(', ')}`)
@@ -218,7 +218,7 @@ function getProviderUrl(provider) {
218
218
  }
219
219
  exports.getProviderUrl = getProviderUrl;
220
220
  function formErrorString(e, errorParams) {
221
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
221
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
222
222
  if (errorParams === void 0) { errorParams = {}; }
223
223
  if (!e) {
224
224
  if (errorParams.promisePoolErrors) {
@@ -242,14 +242,14 @@ function formErrorString(e, errorParams) {
242
242
  var errorString = e.toString();
243
243
  if (typeof e !== 'object')
244
244
  return errorString;
245
- if (((e.reason || e.method) && e.code) || errorParams.result === '0x') { // ethers.js error
246
- var method = (_d = e.method) !== null && _d !== void 0 ? _d : errorParams.abi;
245
+ if (((e.reason || e.method || e.shortMessage) && e.code) || errorParams.result === '0x') { // ethers.js error
246
+ var method = (_g = (_d = e.method) !== null && _d !== void 0 ? _d : (_f = (_e = e.info) === null || _e === void 0 ? void 0 : _e.request) === null || _f === void 0 ? void 0 : _f.method) !== null && _g !== void 0 ? _g : errorParams.abi;
247
247
  if (e.body) {
248
248
  try {
249
249
  e.body = JSON.parse(e.body);
250
250
  }
251
251
  catch (e) { }
252
- if ((_f = (_e = e.body) === null || _e === void 0 ? void 0 : _e.error) === null || _f === void 0 ? void 0 : _f.message)
252
+ if ((_j = (_h = e.body) === null || _h === void 0 ? void 0 : _h.error) === null || _j === void 0 ? void 0 : _j.message)
253
253
  return e.body.error.message;
254
254
  }
255
255
  if (!method && e.requestBody) {
@@ -257,11 +257,11 @@ function formErrorString(e, errorParams) {
257
257
  e.requestBody = JSON.parse(e.requestBody);
258
258
  }
259
259
  catch (e) { }
260
- method = (_g = e.requestBody) === null || _g === void 0 ? void 0 : _g.method;
260
+ method = (_k = e.requestBody) === null || _k === void 0 ? void 0 : _k.method;
261
261
  }
262
- if (!e.provider && errorParams.provider)
262
+ else if (!e.provider && errorParams.provider)
263
263
  e.provider = errorParams.provider;
264
- var providerUrl = getProviderUrl((_h = e.provider) !== null && _h !== void 0 ? _h : errorParams.provider);
264
+ var providerUrl = getProviderUrl((_l = e.provider) !== null && _l !== void 0 ? _l : errorParams.provider);
265
265
  if (e.serverError)
266
266
  return "host: ".concat(providerUrl, " ").concat(e.serverError.toString());
267
267
  if (e.results) {
@@ -275,7 +275,13 @@ function formErrorString(e, errorParams) {
275
275
  var extraInfo = 'target: ' + errorParams.target;
276
276
  if (errorParams.params && errorParams.params.length)
277
277
  extraInfo += shortenString(' params: ' + errorParams.params.join(', '));
278
- return "Failed to call ".concat(method, " ").concat(extraInfo, " on chain: [").concat(errorParams.chain, "] rpc: ").concat(providerUrl, " call reverted ").concat((_j = e.errorName) !== null && _j !== void 0 ? _j : '', " ").concat((_k = e.errorArgs) !== null && _k !== void 0 ? _k : '');
278
+ return "Failed to call: ".concat(method, " ").concat(extraInfo, " on chain: [").concat(errorParams.chain, "] rpc: ").concat(providerUrl, " call reverted, reason: ").concat((_o = (_m = e.errorName) !== null && _m !== void 0 ? _m : e.shortMessage) !== null && _o !== void 0 ? _o : '', " ").concat((_p = e.errorArgs) !== null && _p !== void 0 ? _p : '');
279
+ }
280
+ else if (e.code === 'BAD_DATA') {
281
+ if (!method)
282
+ method = (_r = (_q = e === null || e === void 0 ? void 0 : e.info) === null || _q === void 0 ? void 0 : _q.payload) === null || _r === void 0 ? void 0 : _r.method;
283
+ var extraInfo = 'target: ' + errorParams.target;
284
+ return "Failed method: ".concat(method, " ").concat(extraInfo, " on chain: [").concat(errorParams.chain, "] rpc: ").concat(providerUrl, " reason: ").concat((_u = (_t = (_s = e.errorName) !== null && _s !== void 0 ? _s : e.shortMessage) !== null && _t !== void 0 ? _t : e.message) !== null && _u !== void 0 ? _u : '', " ").concat((_v = e.errorArgs) !== null && _v !== void 0 ? _v : '');
279
285
  }
280
286
  if (method === 'eth_blockNumber')
281
287
  return "host: ".concat(providerUrl, " reason: ").concat(e.reason, " code: ").concat(e.code);
@@ -293,7 +299,8 @@ function shortenString(str, length) {
293
299
  function formError(e, errorParams) {
294
300
  var _a;
295
301
  if (errorParams === void 0) { errorParams = {}; }
296
- if ((e === null || e === void 0 ? void 0 : e._isCustomError) || (!Object.keys(errorParams).length && typeof e === 'object' && !Object.keys(e).length))
302
+ var errorParamsSet = Object.keys(errorParams).length;
303
+ if ((e === null || e === void 0 ? void 0 : e._isCustomError) || (!errorParamsSet && typeof e === 'object' && !Object.keys(e).length))
297
304
  return e; // already formatted or vannila error
298
305
  var error = new Error(formErrorString(e, errorParams));
299
306
  try {
@@ -304,6 +311,7 @@ function formError(e, errorParams) {
304
311
  }
305
312
  catch (e) { }
306
313
  error._isCustomError = true;
314
+ error.message = shortenString(error.message, 420);
307
315
  error.stack = '';
308
316
  return error;
309
317
  }
package/build/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { Balances } from './Balances';
4
4
  export { getProvider, setProvider } from './general';
5
5
  export * as util from "./generalUtil";
6
6
  export * as cache from "./util/cache";
7
+ export { getLogs as getEventLogs } from "./util/logs";
7
8
  export * as sdkCache from "./util/internal-cache";
8
9
  export * as api from "./api";
9
10
  export * as api2 from "./api2";
package/build/index.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.Balances = exports.ChainApi = void 0;
26
+ exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.getEventLogs = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.Balances = exports.ChainApi = void 0;
27
27
  var debugLog_1 = require("./util/debugLog");
28
28
  var ChainApi_1 = require("./ChainApi");
29
29
  Object.defineProperty(exports, "ChainApi", { enumerable: true, get: function () { return ChainApi_1.ChainApi; } });
@@ -34,6 +34,8 @@ Object.defineProperty(exports, "getProvider", { enumerable: true, get: function
34
34
  Object.defineProperty(exports, "setProvider", { enumerable: true, get: function () { return general_1.setProvider; } });
35
35
  exports.util = __importStar(require("./generalUtil"));
36
36
  exports.cache = __importStar(require("./util/cache"));
37
+ var logs_1 = require("./util/logs");
38
+ Object.defineProperty(exports, "getEventLogs", { enumerable: true, get: function () { return logs_1.getLogs; } });
37
39
  exports.sdkCache = __importStar(require("./util/internal-cache"));
38
40
  exports.api = __importStar(require("./api"));
39
41
  exports.api2 = __importStar(require("./api2"));
@@ -17,4 +17,11 @@ export declare function getTempLocalCache({ file, defaultData, clearAfter }: {
17
17
  defaultData?: any;
18
18
  clearAfter?: number;
19
19
  }): any;
20
+ export declare function deleteCache(file: string): Promise<void>;
21
+ export declare function readExpiringJsonCache(file: string): Promise<any>;
22
+ export declare function writeExpiringJsonCache(file: string, data: any, { expireAfter, // cache for 1 day by default
23
+ expiryTimestamp, }: {
24
+ expireAfter?: number;
25
+ expiryTimestamp?: number;
26
+ }): Promise<void>;
20
27
  export {};
@@ -37,7 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  var _a;
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.getTempLocalCache = exports.compressCache = exports.parseCache = exports.writeCache = exports.readCache = exports.currentVersion = void 0;
40
+ exports.writeExpiringJsonCache = exports.readExpiringJsonCache = exports.deleteCache = exports.getTempLocalCache = exports.compressCache = exports.parseCache = exports.writeCache = exports.readCache = exports.currentVersion = void 0;
41
41
  var debugLog_1 = require("./debugLog");
42
42
  var r2_1 = require("./r2");
43
43
  var zlib_1 = require("zlib");
@@ -273,3 +273,65 @@ function getTempLocalCache(_a) {
273
273
  }
274
274
  }
275
275
  exports.getTempLocalCache = getTempLocalCache;
276
+ // this removes data only from the file cache, not from the R2 cache
277
+ function deleteCache(file) {
278
+ return __awaiter(this, void 0, void 0, function () {
279
+ var filePath;
280
+ return __generator(this, function (_a) {
281
+ switch (_a.label) {
282
+ case 0:
283
+ filePath = getFilePath(file);
284
+ return [4 /*yield*/, fs.unlink(filePath)];
285
+ case 1:
286
+ _a.sent();
287
+ return [2 /*return*/];
288
+ }
289
+ });
290
+ });
291
+ }
292
+ exports.deleteCache = deleteCache;
293
+ function readExpiringJsonCache(file) {
294
+ return __awaiter(this, void 0, void 0, function () {
295
+ var options, data;
296
+ return __generator(this, function (_a) {
297
+ switch (_a.label) {
298
+ case 0:
299
+ options = { readFromR2Cache: false, skipR2Cache: true };
300
+ file = 'expiring/' + file;
301
+ return [4 /*yield*/, readCache(file, options)];
302
+ case 1:
303
+ data = _a.sent();
304
+ if (!data || Object.keys(data).length === 0)
305
+ return [2 /*return*/, null];
306
+ if (!(data.expiryTimestamp < (Date.now() / 1e3))) return [3 /*break*/, 3];
307
+ return [4 /*yield*/, deleteCache(file)];
308
+ case 2:
309
+ _a.sent();
310
+ return [2 /*return*/, null];
311
+ case 3: return [2 /*return*/, data.data];
312
+ }
313
+ });
314
+ });
315
+ }
316
+ exports.readExpiringJsonCache = readExpiringJsonCache;
317
+ function writeExpiringJsonCache(file, data, _a) {
318
+ var _b = _a.expireAfter, expireAfter = _b === void 0 ? 60 * 60 * 24 : _b, // cache for 1 day by default
319
+ expiryTimestamp = _a.expiryTimestamp;
320
+ return __awaiter(this, void 0, void 0, function () {
321
+ var options;
322
+ return __generator(this, function (_c) {
323
+ switch (_c.label) {
324
+ case 0:
325
+ file = 'expiring/' + file;
326
+ if (!expiryTimestamp)
327
+ expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
328
+ options = { skipR2CacheWrite: true };
329
+ return [4 /*yield*/, writeCache(file, { data: data, expiryTimestamp: expiryTimestamp }, options)];
330
+ case 1:
331
+ _c.sent();
332
+ return [2 /*return*/];
333
+ }
334
+ });
335
+ });
336
+ }
337
+ exports.writeExpiringJsonCache = writeExpiringJsonCache;
@@ -1,6 +1,6 @@
1
1
  import { Chain } from "../general";
2
2
  import type { Address } from "../types";
3
- import { ethers } from "ethers";
3
+ import { EventLog } from "ethers";
4
4
  import runInPromisePoolOrig from "./promisePool";
5
5
  export { getLatestBlock, getTimestamp, lookupBlock, } from "./blocks";
6
6
  export declare const runInPromisePool: typeof runInPromisePoolOrig;
@@ -11,12 +11,10 @@ export declare function getLogs(params: {
11
11
  keys: string[];
12
12
  fromBlock: number;
13
13
  toBlock: number;
14
- topics?: string[];
14
+ topics?: (string | null)[];
15
15
  chain?: Chain;
16
16
  }): Promise<{
17
- output: (readonly string[])[];
18
- } | {
19
- output: ethers.Log[];
17
+ output: EventLog[];
20
18
  }>;
21
19
  export declare function normalizeAddress(address: string): string;
22
20
  export declare function normalizePrefixes(address: string): string;
@@ -75,7 +75,7 @@ exports.sliceIntoChunks = sliceIntoChunks;
75
75
  function getLogs(params) {
76
76
  var _a;
77
77
  return __awaiter(this, void 0, void 0, function () {
78
- var filter, logs, blockSpread, currentBlock, nextBlock, logParams, partLogs, e_1, error;
78
+ var filter, logs, blockSpread, currentBlock, provider, nextBlock, logParams, partLogs, e_1, error;
79
79
  return __generator(this, function (_b) {
80
80
  switch (_b.label) {
81
81
  case 0:
@@ -91,6 +91,7 @@ function getLogs(params) {
91
91
  logs = [];
92
92
  blockSpread = params.toBlock - params.fromBlock;
93
93
  currentBlock = params.fromBlock;
94
+ provider = (0, general_1.getProvider)(params.chain, true);
94
95
  _b.label = 1;
95
96
  case 1:
96
97
  if (!(currentBlock < params.toBlock)) return [3 /*break*/, 6];
@@ -99,7 +100,7 @@ function getLogs(params) {
99
100
  _b.label = 2;
100
101
  case 2:
101
102
  _b.trys.push([2, 4, , 5]);
102
- return [4 /*yield*/, (0, general_1.getProvider)(params.chain, true).getLogs(logParams)];
103
+ return [4 /*yield*/, provider.getLogs(logParams)];
103
104
  case 3:
104
105
  partLogs = _b.sent();
105
106
  logs = logs.concat(partLogs);
@@ -107,14 +108,14 @@ function getLogs(params) {
107
108
  return [3 /*break*/, 5];
108
109
  case 4:
109
110
  e_1 = _b.sent();
110
- (0, debugLog_1.debugLog)("Error fetching logs for chain ".concat(params.chain, " blockSpread: ").concat(blockSpread, ". ").concat((0, generalUtil_1.formError)(e_1)));
111
+ (0, debugLog_1.debugLog)("Error fetching logs for chain ".concat(params.chain, " blockSpread: ").concat(blockSpread, ". ").concat((0, generalUtil_1.formError)(e_1, { chain: params.chain, target: params.target, provider: provider })));
111
112
  if (blockSpread >= 2e3) {
112
113
  // We got too many results
113
114
  // We could chop it up into 2K block spreads as that is guaranteed to always return but then we'll have to make a lot of queries (easily >1000), so instead we'll keep dividing the block spread by two until we make it
114
115
  blockSpread = Math.floor(blockSpread / 2);
115
116
  }
116
117
  else {
117
- error = (0, generalUtil_1.formError)(e_1);
118
+ error = (0, generalUtil_1.formError)(e_1, { chain: params.chain, target: params.target, provider: provider });
118
119
  error.message = "[chain: ".concat(params.chain, "] ").concat(error === null || error === void 0 ? void 0 : error.message, " params: ").concat(JSON.stringify(logParams));
119
120
  throw e_1;
120
121
  }
@@ -129,8 +130,11 @@ function getLogs(params) {
129
130
  output: logs.map(function (log) { return log.topics; })
130
131
  }];
131
132
  }
132
- // ethers v5 logs had this but not ethers v6, so adding field to keep it compatible
133
- logs.forEach(function (log) { var _a; return log.logIndex = (_a = log.logIndex) !== null && _a !== void 0 ? _a : log.index; });
133
+ // ethers v5 logs had 'logIndex' but not ethers v6, so adding field to keep it compatible
134
+ logs.forEach(function (log) {
135
+ log.logIndex = log.index;
136
+ delete log.provider;
137
+ });
134
138
  return [2 /*return*/, {
135
139
  output: logs
136
140
  }];
@@ -0,0 +1,32 @@
1
+ import { EventLog } from "ethers";
2
+ import { Address } from "../types";
3
+ export type GetLogsOptions = {
4
+ target?: Address;
5
+ topic?: string;
6
+ keys?: string[];
7
+ fromBlock?: number;
8
+ toBlock?: number;
9
+ topics?: (string | null)[];
10
+ extraTopics?: string[];
11
+ chain?: string;
12
+ eventAbi?: string | any;
13
+ fromTimestamp?: number;
14
+ toTimestamp?: number;
15
+ skipCache?: boolean;
16
+ skipCacheRead?: boolean;
17
+ entireLog?: boolean;
18
+ cacheInCloud?: boolean;
19
+ onlyArgs?: boolean;
20
+ targets?: Address[];
21
+ flatten?: boolean;
22
+ };
23
+ export declare function getLogs(options: GetLogsOptions): Promise<EventLog[] | EventLog[][] | any[]>;
24
+ export type logCache = {
25
+ logs: EventLog[];
26
+ metadata?: {
27
+ version: string;
28
+ fromBlock: number;
29
+ toBlock: number;
30
+ };
31
+ };
32
+ export default getLogs;
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.getLogs = void 0;
54
+ var _1 = require(".");
55
+ var ethers_1 = require("ethers");
56
+ var blocks_1 = require("./blocks");
57
+ var cache_1 = require("./cache");
58
+ var debugLog_1 = require("./debugLog");
59
+ var p_limit_1 = __importDefault(require("p-limit"));
60
+ var currentVersion = 'v2';
61
+ function getLogs(options) {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ function addLogsToCache(fromBlock, toBlock) {
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var metadata, logs, logIndices;
66
+ var _a;
67
+ return __generator(this, function (_b) {
68
+ switch (_b.label) {
69
+ case 0:
70
+ metadata = cacheData.metadata;
71
+ return [4 /*yield*/, (0, _1.getLogs)({
72
+ chain: chain,
73
+ target: target, topic: topic,
74
+ keys: keys,
75
+ topics: topics,
76
+ fromBlock: fromBlock,
77
+ toBlock: toBlock,
78
+ })];
79
+ case 1:
80
+ logs = (_b.sent()).output;
81
+ (_a = cacheData.logs).push.apply(_a, logs);
82
+ logIndices = new Set();
83
+ cacheData.logs = cacheData.logs.filter(function (i) {
84
+ var _a;
85
+ var key = i.transactionHash + ((_a = i.logIndex) !== null && _a !== void 0 ? _a : i.index); // ethers v5 had logIndex, ethers v6 has index
86
+ if (!(i.hasOwnProperty('logIndex') || i.hasOwnProperty('index')) || !i.hasOwnProperty('transactionHash')) {
87
+ (0, debugLog_1.debugLog)(i, i.logIndex, i.index, i.transactionHash);
88
+ throw new Error('Missing crucial field');
89
+ }
90
+ if (logIndices.has(key))
91
+ return false;
92
+ logIndices.add(key);
93
+ return true;
94
+ });
95
+ if (!metadata.fromBlock || fromBlock < metadata.fromBlock)
96
+ metadata.fromBlock = fromBlock;
97
+ if (!metadata.toBlock || toBlock > metadata.toBlock)
98
+ metadata.toBlock = toBlock;
99
+ if (!!skipCache) return [3 /*break*/, 3];
100
+ return [4 /*yield*/, (0, cache_1.writeCache)(getFile(), cacheData, { skipR2CacheWrite: !cacheInCloud })];
101
+ case 2:
102
+ _b.sent();
103
+ _b.label = 3;
104
+ case 3: return [2 /*return*/];
105
+ }
106
+ });
107
+ });
108
+ }
109
+ function _getCache() {
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ var key, defaultRes, cache;
112
+ return __generator(this, function (_a) {
113
+ switch (_a.label) {
114
+ case 0:
115
+ key = getFile();
116
+ defaultRes = {
117
+ logs: [],
118
+ };
119
+ if (skipCache || skipCacheRead)
120
+ return [2 /*return*/, defaultRes];
121
+ return [4 /*yield*/, (0, cache_1.readCache)(key, { skipR2Cache: !cacheInCloud })];
122
+ case 1:
123
+ cache = _a.sent();
124
+ if (!cache.metadata || cache.metadata.version !== currentVersion)
125
+ return [2 /*return*/, defaultRes];
126
+ return [2 /*return*/, cache];
127
+ }
128
+ });
129
+ });
130
+ }
131
+ function getFile() {
132
+ var extraKey = topics === null || topics === void 0 ? void 0 : topics.join('-').toLowerCase();
133
+ if (!extraKey)
134
+ throw new Error('extraKey is required');
135
+ if (keys.length)
136
+ extraKey += '-' + keys.join('-').toLowerCase();
137
+ return "event-logs/".concat(chain, "/").concat(target.toLowerCase(), "-").concat(extraKey);
138
+ }
139
+ // we need to form filter topic with indexed keyword, else it messes up generated topic string
140
+ function toFilterTopic(topic) {
141
+ if (typeof topic === 'string') {
142
+ if (topic.startsWith('0x'))
143
+ return topic;
144
+ topic = new ethers_1.Interface([topic]);
145
+ }
146
+ var fragment = topic.fragments[0];
147
+ return (0, ethers_1.id)("".concat(fragment.name, "(").concat(fragment.inputs.map(function (i) { return i.type; }).join(','), ")"));
148
+ }
149
+ var target, _a, chain, fromBlock, toBlock, fromTimestamp, toTimestamp, eventAbi, topic, topics, extraTopics, _b, skipCache, _c, skipCacheRead, _d, entireLog, _e, cacheInCloud, _f, onlyArgs, _g, keys, targets, _h, flatten, limiter, newOptions_1, res, iface, cacheData, isFirstRun, metadata_1, logs;
150
+ return __generator(this, function (_j) {
151
+ switch (_j.label) {
152
+ case 0:
153
+ target = options.target, _a = options.chain, chain = _a === void 0 ? 'ethereum' : _a, fromBlock = options.fromBlock, toBlock = options.toBlock, fromTimestamp = options.fromTimestamp, toTimestamp = options.toTimestamp, eventAbi = options.eventAbi, topic = options.topic, topics = options.topics, extraTopics = options.extraTopics, _b = options.skipCache, skipCache = _b === void 0 ? false : _b, _c = options.skipCacheRead, skipCacheRead = _c === void 0 ? false : _c, _d = options.entireLog, entireLog = _d === void 0 ? false : _d, _e = options.cacheInCloud, cacheInCloud = _e === void 0 ? false : _e, _f = options.onlyArgs, onlyArgs = _f === void 0 ? false : _f, _g = options.keys, keys = _g === void 0 ? [] : _g, targets = options.targets, _h = options.flatten, flatten = _h === void 0 ? true : _h;
154
+ if (!target && !(targets === null || targets === void 0 ? void 0 : targets.length))
155
+ throw new Error('target|targets is required');
156
+ if (!fromBlock && !fromTimestamp)
157
+ throw new Error('fromBlock or fromTimestamp is required');
158
+ if (!toBlock && !toTimestamp)
159
+ throw new Error('toBlock or toTimestamp is required');
160
+ limiter = getChainLimiter(chain);
161
+ if (!!fromBlock) return [3 /*break*/, 2];
162
+ return [4 /*yield*/, limiter(function () { return (0, blocks_1.getBlock)(chain, fromTimestamp); })];
163
+ case 1:
164
+ fromBlock = (_j.sent()).block;
165
+ _j.label = 2;
166
+ case 2:
167
+ if (!!toBlock) return [3 /*break*/, 4];
168
+ return [4 /*yield*/, limiter(function () { return (0, blocks_1.getBlock)(chain, toTimestamp); })];
169
+ case 3:
170
+ toBlock = (_j.sent()).block;
171
+ _j.label = 4;
172
+ case 4:
173
+ if (!(targets === null || targets === void 0 ? void 0 : targets.length)) return [3 /*break*/, 6];
174
+ newOptions_1 = __assign(__assign({}, options), { fromBlock: fromBlock, toBlock: toBlock });
175
+ delete newOptions_1.targets;
176
+ return [4 /*yield*/, Promise.all(targets.map(function (i) { return getLogs(__assign(__assign({}, newOptions_1), { target: i })); }))];
177
+ case 5:
178
+ res = _j.sent();
179
+ if (flatten)
180
+ return [2 /*return*/, res.flat()];
181
+ return [2 /*return*/, res];
182
+ case 6:
183
+ if (eventAbi)
184
+ iface = new ethers_1.Interface([eventAbi]);
185
+ if ((!topics || !topics.length)) {
186
+ if (topic)
187
+ topic = toFilterTopic(topic);
188
+ else if (eventAbi)
189
+ topic = toFilterTopic(iface);
190
+ else {
191
+ throw new Error('eventAbi | topic | topics are required');
192
+ }
193
+ topics = [topic];
194
+ if (extraTopics)
195
+ topics.push.apply(topics, extraTopics);
196
+ }
197
+ return [4 /*yield*/, _getCache()];
198
+ case 7:
199
+ cacheData = _j.sent();
200
+ isFirstRun = !cacheData.metadata;
201
+ if (isFirstRun)
202
+ cacheData.metadata = { version: currentVersion };
203
+ if (!isFirstRun) return [3 /*break*/, 9];
204
+ return [4 /*yield*/, limiter(function () { return addLogsToCache(fromBlock, toBlock); })];
205
+ case 8:
206
+ _j.sent();
207
+ return [3 /*break*/, 13];
208
+ case 9:
209
+ metadata_1 = cacheData.metadata;
210
+ if (!(fromBlock < metadata_1.fromBlock)) return [3 /*break*/, 11];
211
+ return [4 /*yield*/, limiter(function () { return addLogsToCache(fromBlock, metadata_1.fromBlock); })];
212
+ case 10:
213
+ _j.sent();
214
+ _j.label = 11;
215
+ case 11:
216
+ if (!(toBlock > metadata_1.toBlock)) return [3 /*break*/, 13];
217
+ return [4 /*yield*/, limiter(function () { return addLogsToCache(metadata_1.toBlock, toBlock); })];
218
+ case 12:
219
+ _j.sent();
220
+ _j.label = 13;
221
+ case 13:
222
+ logs = cacheData.logs.filter(function (i) {
223
+ if (i.blockNumber < fromBlock)
224
+ return false;
225
+ if (i.blockNumber > toBlock)
226
+ return false;
227
+ return true;
228
+ });
229
+ if (!eventAbi || entireLog)
230
+ return [2 /*return*/, logs];
231
+ return [2 /*return*/, logs.map(function (i) { return iface.parseLog(i); }).map(function (i) { return onlyArgs ? i.args : i; })];
232
+ }
233
+ });
234
+ });
235
+ }
236
+ exports.getLogs = getLogs;
237
+ var chainLimiters = {};
238
+ function getChainLimiter(chain) {
239
+ if (!chainLimiters[chain])
240
+ chainLimiters[chain] = createLimiter(chain);
241
+ return chainLimiters[chain];
242
+ function createLimiter(chain) {
243
+ var _a;
244
+ var defaultLimit = chain === 'fantom' ? 10 : 20;
245
+ var limit = +((_a = process.env["".concat(chain.toUpperCase(), "_RPC_GET_LOGS_CONCURRENCY_LIMIT")]) !== null && _a !== void 0 ? _a : defaultLimit);
246
+ return (0, p_limit_1.default)(limit);
247
+ }
248
+ }
249
+ exports.default = getLogs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "5.0.3",
3
+ "version": "5.0.5",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {