@defillama/sdk 2.3.119 → 2.3.120

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.
@@ -74,7 +74,9 @@ function getBlocks(timestamp, chains) {
74
74
  var _a, ethBlock, chainBlocks;
75
75
  return __generator(this, function (_b) {
76
76
  switch (_b.label) {
77
- case 0: return [4 /*yield*/, Promise.all([getBlock('ethereum', timestamp), getChainBlocks(timestamp, chains)])];
77
+ case 0:
78
+ chains = chains === null || chains === void 0 ? void 0 : chains.filter(function (i) { return i !== 'ethereum'; });
79
+ return [4 /*yield*/, Promise.all([getBlock('ethereum', timestamp), getChainBlocks(timestamp, chains)])];
78
80
  case 1:
79
81
  _a = _b.sent(), ethBlock = _a[0], chainBlocks = _a[1];
80
82
  chainBlocks['ethereum'] = ethBlock.block;
@@ -149,7 +151,7 @@ function getBlock(chain, timestamp) {
149
151
  _a.trys.push([2, 4, , 5]);
150
152
  return [4 /*yield*/, index_1.lookupBlock(timestamp, { chain: chain, })];
151
153
  case 3:
152
- block = (_a.sent()).block;
154
+ block = _a.sent();
153
155
  return [2 /*return*/, block];
154
156
  case 4:
155
157
  e_1 = _a.sent();
@@ -142,17 +142,25 @@ function lookupBlock(timestamp, extraParams) {
142
142
  var _a, _b, _c, _d, _e;
143
143
  if (extraParams === void 0) { extraParams = {}; }
144
144
  return __awaiter(this, void 0, void 0, function () {
145
- var provider, lastBlock, high, low, block, blockPrecision, i, time, mid, e_1;
146
- return __generator(this, function (_f) {
147
- switch (_f.label) {
145
+ var low, lowBlock, highBlock, provider, _f, lastBlock, firstBlock, block, i, time, allowedTimeRange, imprecision, blockDiff, timeDiff, avgBlockTime, closeBlock, e_1;
146
+ return __generator(this, function (_g) {
147
+ switch (_g.label) {
148
148
  case 0:
149
- _f.trys.push([0, 6, , 7]);
150
- provider = getExtraProvider(extraParams.chain);
151
- return [4 /*yield*/, getBlock(provider, "latest", extraParams.chain)];
149
+ low = (_b = intialBlocks[(_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _a !== void 0 ? _a : "ethereum"]) !== null && _b !== void 0 ? _b : 100;
150
+ _g.label = 1;
152
151
  case 1:
153
- lastBlock = _f.sent();
152
+ _g.trys.push([1, 7, , 8]);
153
+ provider = getExtraProvider(extraParams.chain);
154
+ return [4 /*yield*/, Promise.all([
155
+ getBlock(provider, "latest", extraParams.chain),
156
+ getBlock(provider, low, extraParams.chain),
157
+ ])];
158
+ case 2:
159
+ _f = _g.sent(), lastBlock = _f[0], firstBlock = _f[1];
160
+ lowBlock = firstBlock;
161
+ highBlock = lastBlock;
154
162
  if (lastBlock.timestamp - timestamp < -30 * 60) {
155
- throw new Error("Last block of chain \"" + extraParams.chain + "\" is further than 30 minutes into the past. Provider is \"" + ((_b = (_a = provider) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.url) + "\"");
163
+ throw new Error("Last block of chain \"" + extraParams.chain + "\" is further than 30 minutes into the past. Provider is \"" + ((_d = (_c = provider) === null || _c === void 0 ? void 0 : _c.connection) === null || _d === void 0 ? void 0 : _d.url) + "\"");
156
164
  }
157
165
  if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
158
166
  // Short-circuit in case we are trying to get the current block
@@ -161,44 +169,49 @@ function lookupBlock(timestamp, extraParams) {
161
169
  timestamp: lastBlock.timestamp
162
170
  }];
163
171
  }
164
- high = lastBlock.number;
165
- low = (_d = intialBlocks[(_c = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _c !== void 0 ? _c : "ethereum"]) !== null && _d !== void 0 ? _d : 0;
166
172
  block = void 0;
167
- blockPrecision = extraParams.chain === 'ethereum' ? 20 : 200;
168
173
  i = 0;
169
174
  time = Date.now();
170
- _f.label = 2;
171
- case 2:
172
- ++i;
173
- mid = Math.floor((high + low) / 2);
174
- return [4 /*yield*/, getBlock(provider, mid, extraParams.chain)];
175
+ allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
176
+ ;
177
+ imprecision = void 0;
178
+ _g.label = 3;
175
179
  case 3:
176
- block = _f.sent();
177
- if (block.timestamp < timestamp) {
178
- low = mid + 1;
179
- }
180
- else {
181
- high = mid - 1;
182
- }
183
- _f.label = 4;
180
+ ++i;
181
+ blockDiff = highBlock.number - lowBlock.number;
182
+ timeDiff = highBlock.timestamp - lowBlock.timestamp;
183
+ avgBlockTime = timeDiff / blockDiff;
184
+ closeBlock = Math.floor(lowBlock.number + (timestamp - lowBlock.timestamp) / avgBlockTime);
185
+ return [4 /*yield*/, getBlock(provider, closeBlock, extraParams.chain)];
184
186
  case 4:
185
- if (high - low > blockPrecision) return [3 /*break*/, 2];
186
- _f.label = 5;
187
+ block = _g.sent();
188
+ if (block.timestamp < timestamp)
189
+ lowBlock = block;
190
+ else
191
+ highBlock = block;
192
+ imprecision = block.timestamp - timestamp;
193
+ if (imprecision < 0)
194
+ imprecision *= -1;
195
+ _g.label = 5;
187
196
  case 5:
197
+ if (imprecision > allowedTimeRange) return [3 /*break*/, 3];
198
+ _g.label = 6;
199
+ case 6:
188
200
  if (process.env.LLAMA_DEBUG_MODE)
189
- console.log(extraParams.chain, i, (block.timestamp - timestamp) / 60, (Date.now() - time) / 1000, 'block: ', block.number);
190
- if (Math.abs(block.timestamp - timestamp) > 3600) {
201
+ console.log("chain: " + extraParams.chain + " block: " + block.number + " #calls: " + i + " imprecision: " + Number((imprecision) / 60).toFixed(2) + " (min) Time Taken: " + Number((Date.now() - time) / 1000).toFixed(2) + " (in sec)");
202
+ if (extraParams.chain !== "bsc" && // this check is there because bsc halted the chain for few days
203
+ Math.abs(block.timestamp - timestamp) > 3600) {
191
204
  throw new Error("Block selected is more than 1 hour away from the requested timestamp");
192
205
  }
193
206
  return [2 /*return*/, {
194
207
  block: block.number,
195
208
  timestamp: block.timestamp
196
209
  }];
197
- case 6:
198
- e_1 = _f.sent();
210
+ case 7:
211
+ e_1 = _g.sent();
199
212
  console.log(e_1);
200
213
  throw new Error("Couldn't find block height for chain " + ((_e = extraParams.chain) !== null && _e !== void 0 ? _e : "ethereum") + ", RPC node rugged");
201
- case 7: return [2 /*return*/];
214
+ case 8: return [2 /*return*/];
202
215
  }
203
216
  });
204
217
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "2.3.119",
3
+ "version": "2.3.120",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {