@defillama/sdk 2.3.120 → 2.3.122
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.
- package/build/util/index.js +38 -29
- package/package.json +1 -1
package/build/util/index.js
CHANGED
|
@@ -139,28 +139,29 @@ var intialBlocks = {
|
|
|
139
139
|
crab: 4969901
|
|
140
140
|
};
|
|
141
141
|
function lookupBlock(timestamp, extraParams) {
|
|
142
|
-
var _a, _b, _c, _d
|
|
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,
|
|
146
|
-
return __generator(this, function (
|
|
147
|
-
switch (
|
|
145
|
+
var chain, low, lowBlock, highBlock, provider, _e, lastBlock, firstBlock, block, i, time, allowedTimeRange, acceptableBlockImprecision, blockImprecision, 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
|
-
|
|
150
|
-
|
|
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
|
-
|
|
153
|
-
provider = getExtraProvider(
|
|
153
|
+
_f.trys.push([1, 7, , 8]);
|
|
154
|
+
provider = getExtraProvider(chain);
|
|
154
155
|
return [4 /*yield*/, Promise.all([
|
|
155
|
-
getBlock(provider, "latest",
|
|
156
|
-
getBlock(provider, low,
|
|
156
|
+
getBlock(provider, "latest", chain),
|
|
157
|
+
getBlock(provider, low, chain),
|
|
157
158
|
])];
|
|
158
159
|
case 2:
|
|
159
|
-
|
|
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 \"" +
|
|
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
|
|
@@ -174,32 +175,40 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
174
175
|
time = Date.now();
|
|
175
176
|
allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
|
|
176
177
|
;
|
|
178
|
+
acceptableBlockImprecision = chain === 'ethereum' ? 20 : 200;
|
|
179
|
+
blockImprecision = void 0;
|
|
177
180
|
imprecision = void 0;
|
|
178
|
-
|
|
181
|
+
getPrecision_1 = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
|
|
182
|
+
_f.label = 3;
|
|
179
183
|
case 3:
|
|
180
184
|
++i;
|
|
181
185
|
blockDiff = highBlock.number - lowBlock.number;
|
|
182
186
|
timeDiff = highBlock.timestamp - lowBlock.timestamp;
|
|
183
187
|
avgBlockTime = timeDiff / blockDiff;
|
|
184
188
|
closeBlock = Math.floor(lowBlock.number + (timestamp - lowBlock.timestamp) / avgBlockTime);
|
|
185
|
-
|
|
189
|
+
midBlock = Math.floor((lowBlock.number + highBlock.number) / 2);
|
|
190
|
+
return [4 /*yield*/, Promise.all([
|
|
191
|
+
getBlock(provider, closeBlock, chain),
|
|
192
|
+
getBlock(provider, midBlock, chain),
|
|
193
|
+
])];
|
|
186
194
|
case 4:
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
blocks = _f.sent();
|
|
196
|
+
blocks.push(highBlock, lowBlock);
|
|
197
|
+
blocks.sort(function (a, b) { return getPrecision_1(a) - getPrecision_1(b); });
|
|
198
|
+
block = blocks[0];
|
|
199
|
+
// find the closest upper and lower bound between 4 points
|
|
200
|
+
lowBlock = blocks.filter(function (i) { return i.timestamp < timestamp; }).reduce(function (lowestBlock, block) { return (timestamp - lowestBlock.timestamp) < (timestamp - block.timestamp) ? lowestBlock : block; });
|
|
201
|
+
highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
|
|
202
|
+
imprecision = getPrecision_1(block);
|
|
203
|
+
blockImprecision = highBlock.number - lowBlock.number;
|
|
204
|
+
_f.label = 5;
|
|
196
205
|
case 5:
|
|
197
|
-
if (imprecision > allowedTimeRange) return [3 /*break*/, 3];
|
|
198
|
-
|
|
206
|
+
if (imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision) return [3 /*break*/, 3];
|
|
207
|
+
_f.label = 6;
|
|
199
208
|
case 6:
|
|
200
209
|
if (process.env.LLAMA_DEBUG_MODE)
|
|
201
|
-
console.log("chain: " +
|
|
202
|
-
if (
|
|
210
|
+
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)");
|
|
211
|
+
if (chain !== "bsc" && // this check is there because bsc halted the chain for few days
|
|
203
212
|
Math.abs(block.timestamp - timestamp) > 3600) {
|
|
204
213
|
throw new Error("Block selected is more than 1 hour away from the requested timestamp");
|
|
205
214
|
}
|
|
@@ -208,9 +217,9 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
208
217
|
timestamp: block.timestamp
|
|
209
218
|
}];
|
|
210
219
|
case 7:
|
|
211
|
-
e_1 =
|
|
220
|
+
e_1 = _f.sent();
|
|
212
221
|
console.log(e_1);
|
|
213
|
-
throw new Error("Couldn't find block height for chain " +
|
|
222
|
+
throw new Error("Couldn't find block height for chain " + chain + ", RPC node rugged");
|
|
214
223
|
case 8: return [2 /*return*/];
|
|
215
224
|
}
|
|
216
225
|
});
|