@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.
- package/build/computeTVL/blocks.js +4 -2
- package/build/util/index.d.ts +5 -3
- package/build/util/index.js +67 -31
- package/package.json +1 -1
|
@@ -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:
|
|
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 =
|
|
154
|
+
block = _a.sent();
|
|
153
155
|
return [2 /*return*/, block];
|
|
154
156
|
case 4:
|
|
155
157
|
e_1 = _a.sent();
|
package/build/util/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
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 {};
|
package/build/util/index.js
CHANGED
|
@@ -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,
|
|
127
|
-
return __generator(this, function (
|
|
128
|
-
switch (
|
|
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
|
-
|
|
131
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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 (
|
|
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
|
|
175
|
-
e_1 =
|
|
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
|
|
214
|
+
case 8: return [2 /*return*/];
|
|
179
215
|
}
|
|
180
216
|
});
|
|
181
217
|
});
|