@defillama/sdk 5.0.4 → 5.0.6

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.
@@ -257,7 +257,10 @@ function getTempLocalCache(_a) {
257
257
  removePromisesAndFunctions(fileData);
258
258
  _fs.writeFileSync(filePath, JSON.stringify(fileData));
259
259
  }
260
- catch (error) { }
260
+ catch (error) {
261
+ (0, debugLog_1.debugLog)('Error saving cache:', error);
262
+ }
263
+ process.exit(0);
261
264
  }
262
265
  function removePromisesAndFunctions(data) {
263
266
  if (typeof data === 'object') {
@@ -3,6 +3,7 @@ import type { Address } from "../types";
3
3
  import { EventLog } from "ethers";
4
4
  import runInPromisePoolOrig from "./promisePool";
5
5
  export { getLatestBlock, getTimestamp, lookupBlock, } from "./blocks";
6
+ export { convertToBigInt } from "../generalUtil";
6
7
  export declare const runInPromisePool: typeof runInPromisePoolOrig;
7
8
  export declare function sliceIntoChunks(arr: any[], chunkSize?: number): any[][];
8
9
  export declare function getLogs(params: {
@@ -11,7 +12,7 @@ export declare function getLogs(params: {
11
12
  keys: string[];
12
13
  fromBlock: number;
13
14
  toBlock: number;
14
- topics?: string[];
15
+ topics?: (string | null)[];
15
16
  chain?: Chain;
16
17
  }): Promise<{
17
18
  output: EventLog[];
@@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
53
+ exports.normalizeBalances = exports.normalizePrefixes = exports.normalizeAddress = exports.getLogs = exports.sliceIntoChunks = exports.runInPromisePool = exports.convertToBigInt = exports.lookupBlock = exports.getTimestamp = exports.getLatestBlock = void 0;
54
54
  var general_1 = require("../general");
55
55
  var ethers_1 = require("ethers");
56
56
  var generalUtil_1 = require("../generalUtil");
@@ -60,6 +60,8 @@ var blocks_1 = require("./blocks");
60
60
  Object.defineProperty(exports, "getLatestBlock", { enumerable: true, get: function () { return blocks_1.getLatestBlock; } });
61
61
  Object.defineProperty(exports, "getTimestamp", { enumerable: true, get: function () { return blocks_1.getTimestamp; } });
62
62
  Object.defineProperty(exports, "lookupBlock", { enumerable: true, get: function () { return blocks_1.lookupBlock; } });
63
+ var generalUtil_2 = require("../generalUtil");
64
+ Object.defineProperty(exports, "convertToBigInt", { enumerable: true, get: function () { return generalUtil_2.convertToBigInt; } });
63
65
  exports.runInPromisePool = promisePool_1.default;
64
66
  function sliceIntoChunks(arr, chunkSize) {
65
67
  if (chunkSize === void 0) { chunkSize = 100; }
@@ -75,7 +77,7 @@ exports.sliceIntoChunks = sliceIntoChunks;
75
77
  function getLogs(params) {
76
78
  var _a;
77
79
  return __awaiter(this, void 0, void 0, function () {
78
- var filter, logs, blockSpread, currentBlock, nextBlock, logParams, partLogs, e_1, error;
80
+ var filter, logs, blockSpread, currentBlock, provider, nextBlock, logParams, partLogs, e_1, error;
79
81
  return __generator(this, function (_b) {
80
82
  switch (_b.label) {
81
83
  case 0:
@@ -91,6 +93,7 @@ function getLogs(params) {
91
93
  logs = [];
92
94
  blockSpread = params.toBlock - params.fromBlock;
93
95
  currentBlock = params.fromBlock;
96
+ provider = (0, general_1.getProvider)(params.chain, true);
94
97
  _b.label = 1;
95
98
  case 1:
96
99
  if (!(currentBlock < params.toBlock)) return [3 /*break*/, 6];
@@ -99,7 +102,7 @@ function getLogs(params) {
99
102
  _b.label = 2;
100
103
  case 2:
101
104
  _b.trys.push([2, 4, , 5]);
102
- return [4 /*yield*/, (0, general_1.getProvider)(params.chain, true).getLogs(logParams)];
105
+ return [4 /*yield*/, provider.getLogs(logParams)];
103
106
  case 3:
104
107
  partLogs = _b.sent();
105
108
  logs = logs.concat(partLogs);
@@ -107,14 +110,14 @@ function getLogs(params) {
107
110
  return [3 /*break*/, 5];
108
111
  case 4:
109
112
  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)));
113
+ (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
114
  if (blockSpread >= 2e3) {
112
115
  // We got too many results
113
116
  // 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
117
  blockSpread = Math.floor(blockSpread / 2);
115
118
  }
116
119
  else {
117
- error = (0, generalUtil_1.formError)(e_1);
120
+ error = (0, generalUtil_1.formError)(e_1, { chain: params.chain, target: params.target, provider: provider });
118
121
  error.message = "[chain: ".concat(params.chain, "] ").concat(error === null || error === void 0 ? void 0 : error.message, " params: ").concat(JSON.stringify(logParams));
119
122
  throw e_1;
120
123
  }
@@ -129,8 +132,11 @@ function getLogs(params) {
129
132
  output: logs.map(function (log) { return log.topics; })
130
133
  }];
131
134
  }
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; });
135
+ // ethers v5 logs had 'logIndex' but not ethers v6, so adding field to keep it compatible
136
+ logs.forEach(function (log) {
137
+ log.logIndex = log.index;
138
+ delete log.provider;
139
+ });
134
140
  return [2 /*return*/, {
135
141
  output: logs
136
142
  }];
@@ -1,12 +1,12 @@
1
1
  import { EventLog } from "ethers";
2
2
  import { Address } from "../types";
3
3
  export type GetLogsOptions = {
4
- target: Address;
4
+ target?: Address;
5
5
  topic?: string;
6
6
  keys?: string[];
7
7
  fromBlock?: number;
8
8
  toBlock?: number;
9
- topics?: string[];
9
+ topics?: (string | null)[];
10
10
  extraTopics?: string[];
11
11
  chain?: string;
12
12
  eventAbi?: string | any;
@@ -17,9 +17,10 @@ export type GetLogsOptions = {
17
17
  entireLog?: boolean;
18
18
  cacheInCloud?: boolean;
19
19
  onlyArgs?: boolean;
20
+ targets?: Address[];
21
+ flatten?: boolean;
20
22
  };
21
- export declare function getLogs({ target, chain, fromBlock, toBlock, fromTimestamp, toTimestamp, eventAbi, topic, topics, extraTopics, // can be passed as input as extra filter arguments
22
- skipCache, skipCacheRead, entireLog, cacheInCloud, onlyArgs, keys, }: GetLogsOptions): Promise<EventLog[] | any[]>;
23
+ export declare function getLogs(options: GetLogsOptions): Promise<EventLog[] | EventLog[][] | any[]>;
23
24
  export type logCache = {
24
25
  logs: EventLog[];
25
26
  metadata?: {
@@ -1,4 +1,15 @@
1
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
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,6 +46,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
47
  }
37
48
  };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
38
52
  Object.defineProperty(exports, "__esModule", { value: true });
39
53
  exports.getLogs = void 0;
40
54
  var _1 = require(".");
@@ -42,11 +56,9 @@ var ethers_1 = require("ethers");
42
56
  var blocks_1 = require("./blocks");
43
57
  var cache_1 = require("./cache");
44
58
  var debugLog_1 = require("./debugLog");
59
+ var p_limit_1 = __importDefault(require("p-limit"));
45
60
  var currentVersion = 'v2';
46
- function getLogs(_a) {
47
- var target = _a.target, _b = _a.chain, chain = _b === void 0 ? 'ethereum' : _b, fromBlock = _a.fromBlock, toBlock = _a.toBlock, fromTimestamp = _a.fromTimestamp, toTimestamp = _a.toTimestamp, eventAbi = _a.eventAbi, topic = _a.topic, topics = _a.topics, extraTopics = _a.extraTopics, // can be passed as input as extra filter arguments
48
- _c = _a.skipCache, // can be passed as input as extra filter arguments
49
- skipCache = _c === void 0 ? false : _c, _d = _a.skipCacheRead, skipCacheRead = _d === void 0 ? false : _d, _e = _a.entireLog, entireLog = _e === void 0 ? false : _e, _f = _a.cacheInCloud, cacheInCloud = _f === void 0 ? false : _f, _g = _a.onlyArgs, onlyArgs = _g === void 0 ? false : _g, _h = _a.keys, keys = _h === void 0 ? [] : _h;
61
+ function getLogs(options) {
50
62
  return __awaiter(this, void 0, void 0, function () {
51
63
  function addLogsToCache(fromBlock, toBlock) {
52
64
  return __awaiter(this, void 0, void 0, function () {
@@ -58,8 +70,7 @@ function getLogs(_a) {
58
70
  metadata = cacheData.metadata;
59
71
  return [4 /*yield*/, (0, _1.getLogs)({
60
72
  chain: chain,
61
- target: target,
62
- topic: topic,
73
+ target: target, topic: topic,
63
74
  keys: keys,
64
75
  topics: topics,
65
76
  fromBlock: fromBlock,
@@ -123,7 +134,7 @@ function getLogs(_a) {
123
134
  throw new Error('extraKey is required');
124
135
  if (keys.length)
125
136
  extraKey += '-' + keys.join('-').toLowerCase();
126
- return "event-logs/".concat(chain, "/").concat(target.toLowerCase(), "/").concat(target.toLowerCase());
137
+ return "event-logs/".concat(chain, "/").concat(target.toLowerCase(), "-").concat(extraKey);
127
138
  }
128
139
  // we need to form filter topic with indexed keyword, else it messes up generated topic string
129
140
  function toFilterTopic(topic) {
@@ -135,36 +146,47 @@ function getLogs(_a) {
135
146
  var fragment = topic.fragments[0];
136
147
  return (0, ethers_1.id)("".concat(fragment.name, "(").concat(fragment.inputs.map(function (i) { return i.type; }).join(','), ")"));
137
148
  }
138
- var iface, cacheData, isFirstRun, metadata, logs;
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;
139
150
  return __generator(this, function (_j) {
140
151
  switch (_j.label) {
141
152
  case 0:
142
- if (!target)
143
- throw new Error('target is required');
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');
144
156
  if (!fromBlock && !fromTimestamp)
145
157
  throw new Error('fromBlock or fromTimestamp is required');
146
158
  if (!toBlock && !toTimestamp)
147
159
  throw new Error('toBlock or toTimestamp is required');
160
+ limiter = getChainLimiter(chain);
148
161
  if (!!fromBlock) return [3 /*break*/, 2];
149
- return [4 /*yield*/, (0, blocks_1.getBlock)(chain, fromTimestamp)];
162
+ return [4 /*yield*/, limiter(function () { return (0, blocks_1.getBlock)(chain, fromTimestamp); })];
150
163
  case 1:
151
164
  fromBlock = (_j.sent()).block;
152
165
  _j.label = 2;
153
166
  case 2:
154
167
  if (!!toBlock) return [3 /*break*/, 4];
155
- return [4 /*yield*/, (0, blocks_1.getBlock)(chain, toTimestamp)];
168
+ return [4 /*yield*/, limiter(function () { return (0, blocks_1.getBlock)(chain, toTimestamp); })];
156
169
  case 3:
157
170
  toBlock = (_j.sent()).block;
158
171
  _j.label = 4;
159
172
  case 4:
160
- if ((!topics || !topics.length) && !topic) {
161
- if (topic) {
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)
162
187
  topic = toFilterTopic(topic);
163
- }
164
- else if (eventAbi) {
165
- iface = new ethers_1.Interface([eventAbi]);
188
+ else if (eventAbi)
166
189
  topic = toFilterTopic(iface);
167
- }
168
190
  else {
169
191
  throw new Error('eventAbi | topic | topics are required');
170
192
  }
@@ -173,30 +195,30 @@ function getLogs(_a) {
173
195
  topics.push.apply(topics, extraTopics);
174
196
  }
175
197
  return [4 /*yield*/, _getCache()];
176
- case 5:
198
+ case 7:
177
199
  cacheData = _j.sent();
178
200
  isFirstRun = !cacheData.metadata;
179
201
  if (isFirstRun)
180
202
  cacheData.metadata = { version: currentVersion };
181
- if (!isFirstRun) return [3 /*break*/, 7];
182
- return [4 /*yield*/, addLogsToCache(fromBlock, toBlock)];
183
- case 6:
184
- _j.sent();
185
- return [3 /*break*/, 11];
186
- case 7:
187
- metadata = cacheData.metadata;
188
- if (!(fromBlock < metadata.fromBlock)) return [3 /*break*/, 9];
189
- return [4 /*yield*/, addLogsToCache(fromBlock, metadata.fromBlock - 1)];
203
+ if (!isFirstRun) return [3 /*break*/, 9];
204
+ return [4 /*yield*/, limiter(function () { return addLogsToCache(fromBlock, toBlock); })];
190
205
  case 8:
191
206
  _j.sent();
192
- _j.label = 9;
207
+ return [3 /*break*/, 13];
193
208
  case 9:
194
- if (!(toBlock > metadata.toBlock)) return [3 /*break*/, 11];
195
- return [4 /*yield*/, addLogsToCache(metadata.toBlock + 1, toBlock)];
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); })];
196
212
  case 10:
197
213
  _j.sent();
198
214
  _j.label = 11;
199
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:
200
222
  logs = cacheData.logs.filter(function (i) {
201
223
  if (i.blockNumber < fromBlock)
202
224
  return false;
@@ -212,4 +234,16 @@ function getLogs(_a) {
212
234
  });
213
235
  }
214
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
+ }
215
249
  exports.default = getLogs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "5.0.4",
3
+ "version": "5.0.6",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {