@defillama/sdk 4.0.70 → 4.0.71
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/Balances.d.ts +29 -0
- package/build/Balances.js +147 -0
- package/build/ChainApi.d.ts +7 -5
- package/build/ChainApi.js +10 -28
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/providers.json +1 -1
- package/build/util/computeTVL.d.ts +6 -0
- package/build/util/computeTVL.js +185 -0
- package/build/util/index.js +122 -55
- package/package.json +2 -2
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.getUnique = void 0;
|
|
43
|
+
var _1 = require(".");
|
|
44
|
+
var generalUtil_1 = require("../generalUtil");
|
|
45
|
+
var node_fetch_1 = __importDefault(require("node-fetch"));
|
|
46
|
+
var currentPriceCache = {};
|
|
47
|
+
var priceCaches = {};
|
|
48
|
+
var lastPriceUpdate = 0;
|
|
49
|
+
var confidenceThreshold = 0.5;
|
|
50
|
+
var priceUpdateTime = 1000 * 60 * 30; // 30 minutes
|
|
51
|
+
function computeTVL(balances, timestamp) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
+
var usdTokenBalances, usdTvl, keys, priceCache;
|
|
54
|
+
return __generator(this, function (_a) {
|
|
55
|
+
switch (_a.label) {
|
|
56
|
+
case 0:
|
|
57
|
+
usdTokenBalances = {};
|
|
58
|
+
usdTvl = 0;
|
|
59
|
+
keys = Object.keys(balances).filter(function (i) { return +balances[i] > 0; }).map(tokenToKey);
|
|
60
|
+
return [4 /*yield*/, updatePriceCache(keys, timestamp)];
|
|
61
|
+
case 1:
|
|
62
|
+
_a.sent();
|
|
63
|
+
priceCache = getPriceCache(timestamp);
|
|
64
|
+
Object.entries(balances).forEach(function (_a) {
|
|
65
|
+
var _b;
|
|
66
|
+
var token = _a[0], balance = _a[1];
|
|
67
|
+
if (+balance <= 0)
|
|
68
|
+
return;
|
|
69
|
+
var key = tokenToKey(token).toLowerCase();
|
|
70
|
+
var _c = (_b = priceCache[key]) !== null && _b !== void 0 ? _b : {}, price = _c.price, confidence = _c.confidence, _d = _c.decimals, decimals = _d === void 0 ? 0 : _d, _e = _c.symbol, symbol = _e === void 0 ? token : _e;
|
|
71
|
+
if (!price || confidence < confidenceThreshold)
|
|
72
|
+
return;
|
|
73
|
+
var value = +balance * price / (Math.pow(10, decimals));
|
|
74
|
+
usdTvl += value;
|
|
75
|
+
if (symbol === 'ETH')
|
|
76
|
+
symbol = 'WETH';
|
|
77
|
+
(0, generalUtil_1.sumSingleBalance)(usdTokenBalances, symbol, value);
|
|
78
|
+
});
|
|
79
|
+
return [2 /*return*/, { usdTvl: usdTvl, usdTokenBalances: usdTokenBalances }];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
exports.default = computeTVL;
|
|
85
|
+
function tokenToKey(token) {
|
|
86
|
+
if (token.startsWith("0x"))
|
|
87
|
+
token = "ethereum:".concat(token);
|
|
88
|
+
else if (!token.includes(':'))
|
|
89
|
+
token = "coingecko:".concat(token);
|
|
90
|
+
return token;
|
|
91
|
+
}
|
|
92
|
+
function updatePriceCache(keys, timestamp) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
94
|
+
function getPrices(keys) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
96
|
+
var coins_1, coins;
|
|
97
|
+
return __generator(this, function (_a) {
|
|
98
|
+
switch (_a.label) {
|
|
99
|
+
case 0:
|
|
100
|
+
if (!!timestamp) return [3 /*break*/, 2];
|
|
101
|
+
return [4 /*yield*/, (0, node_fetch_1.default)("https://coins.llama.fi/prices/current/".concat(keys.join(','))).then(function (res) { return res.json(); })];
|
|
102
|
+
case 1:
|
|
103
|
+
coins_1 = (_a.sent()).coins;
|
|
104
|
+
return [2 /*return*/, coins_1];
|
|
105
|
+
case 2: return [4 /*yield*/, (0, node_fetch_1.default)("https://coins.llama.fi/prices?source=internal", {
|
|
106
|
+
method: 'POST',
|
|
107
|
+
body: JSON.stringify({ coins: keys, timestamp: timestamp }),
|
|
108
|
+
headers: { "Content-Type": "application/json" },
|
|
109
|
+
}).then(function (res) { return res.json(); })];
|
|
110
|
+
case 3:
|
|
111
|
+
coins = (_a.sent()).coins;
|
|
112
|
+
return [2 /*return*/, coins];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
var pricesCache, missingKeys, chunks, _i, chunks_1, chunk, coins, _a, _b, _c, token, data;
|
|
118
|
+
return __generator(this, function (_d) {
|
|
119
|
+
switch (_d.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
if (isNearlyNow(timestamp))
|
|
122
|
+
timestamp = undefined;
|
|
123
|
+
keys = getUnique(keys);
|
|
124
|
+
pricesCache = getPriceCache(timestamp);
|
|
125
|
+
if (!timestamp && lastPriceUpdate && +Date.now() > lastPriceUpdate + priceUpdateTime) {
|
|
126
|
+
lastPriceUpdate = +Date.now();
|
|
127
|
+
Object.keys(pricesCache).forEach(function (key) { return delete pricesCache[key]; }); // clear cache
|
|
128
|
+
}
|
|
129
|
+
// hardcode cache for USDT
|
|
130
|
+
pricesCache['coingecko:tether'] = {
|
|
131
|
+
"price": 1,
|
|
132
|
+
"symbol": "USDT",
|
|
133
|
+
"confidence": 1
|
|
134
|
+
};
|
|
135
|
+
pricesCache['ethereum:0xdac17f958d2ee523a2206206994597c13d831ec7'] = {
|
|
136
|
+
"price": 1,
|
|
137
|
+
"symbol": "USDT",
|
|
138
|
+
"decimals": 6,
|
|
139
|
+
"confidence": 1
|
|
140
|
+
};
|
|
141
|
+
missingKeys = keys.filter(function (key) { return !pricesCache[key.toLowerCase()]; });
|
|
142
|
+
chunks = (0, _1.sliceIntoChunks)(missingKeys, 100);
|
|
143
|
+
_i = 0, chunks_1 = chunks;
|
|
144
|
+
_d.label = 1;
|
|
145
|
+
case 1:
|
|
146
|
+
if (!(_i < chunks_1.length)) return [3 /*break*/, 4];
|
|
147
|
+
chunk = chunks_1[_i];
|
|
148
|
+
return [4 /*yield*/, getPrices(chunk)];
|
|
149
|
+
case 2:
|
|
150
|
+
coins = _d.sent();
|
|
151
|
+
for (_a = 0, _b = Object.entries(coins); _a < _b.length; _a++) {
|
|
152
|
+
_c = _b[_a], token = _c[0], data = _c[1];
|
|
153
|
+
pricesCache[token.toLowerCase()] = data;
|
|
154
|
+
}
|
|
155
|
+
chunk.map(function (i) { return i.toLowerCase(); }).filter(function (i) { return !pricesCache[i]; }).forEach(function (i) { return pricesCache[i] = {}; });
|
|
156
|
+
_d.label = 3;
|
|
157
|
+
case 3:
|
|
158
|
+
_i++;
|
|
159
|
+
return [3 /*break*/, 1];
|
|
160
|
+
case 4: return [2 /*return*/];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function getPriceCache(timestamp) {
|
|
166
|
+
if (timestamp)
|
|
167
|
+
timestamp = getClosestHourlyTimestamp(timestamp);
|
|
168
|
+
if (timestamp && !priceCaches[timestamp])
|
|
169
|
+
priceCaches[timestamp] = {};
|
|
170
|
+
return timestamp ? priceCaches[timestamp] : currentPriceCache;
|
|
171
|
+
}
|
|
172
|
+
function getUnique(addresses) {
|
|
173
|
+
var set = {};
|
|
174
|
+
addresses.forEach(function (i) { return set[i] = true; });
|
|
175
|
+
return Object.keys(set);
|
|
176
|
+
}
|
|
177
|
+
exports.getUnique = getUnique;
|
|
178
|
+
function isNearlyNow(timestamp) {
|
|
179
|
+
if (!timestamp)
|
|
180
|
+
return true;
|
|
181
|
+
return Math.abs(timestamp - +Date.now() / 1000) < 60 * 60;
|
|
182
|
+
}
|
|
183
|
+
function getClosestHourlyTimestamp(timestamp) {
|
|
184
|
+
return Math.floor(timestamp / 3600) * 3600;
|
|
185
|
+
}
|
package/build/util/index.js
CHANGED
|
@@ -135,78 +135,155 @@ var intialBlocks = {
|
|
|
135
135
|
terra: 4724001,
|
|
136
136
|
crab: 4969901
|
|
137
137
|
};
|
|
138
|
+
var blockscoutEndpoints = {
|
|
139
|
+
celo: 'https://explorer.celo.org',
|
|
140
|
+
kava: 'https://explorer.kava.io',
|
|
141
|
+
onus: 'https://explorer.onuschain.io',
|
|
142
|
+
base: 'https://base.blockscout.com',
|
|
143
|
+
scroll: 'https://blockscout.scroll.io',
|
|
144
|
+
};
|
|
145
|
+
function getBlockscoutBlock(timestamp, chain) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
147
|
+
var api, res, data;
|
|
148
|
+
return __generator(this, function (_a) {
|
|
149
|
+
switch (_a.label) {
|
|
150
|
+
case 0:
|
|
151
|
+
api = "".concat(blockscoutEndpoints[chain], "/api?module=block&action=getblocknobytime×tamp=").concat(timestamp, "&closest=before");
|
|
152
|
+
return [4 /*yield*/, (0, node_fetch_1.default)(api)];
|
|
153
|
+
case 1:
|
|
154
|
+
res = _a.sent();
|
|
155
|
+
return [4 /*yield*/, res.json()];
|
|
156
|
+
case 2:
|
|
157
|
+
data = _a.sent();
|
|
158
|
+
if (data.status !== '1')
|
|
159
|
+
throw new Error(data.message);
|
|
160
|
+
return [2 /*return*/, {
|
|
161
|
+
timestamp: timestamp,
|
|
162
|
+
block: +data.result.blockNumber
|
|
163
|
+
}];
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
var blockTimeCache = {};
|
|
138
169
|
function lookupBlock(timestamp, extraParams) {
|
|
139
170
|
var _a, _b, _c;
|
|
140
171
|
if (extraParams === void 0) { extraParams = {}; }
|
|
141
172
|
return __awaiter(this, void 0, void 0, function () {
|
|
142
|
-
|
|
173
|
+
function updateBlock(blocks) {
|
|
174
|
+
if (blocks === void 0) { blocks = []; }
|
|
175
|
+
blocks.forEach(addBlockToCache);
|
|
176
|
+
var getPrecision = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
|
|
177
|
+
blocks.push(highBlock, lowBlock);
|
|
178
|
+
blocks.sort(function (a, b) { return getPrecision(a) - getPrecision(b); });
|
|
179
|
+
block = blocks[0];
|
|
180
|
+
// find the closest upper and lower bound between 4 points
|
|
181
|
+
lowBlock = blocks.filter(function (i) { return i.timestamp < timestamp; }).reduce(function (lowestBlock, block) { return (timestamp - lowestBlock.timestamp) < (timestamp - block.timestamp) ? lowestBlock : block; });
|
|
182
|
+
highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
|
|
183
|
+
imprecision = getPrecision(block);
|
|
184
|
+
blockImprecision = highBlock.number - lowBlock.number;
|
|
185
|
+
// debugLog(`chain: ${chain} block: ${block.number} #calls: ${i} imprecision: ${Number((imprecision)/60).toFixed(2)} (min) block diff: ${blockImprecision} Time Taken: ${Number((Date.now()-time)/1000).toFixed(2)} (in sec)`)
|
|
186
|
+
}
|
|
187
|
+
function getChainBlockTimeCache(chain) {
|
|
188
|
+
if (!blockTimeCache[chain])
|
|
189
|
+
blockTimeCache[chain] = {};
|
|
190
|
+
return blockTimeCache[chain];
|
|
191
|
+
}
|
|
192
|
+
function addBlockToCache(block) {
|
|
193
|
+
getChainBlockTimeCache(chain)[block.number] = block;
|
|
194
|
+
}
|
|
195
|
+
function getLowBlock() {
|
|
196
|
+
return Object.values(getChainBlockTimeCache(chain))
|
|
197
|
+
.filter(function (i) { return i.timestamp < timestamp; })
|
|
198
|
+
.sort(function (a, b) { return b.timestamp - a.timestamp; })[0];
|
|
199
|
+
}
|
|
200
|
+
function getHighBlock() {
|
|
201
|
+
return Object.values(getChainBlockTimeCache(chain))
|
|
202
|
+
.filter(function (i) { return i.timestamp > timestamp; })
|
|
203
|
+
.sort(function (a, b) { return a.timestamp - b.timestamp; })[0];
|
|
204
|
+
}
|
|
205
|
+
var chain, response, e_1, api, res, data, low, lowBlock, highBlock, block, i, time, allowedTimeRange, acceptableBlockImprecision, blockImprecision, imprecision, firstBlock, lastBlock, provider, firstBlockNum, blockDiff, timeDiff, avgBlockTime, closeBlock, midBlock, blocks, e_2;
|
|
143
206
|
var _d;
|
|
144
207
|
return __generator(this, function (_e) {
|
|
145
208
|
switch (_e.label) {
|
|
146
209
|
case 0:
|
|
147
210
|
chain = (_a = extraParams === null || extraParams === void 0 ? void 0 : extraParams.chain) !== null && _a !== void 0 ? _a : "ethereum";
|
|
148
|
-
if (!
|
|
149
|
-
|
|
150
|
-
return [4 /*yield*/, (0, node_fetch_1.default)(api)];
|
|
211
|
+
if (!blockscoutEndpoints[chain]) return [3 /*break*/, 4];
|
|
212
|
+
_e.label = 1;
|
|
151
213
|
case 1:
|
|
214
|
+
_e.trys.push([1, 3, , 4]);
|
|
215
|
+
return [4 /*yield*/, getBlockscoutBlock(timestamp, chain)];
|
|
216
|
+
case 2:
|
|
217
|
+
response = _e.sent();
|
|
218
|
+
return [2 /*return*/, response];
|
|
219
|
+
case 3:
|
|
220
|
+
e_1 = _e.sent();
|
|
221
|
+
(0, debugLog_1.debugLog)('error fetching block from blockscout', e_1);
|
|
222
|
+
return [3 /*break*/, 4];
|
|
223
|
+
case 4:
|
|
224
|
+
if (!(chain === 'waves')) return [3 /*break*/, 7];
|
|
225
|
+
api = "https://nodes.wavesnodes.com/blocks/heightByTimestamp/".concat(timestamp);
|
|
226
|
+
return [4 /*yield*/, (0, node_fetch_1.default)(api)];
|
|
227
|
+
case 5:
|
|
152
228
|
res = _e.sent();
|
|
153
229
|
return [4 /*yield*/, res.json()];
|
|
154
|
-
case
|
|
230
|
+
case 6:
|
|
155
231
|
data = _e.sent();
|
|
156
232
|
return [2 /*return*/, {
|
|
157
233
|
timestamp: timestamp,
|
|
158
|
-
block: +data.
|
|
234
|
+
block: +data.height
|
|
159
235
|
}];
|
|
160
|
-
case
|
|
236
|
+
case 7:
|
|
161
237
|
low = (_b = intialBlocks[chain]) !== null && _b !== void 0 ? _b : 100;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
238
|
+
lowBlock = getLowBlock();
|
|
239
|
+
highBlock = getHighBlock();
|
|
240
|
+
i = 0;
|
|
241
|
+
time = Date.now();
|
|
242
|
+
allowedTimeRange = 15 * 60 // how much imprecision is allowed (15 minutes now)
|
|
243
|
+
;
|
|
244
|
+
acceptableBlockImprecision = chain === 'ethereum' ? 20 : 200;
|
|
245
|
+
_e.label = 8;
|
|
246
|
+
case 8:
|
|
247
|
+
_e.trys.push([8, 17, , 18]);
|
|
165
248
|
firstBlock = void 0, lastBlock = void 0;
|
|
166
249
|
provider = getExtraProvider(chain);
|
|
167
|
-
if (!['evmos'].includes(chain)) return [3 /*break*/,
|
|
250
|
+
if (!['evmos'].includes(chain)) return [3 /*break*/, 11];
|
|
168
251
|
return [4 /*yield*/, getBlock(provider, "latest", chain)];
|
|
169
|
-
case
|
|
252
|
+
case 9:
|
|
170
253
|
lastBlock = _e.sent();
|
|
171
254
|
firstBlockNum = lastBlock.number;
|
|
172
255
|
switch (chain) {
|
|
173
256
|
default: firstBlockNum -= 2 * 1e5; // evmos hold only the last 200k block data
|
|
174
257
|
}
|
|
175
258
|
return [4 /*yield*/, getBlock(provider, firstBlockNum, chain)];
|
|
176
|
-
case
|
|
259
|
+
case 10:
|
|
177
260
|
firstBlock = _e.sent();
|
|
178
|
-
return [3 /*break*/,
|
|
179
|
-
case
|
|
261
|
+
return [3 /*break*/, 13];
|
|
262
|
+
case 11: return [4 /*yield*/, Promise.all([
|
|
180
263
|
getBlock(provider, "latest", chain),
|
|
181
|
-
getBlock(provider, low, chain),
|
|
264
|
+
lowBlock ? lowBlock : getBlock(provider, low, chain),
|
|
182
265
|
])];
|
|
183
|
-
case
|
|
266
|
+
case 12:
|
|
184
267
|
_d = _e.sent(), lastBlock = _d[0], firstBlock = _d[1];
|
|
185
|
-
_e.label =
|
|
186
|
-
case
|
|
268
|
+
_e.label = 13;
|
|
269
|
+
case 13:
|
|
187
270
|
lowBlock = firstBlock;
|
|
188
|
-
highBlock
|
|
271
|
+
if (!highBlock)
|
|
272
|
+
highBlock = lastBlock;
|
|
189
273
|
if (lastBlock.timestamp - timestamp < -30 * 60) {
|
|
190
274
|
throw new Error("Last block of chain \"".concat(chain, "\" is further than 30 minutes into the past. Provider is \"").concat((_c = provider === null || provider === void 0 ? void 0 : provider.connection) === null || _c === void 0 ? void 0 : _c.url, "\""));
|
|
191
275
|
}
|
|
192
|
-
if (Math.abs(
|
|
276
|
+
if (Math.abs(highBlock.timestamp - timestamp) < 60 * 30) {
|
|
193
277
|
// Short-circuit in case we are trying to get the current block
|
|
194
278
|
return [2 /*return*/, {
|
|
195
|
-
block:
|
|
196
|
-
timestamp:
|
|
279
|
+
block: highBlock.number,
|
|
280
|
+
timestamp: highBlock.timestamp
|
|
197
281
|
}];
|
|
198
282
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
;
|
|
204
|
-
acceptableBlockImprecision = chain === 'ethereum' ? 20 : 200;
|
|
205
|
-
blockImprecision = void 0;
|
|
206
|
-
imprecision = void 0;
|
|
207
|
-
getPrecision_1 = function (block) { return block.timestamp - timestamp > 0 ? block.timestamp - timestamp : timestamp - block.timestamp; };
|
|
208
|
-
_e.label = 10;
|
|
209
|
-
case 10:
|
|
283
|
+
updateBlock();
|
|
284
|
+
_e.label = 14;
|
|
285
|
+
case 14:
|
|
286
|
+
if (!(imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision)) return [3 /*break*/, 16];
|
|
210
287
|
++i;
|
|
211
288
|
blockDiff = highBlock.number - lowBlock.number;
|
|
212
289
|
timeDiff = highBlock.timestamp - lowBlock.timestamp;
|
|
@@ -219,21 +296,11 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
219
296
|
getBlock(provider, closeBlock, chain),
|
|
220
297
|
getBlock(provider, midBlock, chain),
|
|
221
298
|
])];
|
|
222
|
-
case
|
|
299
|
+
case 15:
|
|
223
300
|
blocks = _e.sent();
|
|
224
|
-
blocks
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// find the closest upper and lower bound between 4 points
|
|
228
|
-
lowBlock = blocks.filter(function (i) { return i.timestamp < timestamp; }).reduce(function (lowestBlock, block) { return (timestamp - lowestBlock.timestamp) < (timestamp - block.timestamp) ? lowestBlock : block; });
|
|
229
|
-
highBlock = blocks.filter(function (i) { return i.timestamp > timestamp; }).reduce(function (highestBlock, block) { return (highestBlock.timestamp - timestamp) < (block.timestamp - timestamp) ? highestBlock : block; });
|
|
230
|
-
imprecision = getPrecision_1(block);
|
|
231
|
-
blockImprecision = highBlock.number - lowBlock.number;
|
|
232
|
-
_e.label = 12;
|
|
233
|
-
case 12:
|
|
234
|
-
if (imprecision > allowedTimeRange && blockImprecision > acceptableBlockImprecision) return [3 /*break*/, 10];
|
|
235
|
-
_e.label = 13;
|
|
236
|
-
case 13:
|
|
301
|
+
updateBlock(blocks);
|
|
302
|
+
return [3 /*break*/, 14];
|
|
303
|
+
case 16:
|
|
237
304
|
(0, debugLog_1.debugLog)("chain: ".concat(chain, " block: ").concat(block.number, " #calls: ").concat(i, " imprecision: ").concat(Number((imprecision) / 60).toFixed(2), " (min) Time Taken: ").concat(Number((Date.now() - time) / 1000).toFixed(2), " (in sec)"));
|
|
238
305
|
if (chain !== "bsc" && // this check is there because bsc halted the chain for few days
|
|
239
306
|
Math.abs(block.timestamp - timestamp) > 3600) {
|
|
@@ -243,11 +310,11 @@ function lookupBlock(timestamp, extraParams) {
|
|
|
243
310
|
block: block.number,
|
|
244
311
|
timestamp: block.timestamp
|
|
245
312
|
}];
|
|
246
|
-
case
|
|
247
|
-
|
|
248
|
-
|
|
313
|
+
case 17:
|
|
314
|
+
e_2 = _e.sent();
|
|
315
|
+
(0, debugLog_1.debugLog)(e_2);
|
|
249
316
|
throw new Error("Couldn't find block height for chain ".concat(chain, ", RPC node rugged"));
|
|
250
|
-
case
|
|
317
|
+
case 18: return [2 /*return*/];
|
|
251
318
|
}
|
|
252
319
|
});
|
|
253
320
|
});
|
|
@@ -257,7 +324,7 @@ exports.lookupBlock = lookupBlock;
|
|
|
257
324
|
function getLogs(params) {
|
|
258
325
|
var _a;
|
|
259
326
|
return __awaiter(this, void 0, void 0, function () {
|
|
260
|
-
var filter, logs, blockSpread, currentBlock, nextBlock, partLogs,
|
|
327
|
+
var filter, logs, blockSpread, currentBlock, nextBlock, partLogs, e_3;
|
|
261
328
|
return __generator(this, function (_b) {
|
|
262
329
|
switch (_b.label) {
|
|
263
330
|
case 0:
|
|
@@ -287,14 +354,14 @@ function getLogs(params) {
|
|
|
287
354
|
currentBlock = nextBlock;
|
|
288
355
|
return [3 /*break*/, 5];
|
|
289
356
|
case 4:
|
|
290
|
-
|
|
357
|
+
e_3 = _b.sent();
|
|
291
358
|
if (blockSpread >= 2e3) {
|
|
292
359
|
// We got too many results
|
|
293
360
|
// We could chop it up into 2K block spreads as that is guaranteed to always return but then we'll have to make a lot of queries (easily >1000), so instead we'll keep dividing the block spread by two until we make it
|
|
294
361
|
blockSpread = Math.floor(blockSpread / 2);
|
|
295
362
|
}
|
|
296
363
|
else {
|
|
297
|
-
throw
|
|
364
|
+
throw e_3;
|
|
298
365
|
}
|
|
299
366
|
return [3 /*break*/, 5];
|
|
300
367
|
case 5: return [3 /*break*/, 1];
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defillama/sdk",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.71",
|
|
4
4
|
"description": "SDK for calculating TVL",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"start": "npm run build && node build/index.js",
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"watch": "tsc --watch",
|
|
10
|
-
"test": "jest",
|
|
10
|
+
"test": "LLAMA_DEBUG_MODE='' SDK_DEBUG='' jest",
|
|
11
11
|
"update-providers": "ts-node src/util/updateProviderList.ts",
|
|
12
12
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
13
13
|
"prepublishOnly": "npm run build"
|