@defillama/sdk 2.3.118 → 2.3.119
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.d.ts +5 -3
- package/build/util/index.js +30 -7
- package/package.json +1 -1
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,7 +142,7 @@ 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;
|
|
145
|
+
var provider, lastBlock, high, low, block, blockPrecision, i, time, mid, e_1;
|
|
127
146
|
return __generator(this, function (_f) {
|
|
128
147
|
switch (_f.label) {
|
|
129
148
|
case 0:
|
|
@@ -132,8 +151,7 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
132
151
|
return [4 /*yield*/, getBlock(provider, "latest", extraParams.chain)];
|
|
133
152
|
case 1:
|
|
134
153
|
lastBlock = _f.sent();
|
|
135
|
-
if (
|
|
136
|
-
lastBlock.timestamp - timestamp < -30 * 60) {
|
|
154
|
+
if (lastBlock.timestamp - timestamp < -30 * 60) {
|
|
137
155
|
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) + "\"");
|
|
138
156
|
}
|
|
139
157
|
if (Math.abs(lastBlock.timestamp - timestamp) < 60) {
|
|
@@ -146,8 +164,12 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
146
164
|
high = lastBlock.number;
|
|
147
165
|
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
166
|
block = void 0;
|
|
167
|
+
blockPrecision = extraParams.chain === 'ethereum' ? 20 : 200;
|
|
168
|
+
i = 0;
|
|
169
|
+
time = Date.now();
|
|
149
170
|
_f.label = 2;
|
|
150
171
|
case 2:
|
|
172
|
+
++i;
|
|
151
173
|
mid = Math.floor((high + low) / 2);
|
|
152
174
|
return [4 /*yield*/, getBlock(provider, mid, extraParams.chain)];
|
|
153
175
|
case 3:
|
|
@@ -160,11 +182,12 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
160
182
|
}
|
|
161
183
|
_f.label = 4;
|
|
162
184
|
case 4:
|
|
163
|
-
if (high - low >
|
|
185
|
+
if (high - low > blockPrecision) return [3 /*break*/, 2];
|
|
164
186
|
_f.label = 5;
|
|
165
187
|
case 5:
|
|
166
|
-
if (
|
|
167
|
-
|
|
188
|
+
if (process.env.LLAMA_DEBUG_MODE)
|
|
189
|
+
console.log(extraParams.chain, i, (block.timestamp - timestamp) / 60, (Date.now() - time) / 1000, 'block: ', block.number);
|
|
190
|
+
if (Math.abs(block.timestamp - timestamp) > 3600) {
|
|
168
191
|
throw new Error("Block selected is more than 1 hour away from the requested timestamp");
|
|
169
192
|
}
|
|
170
193
|
return [2 /*return*/, {
|