@defillama/sdk 5.0.240 → 5.0.244
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 +139 -0
- package/build/Balances.js +363 -0
- package/build/ChainApi.d.ts +142 -0
- package/build/ChainApi.js +310 -0
- package/build/abi/abi2.d.ts +5 -0
- package/build/abi/abi2.js +215 -0
- package/build/abi/cachedABIs.d.ts +4 -0
- package/build/abi/cachedABIs.js +8 -0
- package/build/abi/convertResults.d.ts +5 -0
- package/build/abi/convertResults.js +60 -0
- package/build/abi/index.d.ts +31 -0
- package/build/abi/index.js +327 -0
- package/build/abi/multicall.d.ts +23 -0
- package/build/abi/multicall.js +383 -0
- package/build/abi/multicall3.d.ts +7 -0
- package/build/abi/multicall3.js +405 -0
- package/build/abi/rpcCall.d.ts +4 -0
- package/build/abi/rpcCall.js +62 -0
- package/build/abi/tron.d.ts +33 -0
- package/build/abi/tron.js +118 -0
- package/build/api.d.ts +10 -0
- package/build/api.js +45 -0
- package/build/api2.d.ts +5 -0
- package/build/api2.js +41 -0
- package/build/chains/algorand.d.ts +277 -0
- package/build/chains/algorand.js +618 -0
- package/build/chains/aptos.d.ts +269 -0
- package/build/chains/aptos.js +700 -0
- package/build/chains/cardano.d.ts +242 -0
- package/build/chains/cardano.js +407 -0
- package/build/chains/cosmos.d.ts +252 -0
- package/build/chains/cosmos.js +623 -0
- package/build/chains/icp.d.ts +235 -0
- package/build/chains/icp.js +1040 -0
- package/build/chains/index.d.ts +23 -0
- package/build/chains/index.js +59 -0
- package/build/chains/near.d.ts +192 -0
- package/build/chains/near.js +446 -0
- package/build/chains/rpc.d.ts +125 -0
- package/build/chains/rpc.js +332 -0
- package/build/chains/starknet.d.ts +205 -0
- package/build/chains/starknet.js +886 -0
- package/build/chains/stellar.d.ts +410 -0
- package/build/chains/stellar.js +1082 -0
- package/build/chains/substrate.d.ts +333 -0
- package/build/chains/substrate.js +1042 -0
- package/build/chains/sui.d.ts +392 -0
- package/build/chains/sui.js +1213 -0
- package/build/chains/svm.d.ts +340 -0
- package/build/chains/svm.js +764 -0
- package/build/chains/tezos.d.ts +225 -0
- package/build/chains/tezos.js +412 -0
- package/build/chains/ton.d.ts +353 -0
- package/build/chains/ton.js +788 -0
- package/build/chains/tron.d.ts +45 -0
- package/build/chains/tron.js +110 -0
- package/build/chains/utxo.d.ts +210 -0
- package/build/chains/utxo.js +678 -0
- package/build/chains/xrpl.d.ts +179 -0
- package/build/chains/xrpl.js +467 -0
- package/build/computeTVL/blocks.d.ts +1 -0
- package/build/computeTVL/blocks.js +12 -0
- package/build/computeTVL/humanizeNumber.d.ts +1 -0
- package/build/computeTVL/humanizeNumber.js +19 -0
- package/build/erc20/index.d.ts +31 -0
- package/build/erc20/index.js +56 -0
- package/build/eth/index.d.ts +22 -0
- package/build/eth/index.js +118 -0
- package/build/general.d.ts +5 -0
- package/build/general.js +20 -0
- package/build/generalUtil.d.ts +8 -0
- package/build/generalUtil.js +73 -0
- package/build/index.d.ts +33 -0
- package/build/index.js +75 -0
- package/build/providers.json +1 -0
- package/build/types.d.ts +76 -0
- package/build/types.js +2 -0
- package/build/util/LlamaProvider.d.ts +46 -0
- package/build/util/LlamaProvider.js +343 -0
- package/build/util/blocks.d.ts +36 -0
- package/build/util/blocks.js +500 -0
- package/build/util/cache.d.ts +45 -0
- package/build/util/cache.js +309 -0
- package/build/util/callStats.d.ts +74 -0
- package/build/util/callStats.js +385 -0
- package/build/util/chainUtils/data.json +1459 -0
- package/build/util/chainUtils/index.d.ts +11 -0
- package/build/util/chainUtils/index.js +136 -0
- package/build/util/coins.d.ts +19 -0
- package/build/util/coins.js +124 -0
- package/build/util/common.d.ts +34 -0
- package/build/util/common.js +376 -0
- package/build/util/computeTVL.d.ts +19 -0
- package/build/util/computeTVL.js +102 -0
- package/build/util/cosmos.d.ts +1 -0
- package/build/util/cosmos.js +8 -0
- package/build/util/debugLog.d.ts +4 -0
- package/build/util/debugLog.js +18 -0
- package/build/util/discord.d.ts +1 -0
- package/build/util/discord.js +33 -0
- package/build/util/elastic.d.ts +43 -0
- package/build/util/elastic.js +113 -0
- package/build/util/env.d.ts +20 -0
- package/build/util/env.js +176 -0
- package/build/util/graph.d.ts +8 -0
- package/build/util/graph.js +50 -0
- package/build/util/index.d.ts +32 -0
- package/build/util/index.js +227 -0
- package/build/util/indexer.compatibility.d.ts +6 -0
- package/build/util/indexer.compatibility.js +64 -0
- package/build/util/indexer.d.ts +92 -0
- package/build/util/indexer.js +854 -0
- package/build/util/indexerSql.d.ts +9 -0
- package/build/util/indexerSql.js +102 -0
- package/build/util/internal-cache.d.ts +11 -0
- package/build/util/internal-cache.js +77 -0
- package/build/util/logs.d.ts +58 -0
- package/build/util/logs.decode.shared.d.ts +8 -0
- package/build/util/logs.decode.shared.js +289 -0
- package/build/util/logs.js +432 -0
- package/build/util/promisePool.d.ts +8 -0
- package/build/util/promisePool.js +31 -0
- package/build/util/r2.d.ts +4 -0
- package/build/util/r2.js +52 -0
- package/build/util/removeRedundantRPCs.d.ts +1 -0
- package/build/util/removeRedundantRPCs.js +49 -0
- package/build/util/transactions.d.ts +35 -0
- package/build/util/transactions.js +22 -0
- package/build/util/updateChainLabels.d.ts +1 -0
- package/build/util/updateChainLabels.js +5 -0
- package/build/util/updateProviderList.d.ts +1 -0
- package/build/util/updateProviderList.js +275 -0
- package/package.json +3 -2
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ONE_WEEK = exports.currentVersion = void 0;
|
|
4
|
+
exports.readCache = readCache;
|
|
5
|
+
exports.writeCache = writeCache;
|
|
6
|
+
exports.parseCache = parseCache;
|
|
7
|
+
exports.compressCache = compressCache;
|
|
8
|
+
exports.getTempLocalCache = getTempLocalCache;
|
|
9
|
+
exports.deleteCache = deleteCache;
|
|
10
|
+
exports.readExpiringJsonCache = readExpiringJsonCache;
|
|
11
|
+
exports.writeExpiringJsonCache = writeExpiringJsonCache;
|
|
12
|
+
exports.cachedFetch = cachedFetch;
|
|
13
|
+
const debugLog_1 = require("./debugLog");
|
|
14
|
+
const r2_1 = require("./r2");
|
|
15
|
+
const zlib_1 = require("zlib");
|
|
16
|
+
const util_1 = require("util");
|
|
17
|
+
const env_1 = require("./env");
|
|
18
|
+
const common_1 = require("./common");
|
|
19
|
+
const brotliOptions = {
|
|
20
|
+
[zlib_1.constants.BROTLI_PARAM_MODE]: zlib_1.constants.BROTLI_MODE_TEXT,
|
|
21
|
+
[zlib_1.constants.BROTLI_PARAM_QUALITY]: zlib_1.constants.BROTLI_MAX_QUALITY,
|
|
22
|
+
};
|
|
23
|
+
function zipAsync(data) {
|
|
24
|
+
return (0, util_1.promisify)(zlib_1.brotliCompress)(data, brotliOptions);
|
|
25
|
+
}
|
|
26
|
+
function unzipAsync(data) {
|
|
27
|
+
return (0, util_1.promisify)(zlib_1.brotliDecompress)(data, brotliOptions).then(r => r.toString());
|
|
28
|
+
}
|
|
29
|
+
const _fs = require('fs');
|
|
30
|
+
const fs = _fs.promises;
|
|
31
|
+
const path = require('path');
|
|
32
|
+
const foldersCreated = {};
|
|
33
|
+
exports.currentVersion = 'zlib-1.0';
|
|
34
|
+
function getCacheRootFolder() {
|
|
35
|
+
const defaultCacheRootFolder = path.join(__dirname, 'local_cache');
|
|
36
|
+
return path.join((0, env_1.getEnvCacheFolder)(defaultCacheRootFolder), exports.currentVersion);
|
|
37
|
+
}
|
|
38
|
+
function getFilePath(file) {
|
|
39
|
+
return path.join(getCacheRootFolder(), file);
|
|
40
|
+
}
|
|
41
|
+
async function createSubPath(folderPath) {
|
|
42
|
+
const datRoot = getCacheRootFolder();
|
|
43
|
+
folderPath = folderPath.replace(datRoot, '');
|
|
44
|
+
if (foldersCreated[folderPath])
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
await fs.mkdir(path.join(datRoot, folderPath), { recursive: true });
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// sdk.log('Error creating folder path:', error)
|
|
51
|
+
}
|
|
52
|
+
foldersCreated[folderPath] = true;
|
|
53
|
+
}
|
|
54
|
+
const ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
|
|
55
|
+
async function readCache(file, options = {}) {
|
|
56
|
+
if (options.skipCompression)
|
|
57
|
+
file = `${file}-uncompressed`;
|
|
58
|
+
try {
|
|
59
|
+
const data = await readFile();
|
|
60
|
+
return await parseCache(data, options);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
// debugLog('Error reading cache:', error)
|
|
64
|
+
return {};
|
|
65
|
+
}
|
|
66
|
+
async function readFile() {
|
|
67
|
+
var _a;
|
|
68
|
+
try {
|
|
69
|
+
const data = await readDataFromLocal((_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false);
|
|
70
|
+
return data;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
// debugLog('Error reading cache:', error)
|
|
74
|
+
return readDataFromR2();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function readDataFromLocal(readFromR2Cache) {
|
|
78
|
+
const filePath = getFilePath(file);
|
|
79
|
+
if (options.checkIfRecent) {
|
|
80
|
+
const stats = await fs.stat(filePath);
|
|
81
|
+
if (Date.now() - stats.mtimeMs > ONE_DAY_IN_MS)
|
|
82
|
+
throw new Error('File too old, read from R2 instead');
|
|
83
|
+
}
|
|
84
|
+
if (readFromR2Cache)
|
|
85
|
+
throw new Error('Read from R2 cache');
|
|
86
|
+
const data = await fs.readFile(filePath);
|
|
87
|
+
return data;
|
|
88
|
+
}
|
|
89
|
+
async function readDataFromR2() {
|
|
90
|
+
var _a;
|
|
91
|
+
if (options.skipR2Cache)
|
|
92
|
+
return;
|
|
93
|
+
try {
|
|
94
|
+
const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file, { throwErrorOnFail: (_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false });
|
|
95
|
+
if (r2Data) {
|
|
96
|
+
// `file` already carries the `-uncompressed` suffix (added above), so omit skipCompression here to avoid writeCache appending it again
|
|
97
|
+
await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true });
|
|
98
|
+
return r2Data;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (options.readFromR2Cache) {
|
|
103
|
+
// if we were forced to read from R2 cache, try reading from local cache if R2 fails
|
|
104
|
+
try {
|
|
105
|
+
const data = await readDataFromLocal(false);
|
|
106
|
+
return data;
|
|
107
|
+
}
|
|
108
|
+
catch (_e) { }
|
|
109
|
+
}
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async function writeCache(file, data, options = {}) {
|
|
115
|
+
try {
|
|
116
|
+
const fileData = options.alreadyCompressed ? data : await compressCache(data, options);
|
|
117
|
+
if (!data || (typeof data === 'string' && data.length < 20) || fileData.length < 20) {
|
|
118
|
+
(0, debugLog_1.debugLog)('Data too small to cache: ', file);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!options.skipCompression && isSameData(data, await readCache(file, { skipR2Cache: true, skipCompression: options.skipCompression, })))
|
|
122
|
+
return;
|
|
123
|
+
if (options.skipCompression)
|
|
124
|
+
file = `${file}-uncompressed`;
|
|
125
|
+
const filePath = getFilePath(file);
|
|
126
|
+
await createSubPath(path.dirname(filePath));
|
|
127
|
+
await fs.writeFile(filePath, fileData);
|
|
128
|
+
if (!options.skipR2CacheWrite) {
|
|
129
|
+
await (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData);
|
|
130
|
+
}
|
|
131
|
+
return fileData;
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (typeof data === 'object' && data !== null && Object.keys(data).length === 0)
|
|
135
|
+
return;
|
|
136
|
+
(0, debugLog_1.debugLog)('Error writing cache:', error);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async function parseCache(dataString, options = {}) {
|
|
140
|
+
var _a;
|
|
141
|
+
let dataBuffer;
|
|
142
|
+
if (typeof dataString === 'string')
|
|
143
|
+
dataBuffer = Buffer.from(dataString, 'base64');
|
|
144
|
+
else {
|
|
145
|
+
dataBuffer = dataString;
|
|
146
|
+
dataString = dataBuffer.toString('utf8');
|
|
147
|
+
}
|
|
148
|
+
let decompressed;
|
|
149
|
+
let _unzipAsync = (str) => options.skipCompression ? dataString : unzipAsync(str); // if skipCompression is true, we don't decompress the data
|
|
150
|
+
try {
|
|
151
|
+
decompressed = await _unzipAsync((_a = dataBuffer) !== null && _a !== void 0 ? _a : dataString);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
// dataString = dataString.toString('utf8')
|
|
155
|
+
const convertedDataString = Buffer.from(dataString, 'base64');
|
|
156
|
+
decompressed = await _unzipAsync(convertedDataString);
|
|
157
|
+
}
|
|
158
|
+
return JSON.parse(decompressed).llamaWrapped;
|
|
159
|
+
}
|
|
160
|
+
async function compressCache(data, options = {}) {
|
|
161
|
+
const dataString = JSON.stringify({ version: exports.currentVersion, llamaWrapped: data });
|
|
162
|
+
if (options.skipCompression)
|
|
163
|
+
return dataString;
|
|
164
|
+
const comressedCache = await zipAsync(dataString);
|
|
165
|
+
return comressedCache;
|
|
166
|
+
}
|
|
167
|
+
function isSameData(data1, data2) {
|
|
168
|
+
return JSON.stringify(data1) === JSON.stringify(data2);
|
|
169
|
+
}
|
|
170
|
+
exports.ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
|
|
171
|
+
function getTempLocalCache({ file, defaultData = {}, clearAfter = exports.ONE_WEEK, returnWithSaveFunction = false }) {
|
|
172
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
173
|
+
let emptyFile = { created: now, data: defaultData };
|
|
174
|
+
let fileData = emptyFile;
|
|
175
|
+
const filePath = getFilePath(file);
|
|
176
|
+
let saveCacheFlag = false; // to ensure that we save only once on exit
|
|
177
|
+
createSubPath(path.dirname(filePath)); // create folder if not exists, this is async but we don't care
|
|
178
|
+
try {
|
|
179
|
+
const data = _fs.readFileSync(filePath);
|
|
180
|
+
fileData = JSON.parse(data.toString());
|
|
181
|
+
if (now - fileData.created > clearAfter)
|
|
182
|
+
fileData = emptyFile; // clear cache
|
|
183
|
+
}
|
|
184
|
+
catch (error) { }
|
|
185
|
+
// save cache on exit
|
|
186
|
+
process.on('exit', _save);
|
|
187
|
+
process.on('SIGTERM', _save);
|
|
188
|
+
process.on('SIGINT', _save);
|
|
189
|
+
if (returnWithSaveFunction) {
|
|
190
|
+
return {
|
|
191
|
+
data: fileData.data,
|
|
192
|
+
saveCacheFile: () => saveCache(true)
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
return fileData.data;
|
|
196
|
+
function _save() {
|
|
197
|
+
saveCache(false);
|
|
198
|
+
}
|
|
199
|
+
function saveCache(skipCacheFlagCheck = false) {
|
|
200
|
+
if (!skipCacheFlagCheck) {
|
|
201
|
+
if (saveCacheFlag)
|
|
202
|
+
return;
|
|
203
|
+
saveCacheFlag = true;
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
// debugLog('Saving cache to file:', filePath)
|
|
207
|
+
removePromisesAndFunctions(fileData);
|
|
208
|
+
_fs.writeFileSync(filePath, JSON.stringify(fileData));
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
try {
|
|
212
|
+
(0, debugLog_1.debugLog)('Error saving cache:', error === null || error === void 0 ? void 0 : error.toString());
|
|
213
|
+
}
|
|
214
|
+
catch (error) { }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function removePromisesAndFunctions(data) {
|
|
218
|
+
if (typeof data === 'object') {
|
|
219
|
+
for (const key in data) {
|
|
220
|
+
if (typeof data[key] === 'function')
|
|
221
|
+
delete data[key];
|
|
222
|
+
if (data[key] instanceof Promise)
|
|
223
|
+
delete data[key];
|
|
224
|
+
else
|
|
225
|
+
removePromisesAndFunctions(data[key]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// this removes data only from the file cache, not from the R2 cache
|
|
231
|
+
async function deleteCache(file) {
|
|
232
|
+
const filePath = getFilePath(file);
|
|
233
|
+
await fs.unlink(filePath).catch(() => {
|
|
234
|
+
// ignore errors
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
async function readExpiringJsonCache(file) {
|
|
238
|
+
const options = { readFromR2Cache: false, skipR2Cache: true, skipCompression: true };
|
|
239
|
+
file = 'expiring/' + file;
|
|
240
|
+
const data = await readCache(file, options);
|
|
241
|
+
if (!data || Object.keys(data).length === 0)
|
|
242
|
+
return null;
|
|
243
|
+
if (data.expiryTimestamp < (Date.now() / 1e3)) {
|
|
244
|
+
await deleteCache(`${file}-uncompressed`);
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
return data.data;
|
|
248
|
+
}
|
|
249
|
+
async function writeExpiringJsonCache(file, data, { expireAfter = 60 * 60 * 24, // cache for 1 day by default
|
|
250
|
+
expiryTimestamp, } = {}) {
|
|
251
|
+
file = 'expiring/' + file;
|
|
252
|
+
if (!expiryTimestamp)
|
|
253
|
+
expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
|
|
254
|
+
const options = { skipR2CacheWrite: true, skipCompression: true };
|
|
255
|
+
await writeCache(file, { data, expiryTimestamp }, options);
|
|
256
|
+
}
|
|
257
|
+
async function cachedFetch({ key, endpoint, postData, fetcher, fetchOptions, errorOnNoCache = true, cacheMaxAge = 0, writeCacheOptions = {
|
|
258
|
+
skipCompression: true,
|
|
259
|
+
skipR2CacheWrite: true,
|
|
260
|
+
}, readCacheOptions = {
|
|
261
|
+
skipCompression: true,
|
|
262
|
+
skipR2Cache: true,
|
|
263
|
+
}, }) {
|
|
264
|
+
if (!key) {
|
|
265
|
+
if (!endpoint || postData || fetcher)
|
|
266
|
+
throw new Error('Key is required for cachedFetch');
|
|
267
|
+
key = (0, common_1.getHash)(endpoint);
|
|
268
|
+
}
|
|
269
|
+
const fileKey = 'cached-fetch/' + key;
|
|
270
|
+
// check local cache first
|
|
271
|
+
if (cacheMaxAge > 0) {
|
|
272
|
+
try {
|
|
273
|
+
const filePath = getFilePath(readCacheOptions.skipCompression ? `${fileKey}-uncompressed` : fileKey);
|
|
274
|
+
const stats = await fs.stat(filePath);
|
|
275
|
+
if (Date.now() - stats.mtimeMs < cacheMaxAge) {
|
|
276
|
+
const cachedData = await readCache(fileKey, readCacheOptions);
|
|
277
|
+
if (cachedData && Object.keys(cachedData).length > 0)
|
|
278
|
+
return cachedData;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
// cache miss or stat error, proceed to fetch
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
let data;
|
|
287
|
+
if (fetcher)
|
|
288
|
+
data = await fetcher();
|
|
289
|
+
else {
|
|
290
|
+
if (!endpoint)
|
|
291
|
+
throw new Error('Either fetcher or endpoint is required for cachedFetch');
|
|
292
|
+
if (postData)
|
|
293
|
+
data = await (0, common_1.postJson)(endpoint, postData, fetchOptions);
|
|
294
|
+
else
|
|
295
|
+
data = await (0, common_1.fetchJson)(endpoint, fetchOptions);
|
|
296
|
+
}
|
|
297
|
+
await writeCache(fileKey, data, writeCacheOptions);
|
|
298
|
+
return data;
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
const errorMessage = (0, common_1.formError)(error).message;
|
|
302
|
+
(0, debugLog_1.debugLog)('Error in cachedFetch:', errorMessage);
|
|
303
|
+
// if fetch fails, read from cache (regardless of age)
|
|
304
|
+
const cachedData = await readCache(fileKey, readCacheOptions);
|
|
305
|
+
if (!cachedData && errorOnNoCache)
|
|
306
|
+
throw new Error('Fetch failed and no cache available');
|
|
307
|
+
return cachedData;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export declare const ES_INDEX = "adapter-call-stats";
|
|
2
|
+
export declare const DEFAULT_MIN_CALLS = 50;
|
|
3
|
+
/** a chain bucket is also kept when it has at least this many getLogs requests, however few calls it made */
|
|
4
|
+
export declare const DEFAULT_MIN_GET_LOGS = 10;
|
|
5
|
+
export type CallStatsMeta = {
|
|
6
|
+
/** 'tvl' | 'dimensions' | ... */
|
|
7
|
+
application: string;
|
|
8
|
+
/** protocol / module name */
|
|
9
|
+
name: string;
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
};
|
|
12
|
+
export type CallStats = {
|
|
13
|
+
meta: CallStatsMeta;
|
|
14
|
+
startTime: number;
|
|
15
|
+
total: number;
|
|
16
|
+
/** chain -> requests to a registered rpc endpoint of that chain */
|
|
17
|
+
byChain: Record<string, number>;
|
|
18
|
+
/** host -> requests, every request */
|
|
19
|
+
byHost: Record<string, number>;
|
|
20
|
+
/** requests that matched no registered endpoint */
|
|
21
|
+
unattributed: number;
|
|
22
|
+
/**
|
|
23
|
+
* logical rpc method -> chain -> requests, recorded by the SDK at the call site (`recordMethod`):
|
|
24
|
+
* every LlamaProvider attempt (`call`, `getLogs`, `getBlock`, ...), starknet `getLogs`, and `indexerGetLogs`
|
|
25
|
+
*/
|
|
26
|
+
byMethod: Record<string, Record<string, number>>;
|
|
27
|
+
};
|
|
28
|
+
export type CallStatsSnapshot = Pick<CallStats, 'total' | 'byChain' | 'byHost' | 'unattributed' | 'byMethod'>;
|
|
29
|
+
/**
|
|
30
|
+
* Remember that `endpoints` serve `chain`, so requests to them are counted against that chain.
|
|
31
|
+
* Called by the SDK wherever it resolves rpc urls; adapters can call it for endpoints they manage themselves.
|
|
32
|
+
*/
|
|
33
|
+
export declare function registerEndpoints(chain: string | undefined, endpoints: string | string[] | undefined): void;
|
|
34
|
+
/** Chain a url is counted against, or undefined when it matches no registered endpoint. */
|
|
35
|
+
export declare function resolveChain(url: string): string | undefined;
|
|
36
|
+
/** Requests to these hosts are not counted (eg. the elasticsearch cluster the stats are written to). */
|
|
37
|
+
export declare function ignoreHost(url: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Count a logical rpc request (one attempt, so retries count) against `method` and `chain` in the current run.
|
|
40
|
+
* The http layer already counts the request itself; this adds the method breakdown (getLogs being the one that matters).
|
|
41
|
+
*/
|
|
42
|
+
export declare function recordMethod(method: string, chain?: string): void;
|
|
43
|
+
/** Patch the node http layer. Idempotent, shared across sdk copies. Called by `run`. */
|
|
44
|
+
export declare function install(): void;
|
|
45
|
+
export type RunOptions = {
|
|
46
|
+
/** chain / host buckets below this are dropped, and nothing is written when the total is below it (default 50, env LLAMA_SDK_CALL_STATS_MIN_CALLS) */
|
|
47
|
+
minCalls?: number;
|
|
48
|
+
/** print the summary table to the console (always, not only in debug mode) */
|
|
49
|
+
print?: boolean;
|
|
50
|
+
/** skip the elasticsearch write */
|
|
51
|
+
skipElastic?: boolean;
|
|
52
|
+
};
|
|
53
|
+
/** Stats of the current run context, or the process-wide bucket when called outside `run`. */
|
|
54
|
+
export declare function getCurrent(): CallStats;
|
|
55
|
+
export declare function snapshot(stats?: CallStats): CallStatsSnapshot;
|
|
56
|
+
export declare function mergeSnapshots(snapshots: CallStatsSnapshot[]): CallStatsSnapshot;
|
|
57
|
+
/**
|
|
58
|
+
* Run `fn` with its own counters and report them when it settles (success or failure).
|
|
59
|
+
* Nested calls get their own context; calls made by `fn` through any http client are attributed to it.
|
|
60
|
+
*/
|
|
61
|
+
export declare function run<T>(meta: CallStatsMeta, fn: () => Promise<T>, options?: RunOptions): Promise<T>;
|
|
62
|
+
export declare function getMinCalls(minCalls?: number): number;
|
|
63
|
+
/** Elasticsearch documents for a run: one `run` summary, one `chain` doc per chain and one `host` doc per unattributed host at or above `minCalls`. */
|
|
64
|
+
export declare function buildDocs(stats: CallStatsSnapshot, meta: CallStatsMeta, { success, runtime, minCalls }?: {
|
|
65
|
+
success?: boolean;
|
|
66
|
+
runtime?: number;
|
|
67
|
+
minCalls?: number;
|
|
68
|
+
}): any[];
|
|
69
|
+
/** Write the docs for `stats` to elasticsearch (no-op when ELASTICSEARCH_CONFIG is unset) and print the summary in debug mode. */
|
|
70
|
+
export declare function report(stats: CallStats | CallStatsSnapshot, { success, runtime, minCalls, print, skipElastic, meta }?: RunOptions & {
|
|
71
|
+
success?: boolean;
|
|
72
|
+
runtime?: number;
|
|
73
|
+
meta?: CallStatsMeta;
|
|
74
|
+
}): Promise<void>;
|