@defillama/sdk 2.3.119 → 2.3.121

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();
@@ -139,20 +139,29 @@ var intialBlocks = {
139
139
  crab: 4969901
140
140
  };
141
141
  function lookupBlock(timestamp, extraParams) {
142
- var _a, _b, _c, _d, _e;
142
+ var _a, _b, _c, _d;
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;
145
+ var chain, low, lowBlock, highBlock, provider, _e, lastBlock, firstBlock, block, i, time, allowedTimeRange, imprecision, getPrecision_1, blockDiff, timeDiff, avgBlockTime, closeBlock, midBlock, blocks, e_1;
146
146
  return __generator(this, function (_f) {
147
147
  switch (_f.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
+ chain = (_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _a !== void 0 ? _a : "ethereum";
150
+ low = (_b = intialBlocks[chain]) !== null && _b !== void 0 ? _b : 100;
151
+ _f.label = 1;
152
152
  case 1:
153
- lastBlock = _f.sent();
153
+ _f.trys.push([1, 7, , 8]);
154
+ provider = getExtraProvider(chain);
155
+ return [4 /*yield*/, Promise.all([
156
+ getBlock(provider, "latest", chain),
157
+ getBlock(provider, low, chain),
158
+ ])];
159
+ case 2:
160
+ _e = _f.sent(), lastBlock = _e[0], firstBlock = _e[1];
161
+ lowBlock = firstBlock;
162
+ highBlock = lastBlock;
154
163
  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) + "\"");
164
+ throw new Error("Last block of chain \"" + 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
165
  }
157
166
  if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
158
167
  // Short-circuit in case we are trying to get the current block
@@ -161,44 +170,54 @@ function lookupBlock(timestamp, extraParams) {
161
170
  timestamp: lastBlock.timestamp
162
171
  }];
163
172
  }
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
173
  block = void 0;
167
- blockPrecision = extraParams.chain === 'ethereum' ? 20 : 200;
168
174
  i = 0;
169
175
  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)];
176
+ allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
177
+ ;
178
+ imprecision = void 0;
179
+ getPrecision_1 = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
180
+ _f.label = 3;
175
181
  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;
182
+ ++i;
183
+ blockDiff = highBlock.number - lowBlock.number;
184
+ timeDiff = highBlock.timestamp - lowBlock.timestamp;
185
+ avgBlockTime = timeDiff / blockDiff;
186
+ closeBlock = Math.floor(lowBlock.number + (timestamp - lowBlock.timestamp) / avgBlockTime);
187
+ midBlock = Math.floor((lowBlock.number + highBlock.number) / 2);
188
+ return [4 /*yield*/, Promise.all([
189
+ getBlock(provider, closeBlock, chain),
190
+ getBlock(provider, midBlock, chain),
191
+ ])];
184
192
  case 4:
185
- if (high - low > blockPrecision) return [3 /*break*/, 2];
193
+ blocks = _f.sent();
194
+ blocks.push(highBlock, lowBlock);
195
+ blocks.sort(function (a, b) { return getPrecision_1(a) - getPrecision_1(b); });
196
+ block = blocks[0];
197
+ // find the closest upper and lower bound between 4 points
198
+ lowBlock = blocks.filter(function (i) { return i.timestamp < timestamp; }).reduce(function (lowestBlock, block) { return (timestamp - lowestBlock.timestamp) < (timestamp - block.timestamp) ? lowestBlock : block; });
199
+ highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
200
+ imprecision = getPrecision_1(block);
186
201
  _f.label = 5;
187
202
  case 5:
203
+ if (imprecision > allowedTimeRange) return [3 /*break*/, 3];
204
+ _f.label = 6;
205
+ case 6:
188
206
  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) {
207
+ console.log("chain: " + chain + " block: " + block.number + " #calls: " + i + " imprecision: " + Number((imprecision) / 60).toFixed(2) + " (min) Time Taken: " + Number((Date.now() - time) / 1000).toFixed(2) + " (in sec)");
208
+ if (chain !== "bsc" && // this check is there because bsc halted the chain for few days
209
+ Math.abs(block.timestamp - timestamp) > 3600) {
191
210
  throw new Error("Block selected is more than 1 hour away from the requested timestamp");
192
211
  }
193
212
  return [2 /*return*/, {
194
213
  block: block.number,
195
214
  timestamp: block.timestamp
196
215
  }];
197
- case 6:
216
+ case 7:
198
217
  e_1 = _f.sent();
199
218
  console.log(e_1);
200
- 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*/];
219
+ throw new Error("Couldn't find block height for chain " + chain + ", RPC node rugged");
220
+ case 8: return [2 /*return*/];
202
221
  }
203
222
  });
204
223
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "2.3.119",
3
+ "version": "2.3.121",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {