@defillama/sdk 5.0.3 → 5.0.4
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/ChainApi.d.ts +2 -0
- package/build/ChainApi.js +9 -0
- package/build/general.js +1 -1
- package/build/generalUtil.js +11 -9
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/util/cache.d.ts +7 -0
- package/build/util/cache.js +63 -1
- package/build/util/index.d.ts +2 -4
- package/build/util/logs.d.ts +31 -0
- package/build/util/logs.js +215 -0
- package/package.json +1 -1
package/build/ChainApi.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Block, CallOptions, MulticallOptions, FetchListOptions, ByteCodeCallOpt
|
|
|
2
2
|
import Balances from "./Balances";
|
|
3
3
|
import { Chain } from "./general";
|
|
4
4
|
import { Provider } from "ethers";
|
|
5
|
+
import { GetLogsOptions } from "./util/logs";
|
|
5
6
|
export declare class ChainApi {
|
|
6
7
|
block?: Block;
|
|
7
8
|
chain: Chain | string;
|
|
@@ -55,5 +56,6 @@ export declare class ChainApi {
|
|
|
55
56
|
usdTokenBalances: BalancesV1;
|
|
56
57
|
rawTokenBalances: BalancesV1;
|
|
57
58
|
}>;
|
|
59
|
+
getLogs(params: GetLogsOptions): Promise<any[] | import("ethers").EventLog[]>;
|
|
58
60
|
}
|
|
59
61
|
export default ChainApi;
|
package/build/ChainApi.js
CHANGED
|
@@ -60,6 +60,7 @@ var debugLog_1 = require("./util/debugLog");
|
|
|
60
60
|
var generalUtil_1 = require("./generalUtil");
|
|
61
61
|
var multicall3_1 = require("./abi/multicall3");
|
|
62
62
|
var eth_1 = require("./eth");
|
|
63
|
+
var logs_1 = __importDefault(require("./util/logs"));
|
|
63
64
|
var nullAddress = '0x0000000000000000000000000000000000000000';
|
|
64
65
|
var ChainApi = /** @class */ (function () {
|
|
65
66
|
function ChainApi(params) {
|
|
@@ -257,6 +258,14 @@ var ChainApi = /** @class */ (function () {
|
|
|
257
258
|
});
|
|
258
259
|
});
|
|
259
260
|
};
|
|
261
|
+
ChainApi.prototype.getLogs = function (params) {
|
|
262
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
263
|
+
return __generator(this, function (_a) {
|
|
264
|
+
params.chain = this.chain;
|
|
265
|
+
return [2 /*return*/, (0, logs_1.default)(params)];
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
};
|
|
260
269
|
return ChainApi;
|
|
261
270
|
}());
|
|
262
271
|
exports.ChainApi = ChainApi;
|
package/build/general.js
CHANGED
|
@@ -20,7 +20,7 @@ function createProvider(name, rpcString, chainId) {
|
|
|
20
20
|
}
|
|
21
21
|
else {
|
|
22
22
|
try {
|
|
23
|
-
return new ethers_1.ethers.FallbackProvider(rpcList.map(function (url, i) { return ({ provider: getProviderObject(url), priority: i, chainId: chainId, }); }), networkish, { cacheTimeout: 5 * 1000 });
|
|
23
|
+
return new ethers_1.ethers.FallbackProvider(rpcList.map(function (url, i) { return ({ provider: getProviderObject(url), priority: i, chainId: chainId, }); }), networkish, { cacheTimeout: 5 * 1000, quorum: 1, eventWorkers: 1, eventQuorum: 1, });
|
|
24
24
|
}
|
|
25
25
|
catch (e) {
|
|
26
26
|
// debugLog(`Error creating provider for ${name} with RPCs: ${rpcList.join(', ')}`)
|
package/build/generalUtil.js
CHANGED
|
@@ -218,7 +218,7 @@ function getProviderUrl(provider) {
|
|
|
218
218
|
}
|
|
219
219
|
exports.getProviderUrl = getProviderUrl;
|
|
220
220
|
function formErrorString(e, errorParams) {
|
|
221
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
221
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
222
222
|
if (errorParams === void 0) { errorParams = {}; }
|
|
223
223
|
if (!e) {
|
|
224
224
|
if (errorParams.promisePoolErrors) {
|
|
@@ -242,14 +242,14 @@ function formErrorString(e, errorParams) {
|
|
|
242
242
|
var errorString = e.toString();
|
|
243
243
|
if (typeof e !== 'object')
|
|
244
244
|
return errorString;
|
|
245
|
-
if (((e.reason || e.method) && e.code) || errorParams.result === '0x') { // ethers.js error
|
|
246
|
-
var method = (_d = e.method) !== null && _d !== void 0 ? _d : errorParams.abi;
|
|
245
|
+
if (((e.reason || e.method || e.shortMessage) && e.code) || errorParams.result === '0x') { // ethers.js error
|
|
246
|
+
var method = (_g = (_d = e.method) !== null && _d !== void 0 ? _d : (_f = (_e = e.info) === null || _e === void 0 ? void 0 : _e.request) === null || _f === void 0 ? void 0 : _f.method) !== null && _g !== void 0 ? _g : errorParams.abi;
|
|
247
247
|
if (e.body) {
|
|
248
248
|
try {
|
|
249
249
|
e.body = JSON.parse(e.body);
|
|
250
250
|
}
|
|
251
251
|
catch (e) { }
|
|
252
|
-
if ((
|
|
252
|
+
if ((_j = (_h = e.body) === null || _h === void 0 ? void 0 : _h.error) === null || _j === void 0 ? void 0 : _j.message)
|
|
253
253
|
return e.body.error.message;
|
|
254
254
|
}
|
|
255
255
|
if (!method && e.requestBody) {
|
|
@@ -257,11 +257,11 @@ function formErrorString(e, errorParams) {
|
|
|
257
257
|
e.requestBody = JSON.parse(e.requestBody);
|
|
258
258
|
}
|
|
259
259
|
catch (e) { }
|
|
260
|
-
method = (
|
|
260
|
+
method = (_k = e.requestBody) === null || _k === void 0 ? void 0 : _k.method;
|
|
261
261
|
}
|
|
262
|
-
if (!e.provider && errorParams.provider)
|
|
262
|
+
else if (!e.provider && errorParams.provider)
|
|
263
263
|
e.provider = errorParams.provider;
|
|
264
|
-
var providerUrl = getProviderUrl((
|
|
264
|
+
var providerUrl = getProviderUrl((_l = e.provider) !== null && _l !== void 0 ? _l : errorParams.provider);
|
|
265
265
|
if (e.serverError)
|
|
266
266
|
return "host: ".concat(providerUrl, " ").concat(e.serverError.toString());
|
|
267
267
|
if (e.results) {
|
|
@@ -275,7 +275,7 @@ function formErrorString(e, errorParams) {
|
|
|
275
275
|
var extraInfo = 'target: ' + errorParams.target;
|
|
276
276
|
if (errorParams.params && errorParams.params.length)
|
|
277
277
|
extraInfo += shortenString(' params: ' + errorParams.params.join(', '));
|
|
278
|
-
return "Failed to call ".concat(method, " ").concat(extraInfo, " on chain: [").concat(errorParams.chain, "] rpc: ").concat(providerUrl, " call reverted ").concat((
|
|
278
|
+
return "Failed to call: ".concat(method, " ").concat(extraInfo, " on chain: [").concat(errorParams.chain, "] rpc: ").concat(providerUrl, " call reverted, reason: ").concat((_o = (_m = e.errorName) !== null && _m !== void 0 ? _m : e.shortMessage) !== null && _o !== void 0 ? _o : '', " ").concat((_p = e.errorArgs) !== null && _p !== void 0 ? _p : '');
|
|
279
279
|
}
|
|
280
280
|
if (method === 'eth_blockNumber')
|
|
281
281
|
return "host: ".concat(providerUrl, " reason: ").concat(e.reason, " code: ").concat(e.code);
|
|
@@ -293,7 +293,8 @@ function shortenString(str, length) {
|
|
|
293
293
|
function formError(e, errorParams) {
|
|
294
294
|
var _a;
|
|
295
295
|
if (errorParams === void 0) { errorParams = {}; }
|
|
296
|
-
|
|
296
|
+
var errorParamsSet = Object.keys(errorParams).length;
|
|
297
|
+
if ((e === null || e === void 0 ? void 0 : e._isCustomError) || (!errorParamsSet && typeof e === 'object' && !Object.keys(e).length))
|
|
297
298
|
return e; // already formatted or vannila error
|
|
298
299
|
var error = new Error(formErrorString(e, errorParams));
|
|
299
300
|
try {
|
|
@@ -304,6 +305,7 @@ function formError(e, errorParams) {
|
|
|
304
305
|
}
|
|
305
306
|
catch (e) { }
|
|
306
307
|
error._isCustomError = true;
|
|
308
|
+
error.message = shortenString(error.message, 420);
|
|
307
309
|
error.stack = '';
|
|
308
310
|
return error;
|
|
309
311
|
}
|
package/build/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { Balances } from './Balances';
|
|
|
4
4
|
export { getProvider, setProvider } from './general';
|
|
5
5
|
export * as util from "./generalUtil";
|
|
6
6
|
export * as cache from "./util/cache";
|
|
7
|
+
export { getLogs as getEventLogs } from "./util/logs";
|
|
7
8
|
export * as sdkCache from "./util/internal-cache";
|
|
8
9
|
export * as api from "./api";
|
|
9
10
|
export * as api2 from "./api2";
|
package/build/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.Balances = exports.ChainApi = void 0;
|
|
26
|
+
exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.getEventLogs = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.Balances = exports.ChainApi = void 0;
|
|
27
27
|
var debugLog_1 = require("./util/debugLog");
|
|
28
28
|
var ChainApi_1 = require("./ChainApi");
|
|
29
29
|
Object.defineProperty(exports, "ChainApi", { enumerable: true, get: function () { return ChainApi_1.ChainApi; } });
|
|
@@ -34,6 +34,8 @@ Object.defineProperty(exports, "getProvider", { enumerable: true, get: function
|
|
|
34
34
|
Object.defineProperty(exports, "setProvider", { enumerable: true, get: function () { return general_1.setProvider; } });
|
|
35
35
|
exports.util = __importStar(require("./generalUtil"));
|
|
36
36
|
exports.cache = __importStar(require("./util/cache"));
|
|
37
|
+
var logs_1 = require("./util/logs");
|
|
38
|
+
Object.defineProperty(exports, "getEventLogs", { enumerable: true, get: function () { return logs_1.getLogs; } });
|
|
37
39
|
exports.sdkCache = __importStar(require("./util/internal-cache"));
|
|
38
40
|
exports.api = __importStar(require("./api"));
|
|
39
41
|
exports.api2 = __importStar(require("./api2"));
|
package/build/util/cache.d.ts
CHANGED
|
@@ -17,4 +17,11 @@ export declare function getTempLocalCache({ file, defaultData, clearAfter }: {
|
|
|
17
17
|
defaultData?: any;
|
|
18
18
|
clearAfter?: number;
|
|
19
19
|
}): any;
|
|
20
|
+
export declare function deleteCache(file: string): Promise<void>;
|
|
21
|
+
export declare function readExpiringJsonCache(file: string): Promise<any>;
|
|
22
|
+
export declare function writeExpiringJsonCache(file: string, data: any, { expireAfter, // cache for 1 day by default
|
|
23
|
+
expiryTimestamp, }: {
|
|
24
|
+
expireAfter?: number;
|
|
25
|
+
expiryTimestamp?: number;
|
|
26
|
+
}): Promise<void>;
|
|
20
27
|
export {};
|
package/build/util/cache.js
CHANGED
|
@@ -37,7 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
var _a;
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.getTempLocalCache = exports.compressCache = exports.parseCache = exports.writeCache = exports.readCache = exports.currentVersion = void 0;
|
|
40
|
+
exports.writeExpiringJsonCache = exports.readExpiringJsonCache = exports.deleteCache = exports.getTempLocalCache = exports.compressCache = exports.parseCache = exports.writeCache = exports.readCache = exports.currentVersion = void 0;
|
|
41
41
|
var debugLog_1 = require("./debugLog");
|
|
42
42
|
var r2_1 = require("./r2");
|
|
43
43
|
var zlib_1 = require("zlib");
|
|
@@ -273,3 +273,65 @@ function getTempLocalCache(_a) {
|
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
exports.getTempLocalCache = getTempLocalCache;
|
|
276
|
+
// this removes data only from the file cache, not from the R2 cache
|
|
277
|
+
function deleteCache(file) {
|
|
278
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
279
|
+
var filePath;
|
|
280
|
+
return __generator(this, function (_a) {
|
|
281
|
+
switch (_a.label) {
|
|
282
|
+
case 0:
|
|
283
|
+
filePath = getFilePath(file);
|
|
284
|
+
return [4 /*yield*/, fs.unlink(filePath)];
|
|
285
|
+
case 1:
|
|
286
|
+
_a.sent();
|
|
287
|
+
return [2 /*return*/];
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
exports.deleteCache = deleteCache;
|
|
293
|
+
function readExpiringJsonCache(file) {
|
|
294
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
295
|
+
var options, data;
|
|
296
|
+
return __generator(this, function (_a) {
|
|
297
|
+
switch (_a.label) {
|
|
298
|
+
case 0:
|
|
299
|
+
options = { readFromR2Cache: false, skipR2Cache: true };
|
|
300
|
+
file = 'expiring/' + file;
|
|
301
|
+
return [4 /*yield*/, readCache(file, options)];
|
|
302
|
+
case 1:
|
|
303
|
+
data = _a.sent();
|
|
304
|
+
if (!data || Object.keys(data).length === 0)
|
|
305
|
+
return [2 /*return*/, null];
|
|
306
|
+
if (!(data.expiryTimestamp < (Date.now() / 1e3))) return [3 /*break*/, 3];
|
|
307
|
+
return [4 /*yield*/, deleteCache(file)];
|
|
308
|
+
case 2:
|
|
309
|
+
_a.sent();
|
|
310
|
+
return [2 /*return*/, null];
|
|
311
|
+
case 3: return [2 /*return*/, data.data];
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
exports.readExpiringJsonCache = readExpiringJsonCache;
|
|
317
|
+
function writeExpiringJsonCache(file, data, _a) {
|
|
318
|
+
var _b = _a.expireAfter, expireAfter = _b === void 0 ? 60 * 60 * 24 : _b, // cache for 1 day by default
|
|
319
|
+
expiryTimestamp = _a.expiryTimestamp;
|
|
320
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
321
|
+
var options;
|
|
322
|
+
return __generator(this, function (_c) {
|
|
323
|
+
switch (_c.label) {
|
|
324
|
+
case 0:
|
|
325
|
+
file = 'expiring/' + file;
|
|
326
|
+
if (!expiryTimestamp)
|
|
327
|
+
expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
|
|
328
|
+
options = { skipR2CacheWrite: true };
|
|
329
|
+
return [4 /*yield*/, writeCache(file, { data: data, expiryTimestamp: expiryTimestamp }, options)];
|
|
330
|
+
case 1:
|
|
331
|
+
_c.sent();
|
|
332
|
+
return [2 /*return*/];
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
exports.writeExpiringJsonCache = writeExpiringJsonCache;
|
package/build/util/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Chain } from "../general";
|
|
2
2
|
import type { Address } from "../types";
|
|
3
|
-
import {
|
|
3
|
+
import { EventLog } from "ethers";
|
|
4
4
|
import runInPromisePoolOrig from "./promisePool";
|
|
5
5
|
export { getLatestBlock, getTimestamp, lookupBlock, } from "./blocks";
|
|
6
6
|
export declare const runInPromisePool: typeof runInPromisePoolOrig;
|
|
@@ -14,9 +14,7 @@ export declare function getLogs(params: {
|
|
|
14
14
|
topics?: string[];
|
|
15
15
|
chain?: Chain;
|
|
16
16
|
}): Promise<{
|
|
17
|
-
output:
|
|
18
|
-
} | {
|
|
19
|
-
output: ethers.Log[];
|
|
17
|
+
output: EventLog[];
|
|
20
18
|
}>;
|
|
21
19
|
export declare function normalizeAddress(address: string): string;
|
|
22
20
|
export declare function normalizePrefixes(address: string): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { EventLog } from "ethers";
|
|
2
|
+
import { Address } from "../types";
|
|
3
|
+
export type GetLogsOptions = {
|
|
4
|
+
target: Address;
|
|
5
|
+
topic?: string;
|
|
6
|
+
keys?: string[];
|
|
7
|
+
fromBlock?: number;
|
|
8
|
+
toBlock?: number;
|
|
9
|
+
topics?: string[];
|
|
10
|
+
extraTopics?: string[];
|
|
11
|
+
chain?: string;
|
|
12
|
+
eventAbi?: string | any;
|
|
13
|
+
fromTimestamp?: number;
|
|
14
|
+
toTimestamp?: number;
|
|
15
|
+
skipCache?: boolean;
|
|
16
|
+
skipCacheRead?: boolean;
|
|
17
|
+
entireLog?: boolean;
|
|
18
|
+
cacheInCloud?: boolean;
|
|
19
|
+
onlyArgs?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export declare function getLogs({ target, chain, fromBlock, toBlock, fromTimestamp, toTimestamp, eventAbi, topic, topics, extraTopics, // can be passed as input as extra filter arguments
|
|
22
|
+
skipCache, skipCacheRead, entireLog, cacheInCloud, onlyArgs, keys, }: GetLogsOptions): Promise<EventLog[] | any[]>;
|
|
23
|
+
export type logCache = {
|
|
24
|
+
logs: EventLog[];
|
|
25
|
+
metadata?: {
|
|
26
|
+
version: string;
|
|
27
|
+
fromBlock: number;
|
|
28
|
+
toBlock: number;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export default getLogs;
|
|
@@ -0,0 +1,215 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getLogs = void 0;
|
|
40
|
+
var _1 = require(".");
|
|
41
|
+
var ethers_1 = require("ethers");
|
|
42
|
+
var blocks_1 = require("./blocks");
|
|
43
|
+
var cache_1 = require("./cache");
|
|
44
|
+
var debugLog_1 = require("./debugLog");
|
|
45
|
+
var currentVersion = 'v2';
|
|
46
|
+
function getLogs(_a) {
|
|
47
|
+
var target = _a.target, _b = _a.chain, chain = _b === void 0 ? 'ethereum' : _b, fromBlock = _a.fromBlock, toBlock = _a.toBlock, fromTimestamp = _a.fromTimestamp, toTimestamp = _a.toTimestamp, eventAbi = _a.eventAbi, topic = _a.topic, topics = _a.topics, extraTopics = _a.extraTopics, // can be passed as input as extra filter arguments
|
|
48
|
+
_c = _a.skipCache, // can be passed as input as extra filter arguments
|
|
49
|
+
skipCache = _c === void 0 ? false : _c, _d = _a.skipCacheRead, skipCacheRead = _d === void 0 ? false : _d, _e = _a.entireLog, entireLog = _e === void 0 ? false : _e, _f = _a.cacheInCloud, cacheInCloud = _f === void 0 ? false : _f, _g = _a.onlyArgs, onlyArgs = _g === void 0 ? false : _g, _h = _a.keys, keys = _h === void 0 ? [] : _h;
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
function addLogsToCache(fromBlock, toBlock) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
+
var metadata, logs, logIndices;
|
|
54
|
+
var _a;
|
|
55
|
+
return __generator(this, function (_b) {
|
|
56
|
+
switch (_b.label) {
|
|
57
|
+
case 0:
|
|
58
|
+
metadata = cacheData.metadata;
|
|
59
|
+
return [4 /*yield*/, (0, _1.getLogs)({
|
|
60
|
+
chain: chain,
|
|
61
|
+
target: target,
|
|
62
|
+
topic: topic,
|
|
63
|
+
keys: keys,
|
|
64
|
+
topics: topics,
|
|
65
|
+
fromBlock: fromBlock,
|
|
66
|
+
toBlock: toBlock,
|
|
67
|
+
})];
|
|
68
|
+
case 1:
|
|
69
|
+
logs = (_b.sent()).output;
|
|
70
|
+
(_a = cacheData.logs).push.apply(_a, logs);
|
|
71
|
+
logIndices = new Set();
|
|
72
|
+
cacheData.logs = cacheData.logs.filter(function (i) {
|
|
73
|
+
var _a;
|
|
74
|
+
var key = i.transactionHash + ((_a = i.logIndex) !== null && _a !== void 0 ? _a : i.index); // ethers v5 had logIndex, ethers v6 has index
|
|
75
|
+
if (!(i.hasOwnProperty('logIndex') || i.hasOwnProperty('index')) || !i.hasOwnProperty('transactionHash')) {
|
|
76
|
+
(0, debugLog_1.debugLog)(i, i.logIndex, i.index, i.transactionHash);
|
|
77
|
+
throw new Error('Missing crucial field');
|
|
78
|
+
}
|
|
79
|
+
if (logIndices.has(key))
|
|
80
|
+
return false;
|
|
81
|
+
logIndices.add(key);
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
if (!metadata.fromBlock || fromBlock < metadata.fromBlock)
|
|
85
|
+
metadata.fromBlock = fromBlock;
|
|
86
|
+
if (!metadata.toBlock || toBlock > metadata.toBlock)
|
|
87
|
+
metadata.toBlock = toBlock;
|
|
88
|
+
if (!!skipCache) return [3 /*break*/, 3];
|
|
89
|
+
return [4 /*yield*/, (0, cache_1.writeCache)(getFile(), cacheData, { skipR2CacheWrite: !cacheInCloud })];
|
|
90
|
+
case 2:
|
|
91
|
+
_b.sent();
|
|
92
|
+
_b.label = 3;
|
|
93
|
+
case 3: return [2 /*return*/];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function _getCache() {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
100
|
+
var key, defaultRes, cache;
|
|
101
|
+
return __generator(this, function (_a) {
|
|
102
|
+
switch (_a.label) {
|
|
103
|
+
case 0:
|
|
104
|
+
key = getFile();
|
|
105
|
+
defaultRes = {
|
|
106
|
+
logs: [],
|
|
107
|
+
};
|
|
108
|
+
if (skipCache || skipCacheRead)
|
|
109
|
+
return [2 /*return*/, defaultRes];
|
|
110
|
+
return [4 /*yield*/, (0, cache_1.readCache)(key, { skipR2Cache: !cacheInCloud })];
|
|
111
|
+
case 1:
|
|
112
|
+
cache = _a.sent();
|
|
113
|
+
if (!cache.metadata || cache.metadata.version !== currentVersion)
|
|
114
|
+
return [2 /*return*/, defaultRes];
|
|
115
|
+
return [2 /*return*/, cache];
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function getFile() {
|
|
121
|
+
var extraKey = topics === null || topics === void 0 ? void 0 : topics.join('-').toLowerCase();
|
|
122
|
+
if (!extraKey)
|
|
123
|
+
throw new Error('extraKey is required');
|
|
124
|
+
if (keys.length)
|
|
125
|
+
extraKey += '-' + keys.join('-').toLowerCase();
|
|
126
|
+
return "event-logs/".concat(chain, "/").concat(target.toLowerCase(), "/").concat(target.toLowerCase());
|
|
127
|
+
}
|
|
128
|
+
// we need to form filter topic with indexed keyword, else it messes up generated topic string
|
|
129
|
+
function toFilterTopic(topic) {
|
|
130
|
+
if (typeof topic === 'string') {
|
|
131
|
+
if (topic.startsWith('0x'))
|
|
132
|
+
return topic;
|
|
133
|
+
topic = new ethers_1.Interface([topic]);
|
|
134
|
+
}
|
|
135
|
+
var fragment = topic.fragments[0];
|
|
136
|
+
return (0, ethers_1.id)("".concat(fragment.name, "(").concat(fragment.inputs.map(function (i) { return i.type; }).join(','), ")"));
|
|
137
|
+
}
|
|
138
|
+
var iface, cacheData, isFirstRun, metadata, logs;
|
|
139
|
+
return __generator(this, function (_j) {
|
|
140
|
+
switch (_j.label) {
|
|
141
|
+
case 0:
|
|
142
|
+
if (!target)
|
|
143
|
+
throw new Error('target is required');
|
|
144
|
+
if (!fromBlock && !fromTimestamp)
|
|
145
|
+
throw new Error('fromBlock or fromTimestamp is required');
|
|
146
|
+
if (!toBlock && !toTimestamp)
|
|
147
|
+
throw new Error('toBlock or toTimestamp is required');
|
|
148
|
+
if (!!fromBlock) return [3 /*break*/, 2];
|
|
149
|
+
return [4 /*yield*/, (0, blocks_1.getBlock)(chain, fromTimestamp)];
|
|
150
|
+
case 1:
|
|
151
|
+
fromBlock = (_j.sent()).block;
|
|
152
|
+
_j.label = 2;
|
|
153
|
+
case 2:
|
|
154
|
+
if (!!toBlock) return [3 /*break*/, 4];
|
|
155
|
+
return [4 /*yield*/, (0, blocks_1.getBlock)(chain, toTimestamp)];
|
|
156
|
+
case 3:
|
|
157
|
+
toBlock = (_j.sent()).block;
|
|
158
|
+
_j.label = 4;
|
|
159
|
+
case 4:
|
|
160
|
+
if ((!topics || !topics.length) && !topic) {
|
|
161
|
+
if (topic) {
|
|
162
|
+
topic = toFilterTopic(topic);
|
|
163
|
+
}
|
|
164
|
+
else if (eventAbi) {
|
|
165
|
+
iface = new ethers_1.Interface([eventAbi]);
|
|
166
|
+
topic = toFilterTopic(iface);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
throw new Error('eventAbi | topic | topics are required');
|
|
170
|
+
}
|
|
171
|
+
topics = [topic];
|
|
172
|
+
if (extraTopics)
|
|
173
|
+
topics.push.apply(topics, extraTopics);
|
|
174
|
+
}
|
|
175
|
+
return [4 /*yield*/, _getCache()];
|
|
176
|
+
case 5:
|
|
177
|
+
cacheData = _j.sent();
|
|
178
|
+
isFirstRun = !cacheData.metadata;
|
|
179
|
+
if (isFirstRun)
|
|
180
|
+
cacheData.metadata = { version: currentVersion };
|
|
181
|
+
if (!isFirstRun) return [3 /*break*/, 7];
|
|
182
|
+
return [4 /*yield*/, addLogsToCache(fromBlock, toBlock)];
|
|
183
|
+
case 6:
|
|
184
|
+
_j.sent();
|
|
185
|
+
return [3 /*break*/, 11];
|
|
186
|
+
case 7:
|
|
187
|
+
metadata = cacheData.metadata;
|
|
188
|
+
if (!(fromBlock < metadata.fromBlock)) return [3 /*break*/, 9];
|
|
189
|
+
return [4 /*yield*/, addLogsToCache(fromBlock, metadata.fromBlock - 1)];
|
|
190
|
+
case 8:
|
|
191
|
+
_j.sent();
|
|
192
|
+
_j.label = 9;
|
|
193
|
+
case 9:
|
|
194
|
+
if (!(toBlock > metadata.toBlock)) return [3 /*break*/, 11];
|
|
195
|
+
return [4 /*yield*/, addLogsToCache(metadata.toBlock + 1, toBlock)];
|
|
196
|
+
case 10:
|
|
197
|
+
_j.sent();
|
|
198
|
+
_j.label = 11;
|
|
199
|
+
case 11:
|
|
200
|
+
logs = cacheData.logs.filter(function (i) {
|
|
201
|
+
if (i.blockNumber < fromBlock)
|
|
202
|
+
return false;
|
|
203
|
+
if (i.blockNumber > toBlock)
|
|
204
|
+
return false;
|
|
205
|
+
return true;
|
|
206
|
+
});
|
|
207
|
+
if (!eventAbi || entireLog)
|
|
208
|
+
return [2 /*return*/, logs];
|
|
209
|
+
return [2 /*return*/, logs.map(function (i) { return iface.parseLog(i); }).map(function (i) { return onlyArgs ? i.args : i; })];
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
exports.getLogs = getLogs;
|
|
215
|
+
exports.default = getLogs;
|