UNPKG

@exromany/lido-csm-sdk

Version:

[![GitHub license](https://img.shields.io/github/license/lidofinance/lido-csm-sdk?color=limegreen)](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [![Version npm](https://img.shields.io/npm/v/@lidofinance/lido-csm-sdk?label=version)](h

165 lines 10.1 kB
"use strict"; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KeysWithStatusSDK = void 0; const viem_1 = require("viem"); const csm_sdk_module_js_1 = require("../common/class-primitives/csm-sdk-module.js"); const index_js_1 = require("../common/decorators/index.js"); const index_js_2 = require("../common/index.js"); const index_js_3 = require("../common/utils/index.js"); const cl_chunks_js_1 = require("./cl-chunks.js"); const compute_statuses_js_1 = require("./compute-statuses.js"); let KeysWithStatusSDK = (() => { var _a; let _classSuper = csm_sdk_module_js_1.CsmSDKModule; let _instanceExtraInitializers = []; let _getApiKeys_decorators; let _getApiKeysDuplicates_decorators; let _getClKeys_decorators; let _getClKeysStatus_decorators; let _getKeys_decorators; return _a = class KeysWithStatusSDK extends _classSuper { async getApiKeys(pubkeys) { const keysApi = this.core.keysApiLink; if (!keysApi) { throw new Error('Keys API link is not configured'); } if (pubkeys.length === 0) return []; const url = `${keysApi}/v1/keys/find`; const response = await (0, index_js_3.fetchJson)(url, { method: 'POST', body: JSON.stringify({ pubkeys }), headers: { 'Content-Type': 'application/json' }, }).catch(() => null); if (!response) return null; return response.data; } async getApiKeysDuplicates(nodeOperatorId) { const pubkeys = await this.bus.operator.getKeys(nodeOperatorId); const keys = await this.getApiKeys(pubkeys); if (!keys) return null; const moduleAddress = this.core.getContractAddress(this.core.moduleName); const duplicates = [ ...keys.map(({ key }) => key).filter(index_js_3.isNotUnique), ...keys .filter((key) => key.operatorIndex !== Number(nodeOperatorId) || !(0, viem_1.isAddressEqual)(key.moduleAddress, moduleAddress)) .map(({ key }) => key), ].filter(index_js_3.isUnique); return duplicates; } async getClKeys(pubkeys) { if (!this.core.clApiUrl || pubkeys.length === 0) { return null; } const urls = (0, cl_chunks_js_1.getClUrls)(pubkeys, this.core.clApiUrl); const results = await Promise.all(urls.map((url) => (0, index_js_3.fetchJson)(url))); return results.flatMap(({ data }) => data.map(cl_chunks_js_1.prepareKey)); } async getClKeysStatus(nodeOperatorId) { const pubkeys = await this.bus.operator.getKeys(nodeOperatorId); return this.getClKeys(pubkeys); } async getKeys(id) { const [info, unboundCount, keys, currentEpoch] = await Promise.all([ this.bus.operator.getInfo(id), this.bus.operator.getUnboundKeysCount(id), this.bus.operator.getKeys(id), this.bus.frame.getCurrentEpoch(), ]); const [withdrawalSubmitted, requestedToExit, duplicates, clKeysStatus, keysWithStrikes,] = await Promise.all([ this.bus.events.getWithdrawalSubmittedKeys(id, { maxBlocksDepth: index_js_2.MAX_BLOCKS_DEPTH_TWO_WEEKS, }), this.bus.events.getRequestedToExitKeys(id, { maxBlocksDepth: index_js_2.MAX_BLOCKS_DEPTH_TWO_WEEKS, }), this.getApiKeysDuplicates(id), this.getClKeysStatus(id), this.bus.strikes?.getKeysWithStrikes(id) ?? Promise.resolve([]), ]); const ejectableEpoch = currentEpoch - index_js_2.EJECTABLE_EPOCH_COUNT; return keys.map((pubkey, index) => { const prefilled = clKeysStatus?.find((item) => item.pubkey === pubkey); const keyStrikes = keysWithStrikes.find((item) => item.pubkey === pubkey); const statuses = (0, compute_statuses_js_1.computeStatuses)({ pubkey, keyIndex: index, info, prefilled, ejectableEpoch, unboundCount, duplicates, withdrawalSubmitted, requestedToExit, hasCLStatuses: !!clKeysStatus, hasStrikes: !!keyStrikes?.strikes.reduce((a, b) => a + b, 0), }); return { pubkey, index, statuses, validatorIndex: prefilled?.validatorIndex, strikes: keyStrikes?.strikes, }; }); } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _getApiKeys_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)(), (0, index_js_1.Cache)(index_js_2.CACHE_MID)]; _getApiKeysDuplicates_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)(), (0, index_js_1.Cache)(index_js_2.CACHE_MID)]; _getClKeys_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)(), (0, index_js_1.Cache)(index_js_2.CACHE_MID)]; _getClKeysStatus_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)(), (0, index_js_1.Cache)(index_js_2.CACHE_MID)]; _getKeys_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; __esDecorate(_a, null, _getApiKeys_decorators, { kind: "method", name: "getApiKeys", static: false, private: false, access: { has: obj => "getApiKeys" in obj, get: obj => obj.getApiKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getApiKeysDuplicates_decorators, { kind: "method", name: "getApiKeysDuplicates", static: false, private: false, access: { has: obj => "getApiKeysDuplicates" in obj, get: obj => obj.getApiKeysDuplicates }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getClKeys_decorators, { kind: "method", name: "getClKeys", static: false, private: false, access: { has: obj => "getClKeys" in obj, get: obj => obj.getClKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getClKeysStatus_decorators, { kind: "method", name: "getClKeysStatus", static: false, private: false, access: { has: obj => "getClKeysStatus" in obj, get: obj => obj.getClKeysStatus }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getKeys_decorators, { kind: "method", name: "getKeys", static: false, private: false, access: { has: obj => "getKeys" in obj, get: obj => obj.getKeys }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); exports.KeysWithStatusSDK = KeysWithStatusSDK; //# sourceMappingURL=keys-with-status-sdk.js.map