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

280 lines 16.6 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.KeysSDK = void 0; const lido_ethereum_sdk_1 = require("@lidofinance/lido-ethereum-sdk"); 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 parse_deposit_data_js_1 = require("../common/utils/parse-deposit-data.js"); const strip_permit_js_1 = require("../common/utils/strip-permit.js"); let KeysSDK = (() => { var _a; let _classSuper = csm_sdk_module_js_1.CsmSDKModule; let _instanceExtraInitializers = []; let _addKeysETH_decorators; let _addKeysStETH_decorators; let _addKeysWstETH_decorators; let _parseProps_decorators; let _getPermit_decorators; let _removeKeys_decorators; let _ejectKeys_decorators; let _ejectKeysByArray_decorators; let _getEjectFeePerKey_decorators; let _migrateToPriorityQueue_decorators; let _normalizeQueue_decorators; return _a = class KeysSDK extends _classSuper { get moduleContract() { return this.core.contractCSModule; } get ejectorContract() { return this.core.contractCSEjector; } async addKeysETH(props) { const { nodeOperatorId, amount: value, keysCount, publicKeys, signatures, permit, ...rest } = await this.parseProps(props); const args = [ rest.account.address, nodeOperatorId, keysCount, publicKeys, signatures, ]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.addValidatorKeysETH(args, { value, ...options, }), sendTransaction: (options) => this.moduleContract.write.addValidatorKeysETH(args, { value, ...options, }), }); } async addKeysStETH(props) { const { nodeOperatorId, amount, keysCount, publicKeys, signatures, permit: _permit, ...rest } = await this.parseProps(props); const { hash, permit } = await this.getPermit({ ...rest, token: index_js_2.TOKENS.steth, amount }, _permit); if (hash) return { hash }; const args = [ rest.account.address, nodeOperatorId, keysCount, publicKeys, signatures, permit, ]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.addValidatorKeysStETH(args, options), sendTransaction: (options) => this.moduleContract.write.addValidatorKeysStETH(args, options), }); } async addKeysWstETH(props) { const { nodeOperatorId, amount, keysCount, publicKeys, signatures, permit: _permit, ...rest } = await this.parseProps(props); const { hash, permit } = await this.getPermit({ ...rest, token: index_js_2.TOKENS.wsteth, amount }, _permit); if (hash) return { hash }; const args = [ rest.account.address, nodeOperatorId, keysCount, publicKeys, signatures, permit, ]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.addValidatorKeysWstETH(args, options), sendTransaction: (options) => this.moduleContract.write.addValidatorKeysWstETH(args, options), }); } async addKeys(props) { const { token } = props; if (props.amount === 0n) { return this.addKeysStETH(props); } switch (token) { case index_js_2.TOKENS.eth: return this.addKeysETH(props); case index_js_2.TOKENS.steth: return this.addKeysStETH(props); case index_js_2.TOKENS.wsteth: return this.addKeysWstETH(props); default: throw new lido_ethereum_sdk_1.SDKError({ message: 'unsupported token', code: lido_ethereum_sdk_1.ERROR_CODE.INVALID_ARGUMENT, }); } } async parseProps(props) { const { keysCount, publicKeys, signatures } = (0, parse_deposit_data_js_1.parseDepositData)(props.depositData); const account = await this.core.core.useAccount(props.account); return { ...props, keysCount, publicKeys, signatures, account, }; } async getPermit(props, preparedPermit) { if (preparedPermit) return { permit: (0, strip_permit_js_1.stripPermit)(preparedPermit) }; const result = await this.bus?.get('spending')?.signPermitOrApprove(props); return { hash: result?.hash, permit: (0, strip_permit_js_1.stripPermit)(result?.permit ?? index_js_2.EMPTY_PERMIT), }; } async removeKeys(props) { const { nodeOperatorId, startIndex, keysCount, ...rest } = props; const args = [nodeOperatorId, startIndex, keysCount]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.removeKeys(args, { ...options, }), sendTransaction: (options) => this.moduleContract.write.removeKeys(args, { ...options, }), }); } async ejectKeys(props) { const { nodeOperatorId, startIndex, keysCount, amount: value, refundRecipient = viem_1.zeroAddress, ...rest } = props; const args = [ nodeOperatorId, startIndex, keysCount, refundRecipient, ]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.ejectorContract.estimateGas.voluntaryEject(args, { value, ...options, }), sendTransaction: (options) => this.ejectorContract.write.voluntaryEject(args, { value, ...options, }), }); } async ejectKeysByArray(props) { const { nodeOperatorId, keyIndices, amount: value, refundRecipient = viem_1.zeroAddress, ...rest } = props; const args = [nodeOperatorId, keyIndices, refundRecipient]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.ejectorContract.estimateGas.voluntaryEjectByArray(args, { value, ...options, }), sendTransaction: (options) => this.ejectorContract.write.voluntaryEjectByArray(args, { value, ...options, }), }); } async getEjectFeePerKey() { const fee = await this.core.contractWithdrawalVault.read.getWithdrawalRequestFee(); const correctedFee = fee * index_js_2.EJECT_FEE_MULTIPLIEER; return correctedFee < index_js_2.EJECT_FEE_MIN_LIMIT ? index_js_2.EJECT_FEE_MIN_LIMIT : correctedFee; } async migrateToPriorityQueue(props) { const { nodeOperatorId, ...rest } = props; const args = [nodeOperatorId]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.migrateToPriorityQueue(args, { ...options, }), sendTransaction: (options) => this.moduleContract.write.migrateToPriorityQueue(args, { ...options, }), }); } async normalizeQueue(props) { const { nodeOperatorId, ...rest } = props; const args = [nodeOperatorId]; return this.core.performTransaction({ ...rest, getGasLimit: (options) => this.moduleContract.estimateGas.updateDepositableValidatorsCount(args, { ...options, }), sendTransaction: (options) => this.moduleContract.write.updateDepositableValidatorsCount(args, { ...options, }), }); } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _addKeysETH_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _addKeysStETH_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _addKeysWstETH_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _parseProps_decorators = [(0, index_js_1.Logger)('Utils:')]; _getPermit_decorators = [(0, index_js_1.Logger)('Utils:')]; _removeKeys_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _ejectKeys_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _ejectKeysByArray_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _getEjectFeePerKey_decorators = [(0, index_js_1.Logger)('Views:'), (0, index_js_1.ErrorHandler)()]; _migrateToPriorityQueue_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; _normalizeQueue_decorators = [(0, index_js_1.Logger)('Call:'), (0, index_js_1.ErrorHandler)()]; __esDecorate(_a, null, _addKeysETH_decorators, { kind: "method", name: "addKeysETH", static: false, private: false, access: { has: obj => "addKeysETH" in obj, get: obj => obj.addKeysETH }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _addKeysStETH_decorators, { kind: "method", name: "addKeysStETH", static: false, private: false, access: { has: obj => "addKeysStETH" in obj, get: obj => obj.addKeysStETH }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _addKeysWstETH_decorators, { kind: "method", name: "addKeysWstETH", static: false, private: false, access: { has: obj => "addKeysWstETH" in obj, get: obj => obj.addKeysWstETH }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _parseProps_decorators, { kind: "method", name: "parseProps", static: false, private: false, access: { has: obj => "parseProps" in obj, get: obj => obj.parseProps }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getPermit_decorators, { kind: "method", name: "getPermit", static: false, private: false, access: { has: obj => "getPermit" in obj, get: obj => obj.getPermit }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _removeKeys_decorators, { kind: "method", name: "removeKeys", static: false, private: false, access: { has: obj => "removeKeys" in obj, get: obj => obj.removeKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _ejectKeys_decorators, { kind: "method", name: "ejectKeys", static: false, private: false, access: { has: obj => "ejectKeys" in obj, get: obj => obj.ejectKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _ejectKeysByArray_decorators, { kind: "method", name: "ejectKeysByArray", static: false, private: false, access: { has: obj => "ejectKeysByArray" in obj, get: obj => obj.ejectKeysByArray }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getEjectFeePerKey_decorators, { kind: "method", name: "getEjectFeePerKey", static: false, private: false, access: { has: obj => "getEjectFeePerKey" in obj, get: obj => obj.getEjectFeePerKey }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _migrateToPriorityQueue_decorators, { kind: "method", name: "migrateToPriorityQueue", static: false, private: false, access: { has: obj => "migrateToPriorityQueue" in obj, get: obj => obj.migrateToPriorityQueue }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _normalizeQueue_decorators, { kind: "method", name: "normalizeQueue", static: false, private: false, access: { has: obj => "normalizeQueue" in obj, get: obj => obj.normalizeQueue }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); exports.KeysSDK = KeysSDK; //# sourceMappingURL=keys-sdk.js.map