@defillama/sdk 3.0.9 → 3.0.11

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.
@@ -12,8 +12,8 @@ export declare function getLatestBlock(chain: string): Promise<TimestampBlock>;
12
12
  export declare function lookupBlock(timestamp: number, extraParams?: {
13
13
  chain?: Chain | "terra" | "kava" | "algorand";
14
14
  }): Promise<{
15
- block: number;
16
15
  timestamp: number;
16
+ block: number;
17
17
  }>;
18
18
  export declare function getLogs(params: {
19
19
  target: Address;
@@ -154,21 +154,34 @@ function lookupBlock(timestamp, extraParams) {
154
154
  var _a, _b, _c;
155
155
  if (extraParams === void 0) { extraParams = {}; }
156
156
  return __awaiter(this, void 0, void 0, function () {
157
- var chain, low, lowBlock, highBlock, provider, _d, lastBlock, firstBlock, block, i, time, allowedTimeRange, acceptableBlockImprecision, blockImprecision, imprecision, getPrecision_1, blockDiff, timeDiff, avgBlockTime, closeBlock, midBlock, blocks, e_1;
157
+ var chain, api, res, data, low, lowBlock, highBlock, provider, _d, lastBlock, firstBlock, block, i, time, allowedTimeRange, acceptableBlockImprecision, blockImprecision, imprecision, getPrecision_1, blockDiff, timeDiff, avgBlockTime, closeBlock, midBlock, blocks, e_1;
158
158
  return __generator(this, function (_e) {
159
159
  switch (_e.label) {
160
160
  case 0:
161
161
  chain = (_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _a !== void 0 ? _a : "ethereum";
162
- low = (_b = intialBlocks[chain]) !== null && _b !== void 0 ? _b : 100;
163
- _e.label = 1;
162
+ if (!(chain === 'celo')) return [3 /*break*/, 3];
163
+ api = "https://explorer.celo.org/mainnet/api?module=block&action=getblocknobytime&timestamp=".concat(timestamp, "&closest=before");
164
+ return [4 /*yield*/, (0, node_fetch_1.default)(api)];
164
165
  case 1:
165
- _e.trys.push([1, 7, , 8]);
166
+ res = _e.sent();
167
+ return [4 /*yield*/, res.json()];
168
+ case 2:
169
+ data = _e.sent();
170
+ return [2 /*return*/, {
171
+ timestamp: timestamp,
172
+ block: +data.result.blockNumber
173
+ }];
174
+ case 3:
175
+ low = (_b = intialBlocks[chain]) !== null && _b !== void 0 ? _b : 100;
176
+ _e.label = 4;
177
+ case 4:
178
+ _e.trys.push([4, 10, , 11]);
166
179
  provider = getExtraProvider(chain);
167
180
  return [4 /*yield*/, Promise.all([
168
181
  getBlock(provider, "latest", chain),
169
182
  getBlock(provider, low, chain),
170
183
  ])];
171
- case 2:
184
+ case 5:
172
185
  _d = _e.sent(), lastBlock = _d[0], firstBlock = _d[1];
173
186
  lowBlock = firstBlock;
174
187
  highBlock = lastBlock;
@@ -191,8 +204,8 @@ function lookupBlock(timestamp, extraParams) {
191
204
  blockImprecision = void 0;
192
205
  imprecision = void 0;
193
206
  getPrecision_1 = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
194
- _e.label = 3;
195
- case 3:
207
+ _e.label = 6;
208
+ case 6:
196
209
  ++i;
197
210
  blockDiff = highBlock.number - lowBlock.number;
198
211
  timeDiff = highBlock.timestamp - lowBlock.timestamp;
@@ -203,7 +216,7 @@ function lookupBlock(timestamp, extraParams) {
203
216
  getBlock(provider, closeBlock, chain),
204
217
  getBlock(provider, midBlock, chain),
205
218
  ])];
206
- case 4:
219
+ case 7:
207
220
  blocks = _e.sent();
208
221
  blocks.push(highBlock, lowBlock);
209
222
  blocks.sort(function (a, b) { return getPrecision_1(a) - getPrecision_1(b); });
@@ -213,11 +226,11 @@ function lookupBlock(timestamp, extraParams) {
213
226
  highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
214
227
  imprecision = getPrecision_1(block);
215
228
  blockImprecision = highBlock.number - lowBlock.number;
216
- _e.label = 5;
217
- case 5:
218
- if (imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision) return [3 /*break*/, 3];
219
- _e.label = 6;
220
- case 6:
229
+ _e.label = 8;
230
+ case 8:
231
+ if (imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision) return [3 /*break*/, 6];
232
+ _e.label = 9;
233
+ case 9:
221
234
  (0, debugLog_1.debugLog)("chain: ".concat(chain, " block: ").concat(block.number, " #calls: ").concat(i, " imprecision: ").concat(Number((imprecision) / 60).toFixed(2), " (min) Time Taken: ").concat(Number((Date.now() - time) / 1000).toFixed(2), " (in sec)"));
222
235
  if (chain !== "bsc" && // this check is there because bsc halted the chain for few days
223
236
  Math.abs(block.timestamp - timestamp) > 3600) {
@@ -227,11 +240,11 @@ function lookupBlock(timestamp, extraParams) {
227
240
  block: block.number,
228
241
  timestamp: block.timestamp
229
242
  }];
230
- case 7:
243
+ case 10:
231
244
  e_1 = _e.sent();
232
245
  console.log(e_1);
233
246
  throw new Error("Couldn't find block height for chain ".concat(chain, ", RPC node rugged"));
234
- case 8: return [2 /*return*/];
247
+ case 11: return [2 /*return*/];
235
248
  }
236
249
  });
237
250
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "3.0.9",
3
+ "version": "3.0.11",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {