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

166 lines 10.7 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 { VersionCheckAbi } from '../abi/VersionCheck.js'; import { CsmSDKModule } from '../common/class-primitives/csm-sdk-module.js'; import { Cache, ErrorHandler, Logger } from '../common/decorators/index.js'; import { CACHE_MID } from '../common/index.js'; import { fetchJson } from '../common/utils/fetch-json.js'; import { onVersionError } from '../common/utils/on-error.js'; import { calculateShareLimit } from './calculate-share-limit.js'; import { findModuleDigest } from './find-module-digest.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 _getUsedOtherModule_decorators; return _a = class ModuleSDK extends _classSuper { get moduleContract() { return this.core.contractBaseModule; } async getStatus() { const csAccounting = this.core.contractAccounting; const [isPausedModule, isPausedAccounting] = await Promise.all([ this.moduleContract.read.isPaused(), csAccounting.read.isPaused(), ]); return { isPausedModule, isPausedAccounting, }; } async getVersions() { const contractNames = Object.keys(this.core.supportedVersions); const versionPromises = contractNames.map((contractName) => this.core .getContractWithAbi(contractName, VersionCheckAbi) .read.getInitializedVersion() .catch(onVersionError) .then((v) => [contractName, v])); const versions = await Promise.all(versionPromises); return Object.fromEntries(versions.filter(([, version]) => version !== 0n)); } async isVersionsSupported() { const versions = await this.getVersions(); return Object.entries(this.core.supportedVersions) .map(([key, [min, max]]) => { const current = versions[key]; return current !== undefined && current >= min && current <= max; }) .every(Boolean); } async getOperatorsCount() { return this.moduleContract.read.getNodeOperatorsCount(); } async getAllModulesDigests() { const digests = await this.core.contractStakingRouter.read.getAllStakingModuleDigests(); return digests.map((digest) => ({ ...digest, state: { ...digest.state, stakingModuleFee: BigInt(digest.state.stakingModuleFee), stakeShareLimit: BigInt(digest.state.stakeShareLimit), }, })); } 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 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(CACHE_MID)]; _getDigest_decorators = [Logger('Views:'), ErrorHandler()]; _getShareLimit_decorators = [Logger('Utils:')]; _getShareLimitStatus_decorators = [Logger('Utils:')]; _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, _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