@defillama/sdk 2.3.120 → 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.
Files changed (2) hide show
  1. package/build/util/index.js +34 -28
  2. package/package.json +1 -1
@@ -139,28 +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 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) {
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
+ return __generator(this, function (_f) {
147
+ switch (_f.label) {
148
148
  case 0:
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;
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;
151
152
  case 1:
152
- _g.trys.push([1, 7, , 8]);
153
- provider = getExtraProvider(extraParams.chain);
153
+ _f.trys.push([1, 7, , 8]);
154
+ provider = getExtraProvider(chain);
154
155
  return [4 /*yield*/, Promise.all([
155
- getBlock(provider, "latest", extraParams.chain),
156
- getBlock(provider, low, extraParams.chain),
156
+ getBlock(provider, "latest", chain),
157
+ getBlock(provider, low, chain),
157
158
  ])];
158
159
  case 2:
159
- _f = _g.sent(), lastBlock = _f[0], firstBlock = _f[1];
160
+ _e = _f.sent(), lastBlock = _e[0], firstBlock = _e[1];
160
161
  lowBlock = firstBlock;
161
162
  highBlock = lastBlock;
162
163
  if (lastBlock.timestamp - timestamp < -30 * 60) {
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) + "\"");
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) + "\"");
164
165
  }
165
166
  if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
166
167
  // Short-circuit in case we are trying to get the current block
@@ -175,31 +176,36 @@ function lookupBlock(timestamp, extraParams) {
175
176
  allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
176
177
  ;
177
178
  imprecision = void 0;
178
- _g.label = 3;
179
+ getPrecision_1 = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
180
+ _f.label = 3;
179
181
  case 3:
180
182
  ++i;
181
183
  blockDiff = highBlock.number - lowBlock.number;
182
184
  timeDiff = highBlock.timestamp - lowBlock.timestamp;
183
185
  avgBlockTime = timeDiff / blockDiff;
184
186
  closeBlock = Math.floor(lowBlock.number + (timestamp - lowBlock.timestamp) / avgBlockTime);
185
- return [4 /*yield*/, getBlock(provider, closeBlock, extraParams.chain)];
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
+ ])];
186
192
  case 4:
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;
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);
201
+ _f.label = 5;
196
202
  case 5:
197
203
  if (imprecision > allowedTimeRange) return [3 /*break*/, 3];
198
- _g.label = 6;
204
+ _f.label = 6;
199
205
  case 6:
200
206
  if (process.env.LLAMA_DEBUG_MODE)
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
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
203
209
  Math.abs(block.timestamp - timestamp) > 3600) {
204
210
  throw new Error("Block selected is more than 1 hour away from the requested timestamp");
205
211
  }
@@ -208,9 +214,9 @@ function lookupBlock(timestamp, extraParams) {
208
214
  timestamp: block.timestamp
209
215
  }];
210
216
  case 7:
211
- e_1 = _g.sent();
217
+ e_1 = _f.sent();
212
218
  console.log(e_1);
213
- throw new Error("Couldn't find block height for chain " + ((_e = extraParams.chain) !== null && _e !== void 0 ? _e : "ethereum") + ", RPC node rugged");
219
+ throw new Error("Couldn't find block height for chain " + chain + ", RPC node rugged");
214
220
  case 8: return [2 /*return*/];
215
221
  }
216
222
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "2.3.120",
3
+ "version": "2.3.121",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {