@defillama/sdk 5.0.244 → 5.0.246
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/abi/multicall3.js +2 -2
- package/build/chains/icon.d.ts +84 -0
- package/build/chains/icon.js +155 -0
- package/build/chains/index.d.ts +3 -0
- package/build/chains/index.js +4 -1
- package/build/chains/ontology.d.ts +88 -0
- package/build/chains/ontology.js +93 -0
- package/build/chains/starknet.js +2 -1
- package/build/chains/tezos.js +2 -1
- package/build/chains/zilliqa.d.ts +149 -0
- package/build/chains/zilliqa.js +326 -0
- package/build/providers.json +1 -1
- package/build/util/LlamaProvider.js +10 -10
- package/build/util/cache.d.ts +2 -0
- package/build/util/cache.js +2 -2
- package/build/util/callStats.d.ts +62 -13
- package/build/util/callStats.js +179 -79
- package/build/util/chainUtils/data.json +9 -1
- package/build/util/elastic.d.ts +18 -0
- package/build/util/elastic.js +24 -0
- package/build/util/env.js +1 -1
- package/build/util/index.d.ts +1 -1
- package/build/util/index.js +2 -0
- package/build/util/indexer.d.ts +2 -1
- package/build/util/indexer.js +147 -55
- package/build/util/logs.d.ts +3 -2
- package/build/util/logs.js +22 -2
- package/build/util/r2.d.ts +7 -4
- package/build/util/r2.js +75 -16
- package/package.json +1 -1
|
@@ -97,20 +97,20 @@ class LlamaProvider extends ethers_1.FallbackProvider {
|
|
|
97
97
|
await this._isReady;
|
|
98
98
|
let runners = [...this.rpcs];
|
|
99
99
|
if (method === 'getLogs') {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
runners =
|
|
100
|
+
// archival RPCs first, then the remaining regular RPCs, each URL once
|
|
101
|
+
const seen = new Set();
|
|
102
|
+
runners = [...this.archivalRPCs, ...this.rpcs].filter(i => {
|
|
103
|
+
if (i.url.includes('llamarpc.com') || seen.has(i.url))
|
|
104
|
+
return false;
|
|
105
|
+
seen.add(i.url);
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
103
108
|
}
|
|
104
109
|
if (!runners.length)
|
|
105
110
|
throw new Error('No RPCs available for ' + this.chainName);
|
|
106
111
|
let primaryRunner = runners[0];
|
|
107
112
|
let errors = [];
|
|
108
|
-
|
|
109
|
-
runners = runners.slice(1).concat(this.rpcs);
|
|
110
|
-
runners = runners.sort(() => Math.random() - 0.5); // randomize order of runners
|
|
111
|
-
}
|
|
112
|
-
else
|
|
113
|
-
runners = runners.slice(1).sort(() => Math.random() - 0.5); // randomize order of runners
|
|
113
|
+
runners = runners.slice(1).sort(() => Math.random() - 0.5); // randomize order of runners
|
|
114
114
|
const isArchivalRequest = ['call', 'getBalance'].includes(method) && params[1] !== 'latest';
|
|
115
115
|
// check if domain name of primaryRunner.url is llama
|
|
116
116
|
const isLlamaRPC = primaryRunner.url.includes('llama');
|
|
@@ -329,7 +329,7 @@ const httpRPC = {
|
|
|
329
329
|
};
|
|
330
330
|
function getMedianBlockValue(blocks) {
|
|
331
331
|
blocks.sort((a, b) => a - b);
|
|
332
|
-
const mid = Math.floor(blocks.length /
|
|
332
|
+
const mid = Math.floor(blocks.length / 2);
|
|
333
333
|
return blocks.length % 2 !== 0 ? blocks[mid] : (blocks[mid - 1] + blocks[mid]) / 2;
|
|
334
334
|
}
|
|
335
335
|
let lastPrintTimeMS = Date.now();
|
package/build/util/cache.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ interface WriteCacheOptions {
|
|
|
4
4
|
skipR2CacheWrite?: boolean;
|
|
5
5
|
alreadyCompressed?: boolean;
|
|
6
6
|
skipCompression?: boolean;
|
|
7
|
+
private?: boolean;
|
|
7
8
|
}
|
|
8
9
|
interface ReadCacheOptions {
|
|
9
10
|
checkIfRecent?: boolean;
|
|
10
11
|
readFromR2Cache?: boolean;
|
|
11
12
|
skipR2Cache?: boolean;
|
|
12
13
|
skipCompression?: boolean;
|
|
14
|
+
private?: boolean;
|
|
13
15
|
}
|
|
14
16
|
export declare function writeCache(file: string, data: any, options?: WriteCacheOptions): Promise<string | undefined>;
|
|
15
17
|
export declare function parseCache(dataString: string | Buffer, options?: ReadCacheOptions): Promise<any>;
|
package/build/util/cache.js
CHANGED
|
@@ -91,7 +91,7 @@ async function readCache(file, options = {}) {
|
|
|
91
91
|
if (options.skipR2Cache)
|
|
92
92
|
return;
|
|
93
93
|
try {
|
|
94
|
-
const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file, { throwErrorOnFail: (_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false });
|
|
94
|
+
const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file, { throwErrorOnFail: (_a = options.readFromR2Cache) !== null && _a !== void 0 ? _a : false, private: options.private });
|
|
95
95
|
if (r2Data) {
|
|
96
96
|
// `file` already carries the `-uncompressed` suffix (added above), so omit skipCompression here to avoid writeCache appending it again
|
|
97
97
|
await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true });
|
|
@@ -126,7 +126,7 @@ async function writeCache(file, data, options = {}) {
|
|
|
126
126
|
await createSubPath(path.dirname(filePath));
|
|
127
127
|
await fs.writeFile(filePath, fileData);
|
|
128
128
|
if (!options.skipR2CacheWrite) {
|
|
129
|
-
await (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData);
|
|
129
|
+
await (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData, { private: options.private });
|
|
130
130
|
}
|
|
131
131
|
return fileData;
|
|
132
132
|
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const
|
|
3
|
-
/**
|
|
4
|
-
export declare const
|
|
1
|
+
/** one doc per (run, chain) */
|
|
2
|
+
export declare const ES_INDEX = "adapter-calls";
|
|
3
|
+
/** one doc per (run, host) */
|
|
4
|
+
export declare const ES_HOST_INDEX = "adapter-call-hosts";
|
|
5
|
+
/** `chain` of the doc counting requests that matched no registered rpc endpoint */
|
|
6
|
+
export declare const OTHER_CHAIN = "_other";
|
|
7
|
+
/** runs with fewer http requests in total are not written at all (env CALL_STATS_MIN_CALLS) */
|
|
8
|
+
export declare const DEFAULT_MIN_CALLS = 1;
|
|
9
|
+
export declare const MAX_HOSTS_PER_RUN = 25;
|
|
5
10
|
export type CallStatsMeta = {
|
|
6
11
|
/** 'tvl' | 'dimensions' | ... */
|
|
7
12
|
application: string;
|
|
@@ -24,8 +29,14 @@ export type CallStats = {
|
|
|
24
29
|
* every LlamaProvider attempt (`call`, `getLogs`, `getBlock`, ...), starknet `getLogs`, and `indexerGetLogs`
|
|
25
30
|
*/
|
|
26
31
|
byMethod: Record<string, Record<string, number>>;
|
|
32
|
+
/** log rows fetched over the wire: source ('rpc' | 'indexer') -> chain -> rows, recorded by the SDK (`recordLogs`) */
|
|
33
|
+
logs: Record<LogSource, Record<string, number>>;
|
|
34
|
+
/** set once the run's docs were written (by `run` settling, or earlier by `flushActive`) */
|
|
35
|
+
reported?: boolean;
|
|
27
36
|
};
|
|
28
|
-
export type
|
|
37
|
+
export type LogSource = 'rpc' | 'indexer';
|
|
38
|
+
export declare const INDEXER_METHOD = "indexer";
|
|
39
|
+
export type CallStatsSnapshot = Pick<CallStats, 'total' | 'byChain' | 'byHost' | 'unattributed' | 'byMethod'> & Partial<Pick<CallStats, 'logs'>>;
|
|
29
40
|
/**
|
|
30
41
|
* Remember that `endpoints` serve `chain`, so requests to them are counted against that chain.
|
|
31
42
|
* Called by the SDK wherever it resolves rpc urls; adapters can call it for endpoints they manage themselves.
|
|
@@ -40,10 +51,12 @@ export declare function ignoreHost(url: string): void;
|
|
|
40
51
|
* The http layer already counts the request itself; this adds the method breakdown (getLogs being the one that matters).
|
|
41
52
|
*/
|
|
42
53
|
export declare function recordMethod(method: string, chain?: string): void;
|
|
54
|
+
/** Count `count` log rows fetched from `source` for `chain` in the current run (rows received, cached reads are not fetched). */
|
|
55
|
+
export declare function recordLogs(source: LogSource, chain: string | undefined, count: number): void;
|
|
43
56
|
/** Patch the node http layer. Idempotent, shared across sdk copies. Called by `run`. */
|
|
44
57
|
export declare function install(): void;
|
|
45
58
|
export type RunOptions = {
|
|
46
|
-
/**
|
|
59
|
+
/** nothing is written when the run made fewer http requests in total (default 1, env CALL_STATS_MIN_CALLS) */
|
|
47
60
|
minCalls?: number;
|
|
48
61
|
/** print the summary table to the console (always, not only in debug mode) */
|
|
49
62
|
print?: boolean;
|
|
@@ -59,16 +72,52 @@ export declare function mergeSnapshots(snapshots: CallStatsSnapshot[]): CallStat
|
|
|
59
72
|
* Nested calls get their own context; calls made by `fn` through any http client are attributed to it.
|
|
60
73
|
*/
|
|
61
74
|
export declare function run<T>(meta: CallStatsMeta, fn: () => Promise<T>, options?: RunOptions): Promise<T>;
|
|
75
|
+
/**
|
|
76
|
+
* Write the docs of the runs still in flight (all of them, or those whose meta passes `filter`) as failed runs
|
|
77
|
+
* with `interrupted: reason`, and mark them so they are not written again when they eventually settle.
|
|
78
|
+
* For a process that is about to be killed, or a job the caller has given up on: without this the heaviest runs,
|
|
79
|
+
* the ones that hit the timeout, would be exactly the ones never recorded. Returns the number of runs written.
|
|
80
|
+
*/
|
|
81
|
+
export declare function flushActive(reason: string, { filter, ...options }?: RunOptions & {
|
|
82
|
+
filter?: (meta: CallStatsMeta) => boolean;
|
|
83
|
+
}): Promise<number>;
|
|
62
84
|
export declare function getMinCalls(minCalls?: number): number;
|
|
63
|
-
|
|
64
|
-
|
|
85
|
+
export type ChainRow = {
|
|
86
|
+
chain: string;
|
|
87
|
+
/** http requests to a registered rpc of the chain (`_other`: requests matching no registered rpc, the indexer included) */
|
|
88
|
+
calls: number;
|
|
89
|
+
/** rpc getLogs attempts */
|
|
90
|
+
getLogs: number;
|
|
91
|
+
/** http requests to the llama indexer for this chain */
|
|
92
|
+
indexerCalls: number;
|
|
93
|
+
/** log rows fetched over the wire, by source and in total */
|
|
94
|
+
logs: number;
|
|
95
|
+
logsRpc: number;
|
|
96
|
+
logsIndexer: number;
|
|
97
|
+
methods: Record<string, number>;
|
|
98
|
+
};
|
|
99
|
+
export type HostRow = {
|
|
100
|
+
host: string;
|
|
101
|
+
calls: number;
|
|
102
|
+
rpc: boolean;
|
|
103
|
+
};
|
|
104
|
+
export type CallStatsDocs = {
|
|
105
|
+
chainDocs: any[];
|
|
106
|
+
hostDocs: any[];
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Elasticsearch documents for a run: `chainDocs` for `adapter-calls` (one per chain) and `hostDocs` for
|
|
110
|
+
* `adapter-call-hosts` (one per host). Every doc carries the run meta, totals and the same `runId`, so
|
|
111
|
+
* run-level questions are a `runId` grouping of the same index. Empty when the run is under `minCalls`.
|
|
112
|
+
*/
|
|
113
|
+
export type ReportOptions = {
|
|
65
114
|
success?: boolean;
|
|
66
115
|
runtime?: number;
|
|
67
|
-
minCalls?: number;
|
|
68
|
-
|
|
116
|
+
minCalls?: number; /** why the run was written before it settled ('timeout', 'shutdown'...) */
|
|
117
|
+
interrupted?: string;
|
|
118
|
+
};
|
|
119
|
+
export declare function buildDocs(stats: CallStatsSnapshot, meta: CallStatsMeta, { success, runtime, minCalls, interrupted }?: ReportOptions): CallStatsDocs;
|
|
69
120
|
/** 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;
|
|
121
|
+
export declare function report(stats: CallStats | CallStatsSnapshot, { success, runtime, minCalls, interrupted, print, skipElastic, meta }?: RunOptions & ReportOptions & {
|
|
73
122
|
meta?: CallStatsMeta;
|
|
74
123
|
}): Promise<void>;
|
package/build/util/callStats.js
CHANGED
|
@@ -4,16 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
var _a;
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
7
|
+
exports.INDEXER_METHOD = exports.MAX_HOSTS_PER_RUN = exports.DEFAULT_MIN_CALLS = exports.OTHER_CHAIN = exports.ES_HOST_INDEX = exports.ES_INDEX = void 0;
|
|
8
8
|
exports.registerEndpoints = registerEndpoints;
|
|
9
9
|
exports.resolveChain = resolveChain;
|
|
10
10
|
exports.ignoreHost = ignoreHost;
|
|
11
11
|
exports.recordMethod = recordMethod;
|
|
12
|
+
exports.recordLogs = recordLogs;
|
|
12
13
|
exports.install = install;
|
|
13
14
|
exports.getCurrent = getCurrent;
|
|
14
15
|
exports.snapshot = snapshot;
|
|
15
16
|
exports.mergeSnapshots = mergeSnapshots;
|
|
16
17
|
exports.run = run;
|
|
18
|
+
exports.flushActive = flushActive;
|
|
17
19
|
exports.getMinCalls = getMinCalls;
|
|
18
20
|
exports.buildDocs = buildDocs;
|
|
19
21
|
exports.report = report;
|
|
@@ -21,6 +23,17 @@ exports.report = report;
|
|
|
21
23
|
* Per-run counters for every outbound HTTP request a process makes, attributed to a chain
|
|
22
24
|
* and to the adapter run that made them, and flushed to Elasticsearch when the run ends.
|
|
23
25
|
*
|
|
26
|
+
* What gets written (see `buildDocs`)
|
|
27
|
+
* - `adapter-calls`: one flat doc per (run, chain) carrying the run's meta (application, name, runType...),
|
|
28
|
+
* `chain`, `calls` (rpc http requests), `getLogs` (rpc getLogs attempts), `indexerCalls` (http requests to
|
|
29
|
+
* the llama indexer for that chain), `logs` / `logsRpc` / `logsIndexer` (log rows fetched over the wire),
|
|
30
|
+
* `methods.*`, the run totals (`runCalls`, `runGetLogs`, `runLogs`), `success`, `runtime` and a shared
|
|
31
|
+
* `runId`. Requests matching no registered rpc go to `chain: "_other"`. Nothing is dropped, so a terms
|
|
32
|
+
* aggregation on `chain` or `name` over any time range is exact.
|
|
33
|
+
* - `adapter-call-hosts`: one doc per (run, host) for the 25 busiest hosts of the run, same base fields
|
|
34
|
+
* plus `host` and `rpc` (true when the host is a registered rpc endpoint).
|
|
35
|
+
* Both carry `@timestamp` (ISO) so Kibana picks it up as the time field with no index template.
|
|
36
|
+
*
|
|
24
37
|
* How it works
|
|
25
38
|
* - `install()` wraps `http.request` / `http.get` / `https.request` / `https.get` and subscribes to
|
|
26
39
|
* the `undici:request:create` diagnostics channel (global `fetch`). That catches axios, ethers,
|
|
@@ -36,19 +49,26 @@ exports.report = report;
|
|
|
36
49
|
* registry and one context, and nothing is counted twice.
|
|
37
50
|
*/
|
|
38
51
|
const async_hooks_1 = require("async_hooks");
|
|
52
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
39
53
|
const http_1 = __importDefault(require("http"));
|
|
40
54
|
const https_1 = __importDefault(require("https"));
|
|
41
55
|
const diagnostics_channel_1 = __importDefault(require("diagnostics_channel"));
|
|
42
56
|
const elastic_1 = require("./elastic");
|
|
43
57
|
const debugLog_1 = require("./debugLog");
|
|
44
58
|
const env_1 = require("./env");
|
|
45
|
-
|
|
46
|
-
exports.
|
|
47
|
-
/**
|
|
48
|
-
exports.
|
|
59
|
+
/** one doc per (run, chain) */
|
|
60
|
+
exports.ES_INDEX = 'adapter-calls';
|
|
61
|
+
/** one doc per (run, host) */
|
|
62
|
+
exports.ES_HOST_INDEX = 'adapter-call-hosts';
|
|
63
|
+
/** `chain` of the doc counting requests that matched no registered rpc endpoint */
|
|
64
|
+
exports.OTHER_CHAIN = '_other';
|
|
65
|
+
/** runs with fewer http requests in total are not written at all (env CALL_STATS_MIN_CALLS) */
|
|
66
|
+
exports.DEFAULT_MIN_CALLS = 1;
|
|
67
|
+
exports.MAX_HOSTS_PER_RUN = 25;
|
|
68
|
+
exports.INDEXER_METHOD = 'indexer';
|
|
49
69
|
const STATE_KEY = Symbol.for('@defillama/sdk/callStats');
|
|
50
70
|
function newStats(meta) {
|
|
51
|
-
return { meta, startTime: Date.now(), total: 0, byChain: {}, byHost: {}, unattributed: 0, byMethod: {} };
|
|
71
|
+
return { meta, startTime: Date.now(), total: 0, byChain: {}, byHost: {}, unattributed: 0, byMethod: {}, logs: { rpc: {}, indexer: {} } };
|
|
52
72
|
}
|
|
53
73
|
const shared = (_a = globalThis[STATE_KEY]) !== null && _a !== void 0 ? _a : (globalThis[STATE_KEY] = {
|
|
54
74
|
als: new async_hooks_1.AsyncLocalStorage(),
|
|
@@ -57,7 +77,11 @@ const shared = (_a = globalThis[STATE_KEY]) !== null && _a !== void 0 ? _a : (gl
|
|
|
57
77
|
global: newStats({ application: 'global', name: '-' }),
|
|
58
78
|
ignoredHosts: new Set(),
|
|
59
79
|
registered: new Set(),
|
|
80
|
+
active: new Set(),
|
|
60
81
|
});
|
|
82
|
+
// the state may have been created by an older sdk copy that did not track active runs
|
|
83
|
+
if (!shared.active)
|
|
84
|
+
shared.active = new Set();
|
|
61
85
|
// ---------------------------------------------------------------------------
|
|
62
86
|
// endpoint registry
|
|
63
87
|
// ---------------------------------------------------------------------------
|
|
@@ -141,19 +165,39 @@ function record(url) {
|
|
|
141
165
|
* Count a logical rpc request (one attempt, so retries count) against `method` and `chain` in the current run.
|
|
142
166
|
* The http layer already counts the request itself; this adds the method breakdown (getLogs being the one that matters).
|
|
143
167
|
*/
|
|
144
|
-
function recordMethod(method, chain =
|
|
168
|
+
function recordMethod(method, chain = exports.OTHER_CHAIN) {
|
|
145
169
|
var _a, _b, _c;
|
|
146
170
|
const stats = (_a = shared.als.getStore()) !== null && _a !== void 0 ? _a : shared.global;
|
|
147
171
|
const byChain = (_b = stats.byMethod[method]) !== null && _b !== void 0 ? _b : (stats.byMethod[method] = {});
|
|
148
172
|
chain = chain.toLowerCase();
|
|
149
173
|
byChain[chain] = ((_c = byChain[chain]) !== null && _c !== void 0 ? _c : 0) + 1;
|
|
150
174
|
}
|
|
175
|
+
/** Count `count` log rows fetched from `source` for `chain` in the current run (rows received, cached reads are not fetched). */
|
|
176
|
+
function recordLogs(source, chain = exports.OTHER_CHAIN, count) {
|
|
177
|
+
var _a, _b, _c;
|
|
178
|
+
if (!(count > 0))
|
|
179
|
+
return;
|
|
180
|
+
const stats = (_a = shared.als.getStore()) !== null && _a !== void 0 ? _a : shared.global;
|
|
181
|
+
const byChain = (_b = stats.logs[source]) !== null && _b !== void 0 ? _b : (stats.logs[source] = {});
|
|
182
|
+
chain = chain.toLowerCase();
|
|
183
|
+
byChain[chain] = ((_c = byChain[chain]) !== null && _c !== void 0 ? _c : 0) + count;
|
|
184
|
+
}
|
|
185
|
+
function sumValues(map) {
|
|
186
|
+
return Object.values(map !== null && map !== void 0 ? map : {}).reduce((a, b) => a + b, 0);
|
|
187
|
+
}
|
|
151
188
|
function getLogsCount(stats, chain) {
|
|
152
189
|
var _a, _b, _c;
|
|
153
190
|
const byChain = (_b = (_a = stats.byMethod) === null || _a === void 0 ? void 0 : _a.getLogs) !== null && _b !== void 0 ? _b : {};
|
|
154
191
|
if (chain)
|
|
155
192
|
return (_c = byChain[chain]) !== null && _c !== void 0 ? _c : 0;
|
|
156
|
-
return
|
|
193
|
+
return sumValues(byChain);
|
|
194
|
+
}
|
|
195
|
+
function logsFetched(stats, source, chain) {
|
|
196
|
+
var _a, _b, _c;
|
|
197
|
+
const byChain = (_b = (_a = stats.logs) === null || _a === void 0 ? void 0 : _a[source]) !== null && _b !== void 0 ? _b : {};
|
|
198
|
+
if (chain)
|
|
199
|
+
return (_c = byChain[chain]) !== null && _c !== void 0 ? _c : 0;
|
|
200
|
+
return sumValues(byChain);
|
|
157
201
|
}
|
|
158
202
|
/** method -> requests across chains */
|
|
159
203
|
function methodTotals(stats) {
|
|
@@ -252,15 +296,29 @@ function getCurrent() {
|
|
|
252
296
|
var _a;
|
|
253
297
|
return (_a = shared.als.getStore()) !== null && _a !== void 0 ? _a : shared.global;
|
|
254
298
|
}
|
|
299
|
+
function copyNested(map) {
|
|
300
|
+
const out = {};
|
|
301
|
+
for (const [key, byChain] of Object.entries(map !== null && map !== void 0 ? map : {}))
|
|
302
|
+
out[key] = { ...byChain };
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
function addNested(target, source) {
|
|
306
|
+
var _a, _b;
|
|
307
|
+
for (const [key, byChain] of Object.entries(source !== null && source !== void 0 ? source : {})) {
|
|
308
|
+
const into = (_a = target[key]) !== null && _a !== void 0 ? _a : (target[key] = {});
|
|
309
|
+
for (const [k, v] of Object.entries(byChain))
|
|
310
|
+
into[k] = ((_b = into[k]) !== null && _b !== void 0 ? _b : 0) + v;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
255
313
|
function snapshot(stats = getCurrent()) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
byMethod
|
|
259
|
-
|
|
314
|
+
return {
|
|
315
|
+
total: stats.total, byChain: { ...stats.byChain }, byHost: { ...stats.byHost }, unattributed: stats.unattributed,
|
|
316
|
+
byMethod: copyNested(stats.byMethod), logs: copyNested(stats.logs),
|
|
317
|
+
};
|
|
260
318
|
}
|
|
261
319
|
function mergeSnapshots(snapshots) {
|
|
262
|
-
var _a, _b, _c, _d, _e, _f
|
|
263
|
-
const out = { total: 0, byChain: {}, byHost: {}, unattributed: 0, byMethod: {} };
|
|
320
|
+
var _a, _b, _c, _d, _e, _f;
|
|
321
|
+
const out = { total: 0, byChain: {}, byHost: {}, unattributed: 0, byMethod: {}, logs: { rpc: {}, indexer: {} } };
|
|
264
322
|
for (const s of snapshots) {
|
|
265
323
|
if (!s)
|
|
266
324
|
continue;
|
|
@@ -270,11 +328,8 @@ function mergeSnapshots(snapshots) {
|
|
|
270
328
|
out.byChain[k] = ((_d = out.byChain[k]) !== null && _d !== void 0 ? _d : 0) + v;
|
|
271
329
|
for (const [k, v] of Object.entries((_e = s.byHost) !== null && _e !== void 0 ? _e : {}))
|
|
272
330
|
out.byHost[k] = ((_f = out.byHost[k]) !== null && _f !== void 0 ? _f : 0) + v;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
for (const [k, v] of Object.entries(byChain))
|
|
276
|
-
target[k] = ((_j = target[k]) !== null && _j !== void 0 ? _j : 0) + v;
|
|
277
|
-
}
|
|
331
|
+
addNested(out.byMethod, s.byMethod);
|
|
332
|
+
addNested(out.logs, s.logs);
|
|
278
333
|
}
|
|
279
334
|
return out;
|
|
280
335
|
}
|
|
@@ -285,6 +340,7 @@ function mergeSnapshots(snapshots) {
|
|
|
285
340
|
async function run(meta, fn, options = {}) {
|
|
286
341
|
install();
|
|
287
342
|
const stats = newStats(meta);
|
|
343
|
+
shared.active.add(stats);
|
|
288
344
|
return shared.als.run(stats, async () => {
|
|
289
345
|
let success = false;
|
|
290
346
|
try {
|
|
@@ -293,92 +349,136 @@ async function run(meta, fn, options = {}) {
|
|
|
293
349
|
return result;
|
|
294
350
|
}
|
|
295
351
|
finally {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
352
|
+
shared.active.delete(stats);
|
|
353
|
+
// already written by flushActive: the run was given up on (timeout / shutdown), its outcome no longer counts
|
|
354
|
+
if (!stats.reported) {
|
|
355
|
+
stats.reported = true;
|
|
356
|
+
try {
|
|
357
|
+
await report(stats, { success, runtime: (Date.now() - stats.startTime) / 1000, ...options });
|
|
358
|
+
}
|
|
359
|
+
catch (e) {
|
|
360
|
+
(0, debugLog_1.debugLog)('[callStats] failed to report', e === null || e === void 0 ? void 0 : e.message);
|
|
361
|
+
}
|
|
301
362
|
}
|
|
302
363
|
}
|
|
303
364
|
});
|
|
304
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Write the docs of the runs still in flight (all of them, or those whose meta passes `filter`) as failed runs
|
|
368
|
+
* with `interrupted: reason`, and mark them so they are not written again when they eventually settle.
|
|
369
|
+
* For a process that is about to be killed, or a job the caller has given up on: without this the heaviest runs,
|
|
370
|
+
* the ones that hit the timeout, would be exactly the ones never recorded. Returns the number of runs written.
|
|
371
|
+
*/
|
|
372
|
+
async function flushActive(reason, { filter, ...options } = {}) {
|
|
373
|
+
const runs = [...shared.active].filter(stats => !stats.reported && (!filter || filter(stats.meta)));
|
|
374
|
+
for (const stats of runs)
|
|
375
|
+
stats.reported = true;
|
|
376
|
+
await Promise.all(runs.map(stats => report(stats, { success: false, interrupted: reason, runtime: (Date.now() - stats.startTime) / 1000, ...options })
|
|
377
|
+
.catch(e => (0, debugLog_1.debugLog)('[callStats] failed to flush', e === null || e === void 0 ? void 0 : e.message))));
|
|
378
|
+
return runs.length;
|
|
379
|
+
}
|
|
305
380
|
// ---------------------------------------------------------------------------
|
|
306
381
|
// reporting
|
|
307
382
|
// ---------------------------------------------------------------------------
|
|
308
|
-
function sortedEntries(map, minCalls, limit) {
|
|
309
|
-
const entries = Object.entries(map).filter(([, calls]) => calls >= minCalls).map(([key, calls]) => ({ key, calls }));
|
|
310
|
-
entries.sort((a, b) => b.calls - a.calls);
|
|
311
|
-
return limit ? entries.slice(0, limit) : entries;
|
|
312
|
-
}
|
|
313
383
|
function getMinCalls(minCalls) {
|
|
314
384
|
if (minCalls !== undefined)
|
|
315
385
|
return minCalls;
|
|
316
386
|
return +(0, env_1.getEnvValue)('CALL_STATS_MIN_CALLS', String(exports.DEFAULT_MIN_CALLS));
|
|
317
387
|
}
|
|
318
|
-
function getMinGetLogs() {
|
|
319
|
-
return +(0, env_1.getEnvValue)('CALL_STATS_MIN_GET_LOGS', String(exports.DEFAULT_MIN_GET_LOGS));
|
|
320
|
-
}
|
|
321
388
|
/**
|
|
322
|
-
*
|
|
323
|
-
*
|
|
389
|
+
* One row per chain that made an http request, had an rpc method recorded or had logs fetched, plus `_other`
|
|
390
|
+
* for requests matching no registered rpc. Most calls first. Nothing is thresholded away: the long tail of
|
|
391
|
+
* small adapters is exactly what a per-chain sum must include.
|
|
324
392
|
*/
|
|
325
|
-
function
|
|
393
|
+
function chainRows(stats) {
|
|
326
394
|
var _a, _b;
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
395
|
+
const chains = new Set(Object.keys(stats.byChain));
|
|
396
|
+
for (const byChain of Object.values((_a = stats.byMethod) !== null && _a !== void 0 ? _a : {}))
|
|
397
|
+
for (const chain of Object.keys(byChain))
|
|
398
|
+
chains.add(chain);
|
|
399
|
+
for (const byChain of Object.values((_b = stats.logs) !== null && _b !== void 0 ? _b : {}))
|
|
400
|
+
for (const chain of Object.keys(byChain))
|
|
401
|
+
chains.add(chain);
|
|
402
|
+
if (stats.unattributed > 0)
|
|
403
|
+
chains.add(exports.OTHER_CHAIN);
|
|
404
|
+
const rows = [...chains].map(chain => {
|
|
405
|
+
var _a, _b;
|
|
406
|
+
const methods = methodsForChain(stats, chain);
|
|
407
|
+
const logsRpc = logsFetched(stats, 'rpc', chain);
|
|
408
|
+
const logsIndexer = logsFetched(stats, 'indexer', chain);
|
|
409
|
+
return {
|
|
410
|
+
chain,
|
|
411
|
+
calls: chain === exports.OTHER_CHAIN ? stats.unattributed : (_a = stats.byChain[chain]) !== null && _a !== void 0 ? _a : 0,
|
|
412
|
+
getLogs: getLogsCount(stats, chain),
|
|
413
|
+
indexerCalls: (_b = methods[exports.INDEXER_METHOD]) !== null && _b !== void 0 ? _b : 0,
|
|
414
|
+
logs: logsRpc + logsIndexer, logsRpc, logsIndexer,
|
|
415
|
+
methods,
|
|
416
|
+
};
|
|
417
|
+
}).filter(r => r.calls > 0 || r.logs > 0 || Object.keys(r.methods).length > 0);
|
|
418
|
+
rows.sort((a, b) => b.calls - a.calls || b.getLogs - a.getLogs || b.logs - a.logs);
|
|
419
|
+
return rows;
|
|
334
420
|
}
|
|
335
|
-
/**
|
|
336
|
-
function
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
421
|
+
/** The busiest hosts of the run, rpc endpoints included so provider load is visible too. */
|
|
422
|
+
function hostRows(stats) {
|
|
423
|
+
return Object.entries(stats.byHost)
|
|
424
|
+
.map(([host, calls]) => ({ host, calls, rpc: shared.endpoints.has(host) }))
|
|
425
|
+
.sort((a, b) => b.calls - a.calls)
|
|
426
|
+
.slice(0, exports.MAX_HOSTS_PER_RUN);
|
|
427
|
+
}
|
|
428
|
+
/** Scalar meta fields become top-level doc fields; arrays/objects (eg. the list of chains to run) would only bloat the mapping. */
|
|
429
|
+
function flatMeta(meta) {
|
|
430
|
+
const out = {};
|
|
431
|
+
for (const [key, value] of Object.entries(meta))
|
|
432
|
+
if (value !== undefined && value !== null && typeof value !== 'object')
|
|
433
|
+
out[key] = value;
|
|
434
|
+
return out;
|
|
435
|
+
}
|
|
436
|
+
function buildDocs(stats, meta, { success, runtime, minCalls, interrupted } = {}) {
|
|
437
|
+
var _a;
|
|
438
|
+
const rows = stats.total >= getMinCalls(minCalls) ? chainRows(stats) : [];
|
|
439
|
+
if (!rows.length)
|
|
440
|
+
return { chainDocs: [], hostDocs: [] };
|
|
441
|
+
const base = {
|
|
442
|
+
'@timestamp': new Date().toISOString(),
|
|
443
|
+
...flatMeta(meta),
|
|
444
|
+
runId: crypto_1.default.randomBytes(8).toString('hex'),
|
|
445
|
+
success, runtime, interrupted,
|
|
446
|
+
runCalls: stats.total,
|
|
447
|
+
runGetLogs: getLogsCount(stats),
|
|
448
|
+
runIndexerCalls: sumValues((_a = stats.byMethod) === null || _a === void 0 ? void 0 : _a[exports.INDEXER_METHOD]),
|
|
449
|
+
runLogs: logsFetched(stats, 'rpc') + logsFetched(stats, 'indexer'),
|
|
450
|
+
runChains: rows.filter(r => r.chain !== exports.OTHER_CHAIN).length,
|
|
451
|
+
};
|
|
452
|
+
return {
|
|
453
|
+
chainDocs: rows.map(r => ({ ...base, ...r })),
|
|
454
|
+
hostDocs: hostRows(stats).map(r => ({ ...base, ...r })),
|
|
455
|
+
};
|
|
359
456
|
}
|
|
360
457
|
/** Write the docs for `stats` to elasticsearch (no-op when ELASTICSEARCH_CONFIG is unset) and print the summary in debug mode. */
|
|
361
|
-
async function report(stats, { success, runtime, minCalls, print, skipElastic, meta } = {}) {
|
|
458
|
+
async function report(stats, { success, runtime, minCalls, interrupted, print, skipElastic, meta } = {}) {
|
|
362
459
|
var _a;
|
|
363
460
|
meta = (_a = meta !== null && meta !== void 0 ? meta : stats.meta) !== null && _a !== void 0 ? _a : { application: 'unknown', name: '-' };
|
|
364
|
-
|
|
365
|
-
const docs = buildDocs(stats, meta, { success, runtime, minCalls });
|
|
461
|
+
const { chainDocs, hostDocs } = buildDocs(stats, meta, { success, runtime, minCalls, interrupted });
|
|
366
462
|
if (print)
|
|
367
|
-
printStats(stats, meta,
|
|
463
|
+
printStats(stats, meta, console.log, console.table);
|
|
368
464
|
else
|
|
369
|
-
printStats(stats, meta,
|
|
370
|
-
if (skipElastic
|
|
465
|
+
printStats(stats, meta, debugLog_1.debugLog, debugLog_1.debugTable);
|
|
466
|
+
if (skipElastic)
|
|
371
467
|
return;
|
|
372
|
-
await Promise.all(
|
|
468
|
+
await Promise.all([
|
|
469
|
+
...chainDocs.map(doc => (0, elastic_1.writeLog)(exports.ES_INDEX, doc)),
|
|
470
|
+
...hostDocs.map(doc => (0, elastic_1.writeLog)(exports.ES_HOST_INDEX, doc)),
|
|
471
|
+
]);
|
|
373
472
|
}
|
|
374
|
-
function printStats(stats, meta,
|
|
375
|
-
|
|
376
|
-
if (stats.total
|
|
473
|
+
function printStats(stats, meta, log, table) {
|
|
474
|
+
var _a;
|
|
475
|
+
if (!stats.total)
|
|
377
476
|
return;
|
|
378
|
-
|
|
477
|
+
const logs = logsFetched(stats, 'rpc') + logsFetched(stats, 'indexer');
|
|
478
|
+
log(`[callStats] ${meta.application}/${meta.name}: ${stats.total} http requests (${stats.unattributed} not matching a known rpc, ${getLogsCount(stats)} getLogs, ${sumValues((_a = stats.byMethod) === null || _a === void 0 ? void 0 : _a[exports.INDEXER_METHOD])} indexer requests, ${logs} logs fetched)`);
|
|
379
479
|
const rows = [
|
|
380
|
-
...
|
|
381
|
-
...
|
|
480
|
+
...chainRows(stats).slice(0, 30).map(r => ({ chain: r.chain, calls: r.calls, getLogs: r.getLogs, indexerCalls: r.indexerCalls, logs: r.logs })),
|
|
481
|
+
...hostRows(stats).filter(h => !h.rpc).slice(0, 10).map(h => ({ chain: `host: ${h.host}`, calls: h.calls, getLogs: 0, indexerCalls: 0, logs: 0 })),
|
|
382
482
|
];
|
|
383
483
|
if (rows.length)
|
|
384
484
|
table(rows);
|
|
@@ -498,6 +498,7 @@
|
|
|
498
498
|
"saga": "Saga",
|
|
499
499
|
"sanko": "Sanko",
|
|
500
500
|
"sapphire": "Oasis Sapphire",
|
|
501
|
+
"saseul": "SASEUL",
|
|
501
502
|
"satori": "Satori",
|
|
502
503
|
"scroll": "Scroll",
|
|
503
504
|
"secret": "Secret",
|
|
@@ -627,7 +628,7 @@
|
|
|
627
628
|
"zenrock": "Zenrock",
|
|
628
629
|
"zero_network": "Zero Network",
|
|
629
630
|
"zeta": "ZetaChain",
|
|
630
|
-
"zigchain": "
|
|
631
|
+
"zigchain": "ZIG Chain",
|
|
631
632
|
"zilliqa": "Zilliqa",
|
|
632
633
|
"zircuit": "Zircuit",
|
|
633
634
|
"zkfair": "Zkfair",
|
|
@@ -1156,6 +1157,7 @@
|
|
|
1156
1157
|
"Ronin": "ronin",
|
|
1157
1158
|
"Rootstock": "rsk",
|
|
1158
1159
|
"Rubin": "rubin",
|
|
1160
|
+
"SASEUL": "saseul",
|
|
1159
1161
|
"SELEMAN": "seleman",
|
|
1160
1162
|
"SGE": "sge",
|
|
1161
1163
|
"SKALE": "skale",
|
|
@@ -1291,6 +1293,7 @@
|
|
|
1291
1293
|
"Xone Chain": "xone",
|
|
1292
1294
|
"Xphere": "xp",
|
|
1293
1295
|
"Yominet": "yomi",
|
|
1296
|
+
"ZIG Chain": "zigchain",
|
|
1294
1297
|
"ZIGChain": "zigchain",
|
|
1295
1298
|
"ZKsync Era": "era",
|
|
1296
1299
|
"ZKsync Lite": "zksync",
|
|
@@ -1327,10 +1330,12 @@
|
|
|
1327
1330
|
},
|
|
1328
1331
|
"deadChains": {
|
|
1329
1332
|
"airdao": "2026-01-20",
|
|
1333
|
+
"alv": "2026-10-27",
|
|
1330
1334
|
"ancient8": "2026-06-19",
|
|
1331
1335
|
"artela": "2026-06-05",
|
|
1332
1336
|
"astrzk": "2026-02-12",
|
|
1333
1337
|
"bitrock": "2026-02-12",
|
|
1338
|
+
"blast": "2026-10-26",
|
|
1334
1339
|
"boba_bnb": "2026-01-20",
|
|
1335
1340
|
"bouncebit": "2026-08-24",
|
|
1336
1341
|
"btnx": "2026-07-09",
|
|
@@ -1372,6 +1377,7 @@
|
|
|
1372
1377
|
"genesys": "2026-03-01",
|
|
1373
1378
|
"godwoken": "2026-01-06",
|
|
1374
1379
|
"godwoken_v1": "2026-01-06",
|
|
1380
|
+
"gravity": "2026-11-01",
|
|
1375
1381
|
"grove": "2025-07-28",
|
|
1376
1382
|
"ham": "2026-06-05",
|
|
1377
1383
|
"harmony": "2026-09-09",
|
|
@@ -1391,6 +1397,7 @@
|
|
|
1391
1397
|
"kopi": "2026-05-16",
|
|
1392
1398
|
"kroma": "2026-01-20",
|
|
1393
1399
|
"kujira": "2026-08-03",
|
|
1400
|
+
"lisk": "2026-11-02",
|
|
1394
1401
|
"loop": "2026-08-03",
|
|
1395
1402
|
"manta_atlantic": "2026-08-01",
|
|
1396
1403
|
"migaloo": "2026-02-12",
|
|
@@ -1409,6 +1416,7 @@
|
|
|
1409
1416
|
"nova": "2026-08-03",
|
|
1410
1417
|
"nuls": "2026-03-25",
|
|
1411
1418
|
"odyssey": "2026-09-02",
|
|
1419
|
+
"okexchain": "2026-01-01",
|
|
1412
1420
|
"onus": "2026-06-05",
|
|
1413
1421
|
"pacaswap": "2026-10-01",
|
|
1414
1422
|
"palm": "2026-10-13",
|