@defillama/sdk 2.3.117 → 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/computeTVL/blocks.d.ts +4 -3
- package/build/computeTVL/blocks.js +95 -46
- package/build/util/index.d.ts +5 -3
- package/build/util/index.js +30 -7
- package/package.json +1 -1
|
@@ -3,16 +3,17 @@ export declare const chainsForBlocks: Chain[];
|
|
|
3
3
|
export declare function getChainBlocks(timestamp: number, chains?: Chain[]): Promise<{
|
|
4
4
|
[chain: string]: number;
|
|
5
5
|
}>;
|
|
6
|
-
export declare function getBlocks(timestamp: number): Promise<{
|
|
7
|
-
ethereumBlock:
|
|
6
|
+
export declare function getBlocks(timestamp: number, chains?: Chain[] | undefined): Promise<{
|
|
7
|
+
ethereumBlock: any;
|
|
8
8
|
chainBlocks: {
|
|
9
9
|
[chain: string]: number;
|
|
10
10
|
};
|
|
11
11
|
}>;
|
|
12
|
-
export declare function getCurrentBlocks(): Promise<{
|
|
12
|
+
export declare function getCurrentBlocks(chains?: Chain[] | undefined): Promise<{
|
|
13
13
|
timestamp: number;
|
|
14
14
|
ethereumBlock: number;
|
|
15
15
|
chainBlocks: {
|
|
16
16
|
[chain: string]: number;
|
|
17
17
|
};
|
|
18
18
|
}>;
|
|
19
|
+
export declare function getBlock(chain: Chain, timestamp?: number | undefined): Promise<any>;
|
|
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getCurrentBlocks = exports.getBlocks = exports.getChainBlocks = exports.chainsForBlocks = void 0;
|
|
39
|
+
exports.getBlock = exports.getCurrentBlocks = exports.getBlocks = exports.getChainBlocks = exports.chainsForBlocks = void 0;
|
|
40
40
|
var general_1 = require("../general");
|
|
41
41
|
var index_1 = require("../util/index");
|
|
42
42
|
exports.chainsForBlocks = ["avax", "bsc", "polygon", "xdai", "fantom", "arbitrum"];
|
|
@@ -44,45 +44,22 @@ var blockRetries = 5;
|
|
|
44
44
|
function getChainBlocks(timestamp, chains) {
|
|
45
45
|
if (chains === void 0) { chains = exports.chainsForBlocks; }
|
|
46
46
|
return __awaiter(this, void 0, void 0, function () {
|
|
47
|
-
var chainBlocks;
|
|
47
|
+
var chainBlocks, setBlock;
|
|
48
48
|
var _this = this;
|
|
49
49
|
return __generator(this, function (_a) {
|
|
50
50
|
switch (_a.label) {
|
|
51
51
|
case 0:
|
|
52
52
|
chainBlocks = {};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
case 2:
|
|
64
|
-
_c.trys.push([2, 4, , 5]);
|
|
65
|
-
_a = chainBlocks;
|
|
66
|
-
_b = chain;
|
|
67
|
-
return [4 /*yield*/, index_1.lookupBlock(timestamp, {
|
|
68
|
-
chain: chain,
|
|
69
|
-
}).then(function (block) { return block.block; })];
|
|
70
|
-
case 3:
|
|
71
|
-
_a[_b] = _c.sent();
|
|
72
|
-
return [3 /*break*/, 6];
|
|
73
|
-
case 4:
|
|
74
|
-
e_1 = _c.sent();
|
|
75
|
-
if (i === blockRetries - 1) {
|
|
76
|
-
throw e_1;
|
|
77
|
-
}
|
|
78
|
-
return [3 /*break*/, 5];
|
|
79
|
-
case 5:
|
|
80
|
-
i++;
|
|
81
|
-
return [3 /*break*/, 1];
|
|
82
|
-
case 6: return [2 /*return*/];
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}); }))];
|
|
53
|
+
setBlock = function (chain) { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) {
|
|
54
|
+
switch (_c.label) {
|
|
55
|
+
case 0:
|
|
56
|
+
_a = chainBlocks;
|
|
57
|
+
_b = chain;
|
|
58
|
+
return [4 /*yield*/, getBlock(chain, timestamp)];
|
|
59
|
+
case 1: return [2 /*return*/, _a[_b] = (_c.sent()).block];
|
|
60
|
+
}
|
|
61
|
+
}); }); };
|
|
62
|
+
return [4 /*yield*/, Promise.all(chains.map(setBlock))];
|
|
86
63
|
case 1:
|
|
87
64
|
_a.sent();
|
|
88
65
|
return [2 /*return*/, chainBlocks];
|
|
@@ -91,12 +68,13 @@ function getChainBlocks(timestamp, chains) {
|
|
|
91
68
|
});
|
|
92
69
|
}
|
|
93
70
|
exports.getChainBlocks = getChainBlocks;
|
|
94
|
-
function getBlocks(timestamp) {
|
|
71
|
+
function getBlocks(timestamp, chains) {
|
|
72
|
+
if (chains === void 0) { chains = undefined; }
|
|
95
73
|
return __awaiter(this, void 0, void 0, function () {
|
|
96
74
|
var _a, ethBlock, chainBlocks;
|
|
97
75
|
return __generator(this, function (_b) {
|
|
98
76
|
switch (_b.label) {
|
|
99
|
-
case 0: return [4 /*yield*/, Promise.all([
|
|
77
|
+
case 0: return [4 /*yield*/, Promise.all([getBlock('ethereum', timestamp), getChainBlocks(timestamp, chains)])];
|
|
100
78
|
case 1:
|
|
101
79
|
_a = _b.sent(), ethBlock = _a[0], chainBlocks = _a[1];
|
|
102
80
|
chainBlocks['ethereum'] = ethBlock.block;
|
|
@@ -109,21 +87,20 @@ function getBlocks(timestamp) {
|
|
|
109
87
|
});
|
|
110
88
|
}
|
|
111
89
|
exports.getBlocks = getBlocks;
|
|
112
|
-
function getCurrentBlocks() {
|
|
90
|
+
function getCurrentBlocks(chains) {
|
|
91
|
+
if (chains === void 0) { chains = undefined; }
|
|
113
92
|
return __awaiter(this, void 0, void 0, function () {
|
|
114
|
-
var
|
|
93
|
+
var block, chainBlocks;
|
|
115
94
|
return __generator(this, function (_a) {
|
|
116
95
|
switch (_a.label) {
|
|
117
96
|
case 0:
|
|
118
|
-
|
|
119
|
-
|
|
97
|
+
if (chains)
|
|
98
|
+
chains = chains.filter(function (i) { return i !== "ethereum"; });
|
|
99
|
+
return [4 /*yield*/, getBlock('ethereum')];
|
|
120
100
|
case 1:
|
|
121
|
-
lastBlockNumber = _a.sent();
|
|
122
|
-
return [4 /*yield*/, provider.getBlock(lastBlockNumber - 5)];
|
|
123
|
-
case 2:
|
|
124
101
|
block = _a.sent();
|
|
125
|
-
return [4 /*yield*/, getChainBlocks(block.timestamp)];
|
|
126
|
-
case
|
|
102
|
+
return [4 /*yield*/, getChainBlocks(block.timestamp, chains)];
|
|
103
|
+
case 2:
|
|
127
104
|
chainBlocks = _a.sent();
|
|
128
105
|
chainBlocks['ethereum'] = block.number;
|
|
129
106
|
return [2 /*return*/, {
|
|
@@ -136,3 +113,75 @@ function getCurrentBlocks() {
|
|
|
136
113
|
});
|
|
137
114
|
}
|
|
138
115
|
exports.getCurrentBlocks = getCurrentBlocks;
|
|
116
|
+
var blockCache = {
|
|
117
|
+
current: {}
|
|
118
|
+
};
|
|
119
|
+
function getBlock(chain, timestamp) {
|
|
120
|
+
if (timestamp === void 0) { timestamp = undefined; }
|
|
121
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
122
|
+
function getCurrentEthBlock() {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
+
var provider, lastBlockNumber;
|
|
125
|
+
return __generator(this, function (_a) {
|
|
126
|
+
switch (_a.label) {
|
|
127
|
+
case 0:
|
|
128
|
+
provider = general_1.getProvider("ethereum");
|
|
129
|
+
return [4 /*yield*/, provider.getBlockNumber()];
|
|
130
|
+
case 1:
|
|
131
|
+
lastBlockNumber = _a.sent();
|
|
132
|
+
return [2 /*return*/, provider.getBlock(lastBlockNumber - 5)]; // To allow indexers to catch up
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function _getBlock() {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
139
|
+
var i, block, e_1;
|
|
140
|
+
return __generator(this, function (_a) {
|
|
141
|
+
switch (_a.label) {
|
|
142
|
+
case 0:
|
|
143
|
+
i = 0;
|
|
144
|
+
_a.label = 1;
|
|
145
|
+
case 1:
|
|
146
|
+
if (!(i < blockRetries)) return [3 /*break*/, 6];
|
|
147
|
+
_a.label = 2;
|
|
148
|
+
case 2:
|
|
149
|
+
_a.trys.push([2, 4, , 5]);
|
|
150
|
+
return [4 /*yield*/, index_1.lookupBlock(timestamp, { chain: chain, })];
|
|
151
|
+
case 3:
|
|
152
|
+
block = (_a.sent()).block;
|
|
153
|
+
return [2 /*return*/, block];
|
|
154
|
+
case 4:
|
|
155
|
+
e_1 = _a.sent();
|
|
156
|
+
if (i === blockRetries - 1) {
|
|
157
|
+
throw e_1;
|
|
158
|
+
}
|
|
159
|
+
return [3 /*break*/, 5];
|
|
160
|
+
case 5:
|
|
161
|
+
i++;
|
|
162
|
+
return [3 /*break*/, 1];
|
|
163
|
+
case 6: return [2 /*return*/];
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return __generator(this, function (_a) {
|
|
169
|
+
if (!timestamp) {
|
|
170
|
+
if (chain !== 'ethereum')
|
|
171
|
+
throw new Error('Timestamp is missing!!!');
|
|
172
|
+
if (!blockCache.current)
|
|
173
|
+
blockCache.current = {};
|
|
174
|
+
if (!blockCache.current.ethereum) {
|
|
175
|
+
blockCache.current.ethereum = getCurrentEthBlock();
|
|
176
|
+
}
|
|
177
|
+
return [2 /*return*/, blockCache.current.ethereum];
|
|
178
|
+
}
|
|
179
|
+
if (!blockCache[timestamp])
|
|
180
|
+
blockCache[timestamp] = {};
|
|
181
|
+
if (!blockCache[timestamp][chain])
|
|
182
|
+
blockCache[timestamp][chain] = _getBlock();
|
|
183
|
+
return [2 /*return*/, blockCache[timestamp][chain]];
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
exports.getBlock = getBlock;
|
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*/, {
|