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

184 lines 11.9 kB
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; }; import { isAddressEqual } from 'viem'; import { CsmSDKModule } from '../common/class-primitives/csm-sdk-module.js'; import { Cache, ErrorHandler, Logger } from '../common/decorators/index.js'; import { CSM_CONTRACT_NAMES, SUPPORTED_VERSION_BY_CONTRACT, } from '../common/index.js'; import { fetchJson } from '../common/utils/fetch-json.js'; import { calculateShareLimit } from './calculate-share-limit.js'; import { findModuleDigest } from './find-module-digest.js'; import { onError } from './on-error.js'; import { ShareLimitStatus, } from './types.js'; let ModuleSDK = (() => { var _a; let _classSuper = CsmSDKModule; let _instanceExtraInitializers = []; let _getStatus_decorators; let _getVersions_decorators; let _getOperatorsCount_decorators; let _getAllModulesDigests_decorators; let _getDigest_decorators; let _getShareLimit_decorators; let _getShareLimitStatus_decorators; let _getQueues_decorators; let _getUsedOtherModule_decorators; return _a = class ModuleSDK extends _classSuper { get moduleContract() { return this.core.contractCSModule; } get stakingRouterContract() { return this.core.contractStakingRouter; } async getStatus() { const csAccounting = this.core.contractCSAccounting; const csModule = this.core.contractCSModule; const [isPausedModule, isPausedAccounting] = await Promise.all([ csModule.read.isPaused(), csAccounting.read.isPaused(), ]); return { isPausedModule, isPausedAccounting, }; } async getVersions() { const [module, accounting, feeDistributor, parametersRegistry, strikes, vettedGate,] = await Promise.all([ this.core.contractCSModule.read.getInitializedVersion().catch(onError), this.core.contractCSAccounting.read .getInitializedVersion() .catch(onError), this.core.contractCSFeeDistributor.read .getInitializedVersion() .catch(onError), this.core.contractCSParametersRegistry.read .getInitializedVersion() .catch(onError), this.core.contractCSStrikes.read.getInitializedVersion().catch(onError), this.core.contractVettedGate.read.getInitializedVersion().catch(onError), ]); return { [CSM_CONTRACT_NAMES.csModule]: module, [CSM_CONTRACT_NAMES.csAccounting]: accounting, [CSM_CONTRACT_NAMES.csFeeDistributor]: feeDistributor, [CSM_CONTRACT_NAMES.csParametersRegistry]: parametersRegistry, [CSM_CONTRACT_NAMES.csStrikes]: strikes, [CSM_CONTRACT_NAMES.vettedGate]: vettedGate, }; } async isVersionsSupported() { const versions = await this.getVersions(); return Object.entries(SUPPORTED_VERSION_BY_CONTRACT) .map(([key, [min, max]]) => { const current = versions[key]; return current >= min && current <= max; }) .every(Boolean); } async getOperatorsCount() { return this.moduleContract.read.getNodeOperatorsCount(); } async getAllModulesDigests() { const digests = await this.stakingRouterContract.read.getAllStakingModuleDigests(); return digests; } async getDigest() { const digests = await this.getAllModulesDigests(); return findModuleDigest(digests, this.core.moduleId); } async getShareLimit() { const digests = (await this.getAllModulesDigests()); return calculateShareLimit(digests, this.core.moduleId); } async getShareLimitStatus(shareLimitThreshold = 200n) { const info = await this.getShareLimit(); return info.activeLeft <= 0 ? ShareLimitStatus.REACHED : info.activeLeft - info.queue < 0 ? ShareLimitStatus.EXHAUSTED : info.activeLeft - info.queue < shareLimitThreshold ? ShareLimitStatus.APPROACHING : ShareLimitStatus.FAR; } async getQueues() { const queuesCount = await this.moduleContract.read.QUEUE_LOWEST_PRIORITY(); const pointers = await Promise.all(Array.from({ length: Number(queuesCount) }, (_, i) => this.moduleContract.read.depositQueuePointers([BigInt(i)]))); return pointers.map(([head, tail]) => ({ head, tail })); } async getUsedOtherModule(address) { const keysApi = this.core.keysApiLink; const csmId = this.core.moduleId; const { data: modules } = await fetchJson(`${keysApi}/v1/modules`, { headers: { 'Content-Type': 'application/json' }, }).catch(() => ({ data: [] })); const results = await Promise.all(modules.map(({ id }) => id === csmId ? undefined : fetchJson(`${keysApi}/v1/modules/${id}/operators`).catch(() => undefined))); const operators = results.flatMap((r) => r?.data.operators || []); const matchedOperator = operators.find((o) => isAddressEqual(o.rewardAddress, address)); const matchedModule = matchedOperator && modules.find((m) => isAddressEqual(m.stakingModuleAddress, matchedOperator.moduleAddress)); return matchedModule?.name ?? null; } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _getStatus_decorators = [Logger('Views:'), ErrorHandler()]; _getVersions_decorators = [Logger('Views:'), ErrorHandler()]; _getOperatorsCount_decorators = [Logger('Views:'), ErrorHandler()]; _getAllModulesDigests_decorators = [Logger('Views:'), ErrorHandler(), Cache(10 * 60 * 1000)]; _getDigest_decorators = [Logger('Views:'), ErrorHandler()]; _getShareLimit_decorators = [Logger('Utils:')]; _getShareLimitStatus_decorators = [Logger('Utils:')]; _getQueues_decorators = [Logger('Views:'), ErrorHandler()]; _getUsedOtherModule_decorators = [Logger('API:'), ErrorHandler()]; __esDecorate(_a, null, _getStatus_decorators, { kind: "method", name: "getStatus", static: false, private: false, access: { has: obj => "getStatus" in obj, get: obj => obj.getStatus }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getVersions_decorators, { kind: "method", name: "getVersions", static: false, private: false, access: { has: obj => "getVersions" in obj, get: obj => obj.getVersions }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getOperatorsCount_decorators, { kind: "method", name: "getOperatorsCount", static: false, private: false, access: { has: obj => "getOperatorsCount" in obj, get: obj => obj.getOperatorsCount }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getAllModulesDigests_decorators, { kind: "method", name: "getAllModulesDigests", static: false, private: false, access: { has: obj => "getAllModulesDigests" in obj, get: obj => obj.getAllModulesDigests }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getDigest_decorators, { kind: "method", name: "getDigest", static: false, private: false, access: { has: obj => "getDigest" in obj, get: obj => obj.getDigest }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getShareLimit_decorators, { kind: "method", name: "getShareLimit", static: false, private: false, access: { has: obj => "getShareLimit" in obj, get: obj => obj.getShareLimit }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getShareLimitStatus_decorators, { kind: "method", name: "getShareLimitStatus", static: false, private: false, access: { has: obj => "getShareLimitStatus" in obj, get: obj => obj.getShareLimitStatus }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getQueues_decorators, { kind: "method", name: "getQueues", static: false, private: false, access: { has: obj => "getQueues" in obj, get: obj => obj.getQueues }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getUsedOtherModule_decorators, { kind: "method", name: "getUsedOtherModule", static: false, private: false, access: { has: obj => "getUsedOtherModule" in obj, get: obj => obj.getUsedOtherModule }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); export { ModuleSDK }; //# sourceMappingURL=module-sdk.js.map