@defillama/sdk 2.3.118 → 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();
@@ -1,12 +1,13 @@
1
1
  import { Chain } from "../general";
2
2
  import type { Address } from "../types";
3
3
  import type { Log } from "@ethersproject/abstract-provider";
4
- export declare function getLatestBlock(chain: string): Promise<{
4
+ interface TimestampBlock {
5
5
  number: number;
6
6
  timestamp: number;
7
- }>;
7
+ }
8
+ export declare function getLatestBlock(chain: string): Promise<TimestampBlock>;
8
9
  export declare function lookupBlock(timestamp: number, extraParams?: {
9
- chain?: Chain | "terra" | "kava";
10
+ chain?: Chain | "terra" | "kava" | "algorand";
10
11
  }): Promise<{
11
12
  block: number;
12
13
  timestamp: number;
@@ -40,3 +41,4 @@ export declare function normalizeBalances(balances: {
40
41
  }): {
41
42
  [address: string]: string;
42
43
  };
44
+ export {};
@@ -75,11 +75,27 @@ var terraBlockProvider = {
75
75
  .then(function (res) { return res.json(); })
76
76
  .then(function (block) { return ({
77
77
  number: Number(block.block.header.height),
78
- timestamp: Math.round(Date.parse(block.block.header.time) / 1000)
78
+ timestamp: Math.round(Date.parse(block.block.header.time) / 1000),
79
79
  }); })];
80
80
  });
81
81
  }); }
82
82
  };
83
+ var algorandBlockProvider = {
84
+ getBlock: function (height) { return __awaiter(void 0, void 0, void 0, function () {
85
+ return __generator(this, function (_a) {
86
+ if (height !== 'latest')
87
+ return [2 /*return*/, node_fetch_1.default("https://algoindexer.algoexplorerapi.io/v2/blocks/" + height)
88
+ .then(function (res) { return res.json(); })
89
+ .then(function (block) { return ({
90
+ number: block.round,
91
+ timestamp: block.timestamp
92
+ }); })];
93
+ return [2 /*return*/, node_fetch_1.default('https://algoindexer.algoexplorerapi.io/health')
94
+ .then(function (res) { return res.json(); })
95
+ .then(function (block) { return algorandBlockProvider.getBlock(block.round); })];
96
+ });
97
+ }); }
98
+ };
83
99
  function getBlock(provider, height, chain) {
84
100
  return __awaiter(this, void 0, void 0, function () {
85
101
  var block;
@@ -103,6 +119,9 @@ function getExtraProvider(chain) {
103
119
  else if (chain === "kava") {
104
120
  return kavaBlockProvider;
105
121
  }
122
+ else if (chain === "algorand") {
123
+ return algorandBlockProvider;
124
+ }
106
125
  return general_1.getProvider(chain);
107
126
  }
108
127
  function getLatestBlock(chain) {
@@ -123,18 +142,25 @@ function lookupBlock(timestamp, extraParams) {
123
142
  var _a, _b, _c, _d, _e;
124
143
  if (extraParams === void 0) { extraParams = {}; }
125
144
  return __awaiter(this, void 0, void 0, function () {
126
- var provider, lastBlock, high, low, block, mid, e_1;
127
- return __generator(this, function (_f) {
128
- 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) {
129
148
  case 0:
130
- _f.trys.push([0, 6, , 7]);
131
- provider = getExtraProvider(extraParams.chain);
132
- 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;
133
151
  case 1:
134
- lastBlock = _f.sent();
135
- if (extraParams.chain !== "bsc" &&
136
- lastBlock.timestamp - timestamp < -30 * 60) {
137
- 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) + "\"");
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;
162
+ 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) + "\"");
138
164
  }
139
165
  if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
140
166
  // Short-circuit in case we are trying to get the current block
@@ -143,27 +169,37 @@ function lookupBlock(timestamp, extraParams) {
143
169
  timestamp: lastBlock.timestamp
144
170
  }];
145
171
  }
146
- high = lastBlock.number;
147
- 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;
148
172
  block = void 0;
149
- _f.label = 2;
150
- case 2:
151
- mid = Math.floor((high + low) / 2);
152
- return [4 /*yield*/, getBlock(provider, mid, extraParams.chain)];
173
+ i = 0;
174
+ time = Date.now();
175
+ allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
176
+ ;
177
+ imprecision = void 0;
178
+ _g.label = 3;
153
179
  case 3:
154
- block = _f.sent();
155
- if (block.timestamp < timestamp) {
156
- low = mid + 1;
157
- }
158
- else {
159
- high = mid - 1;
160
- }
161
- _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)];
162
186
  case 4:
163
- if (high - low > 4) return [3 /*break*/, 2];
164
- _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;
165
196
  case 5:
166
- if (extraParams.chain !== "bsc" &&
197
+ if (imprecision > allowedTimeRange) return [3 /*break*/, 3];
198
+ _g.label = 6;
199
+ case 6:
200
+ 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
167
203
  Math.abs(block.timestamp - timestamp) > 3600) {
168
204
  throw new Error("Block selected is more than 1 hour away from the requested timestamp");
169
205
  }
@@ -171,11 +207,11 @@ function lookupBlock(timestamp, extraParams) {
171
207
  block: block.number,
172
208
  timestamp: block.timestamp
173
209
  }];
174
- case 6:
175
- e_1 = _f.sent();
210
+ case 7:
211
+ e_1 = _g.sent();
176
212
  console.log(e_1);
177
213
  throw new Error("Couldn't find block height for chain " + ((_e = extraParams.chain) !== null && _e !== void 0 ? _e : "ethereum") + ", RPC node rugged");
178
- case 7: return [2 /*return*/];
214
+ case 8: return [2 /*return*/];
179
215
  }
180
216
  });
181
217
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defillama/sdk",
3
- "version": "2.3.118",
3
+ "version": "2.3.120",
4
4
  "description": "SDK for calculating TVL",
5
5
  "main": "build/index.js",
6
6
  "scripts": {