UNPKG

@lidofinance/lido-ethereum-sdk

Version:

<div style="display: flex;" align="center"> <h1 align="center">Lido Ethereum SDK</h1> </div>

276 lines 20.6 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 { formatUnits, getContract, } from 'viem'; import { DUAL_GOVERNANCE_CONTRACT_NAMES, ERROR_CODE, invariant, } from '../common/index.js'; import { LidoSDKModule } from '../common/class-primitives/sdk-module.js'; import { Cache, Logger } from '../common/decorators/index.js'; import { dualGovernanceAbi } from './abi/DualGovernance.js'; import { escrowAbi } from './abi/Escrow.js'; import { emergencyProtectedTimelockAbi } from './abi/EmergencyProtectedTimelock.js'; import { stETH } from './abi/StETH.js'; import { dgConfigProviderAbi } from './abi/DGConfigProvider.js'; import { GovernanceState, } from './types.js'; let LidoSDKDualGovernance = (() => { var _a; let _classSuper = LidoSDKModule; let _instanceExtraInitializers = []; let _getContractEmergencyProtectedTimelockAddress_decorators; let _getContractEmergencyProtectedTimelock_decorators; let _getContractDualGovernance_decorators; let _getContractVetoSignallingEscrow_decorators; let _getContractStETH_decorators; let _getContractDualGovernanceConfigProvider_decorators; let _getGovernanceAddress_decorators; let _getVetoSignallingEscrowAddress_decorators; let _getStETHAddress_decorators; let _getDualGovernanceConfigProviderAddress_decorators; let _getVetoSignallingEscrowLockedAssets_decorators; let _getTotalStEthInEscrow_decorators; let _getDualGovernanceConfig_decorators; let _getTotalStETHSupply_decorators; let _calculateCurrentVetoSignallingThresholdProgress_decorators; let _getDualGovernanceState_decorators; let _getGovernanceWarningStatus_decorators; return _a = class LidoSDKDualGovernance extends _classSuper { // Contracts Addresses getContractEmergencyProtectedTimelockAddress() { return this.core.getDualGovernanceContractAddress(DUAL_GOVERNANCE_CONTRACT_NAMES.EPT); } // Contracts async getContractEmergencyProtectedTimelock() { const address = this.getContractEmergencyProtectedTimelockAddress(); invariant(address, `EmergencyProtectedTimelock is not supported on ${this.core.chain.name}`, ERROR_CODE.NOT_SUPPORTED); return getContract({ address, abi: emergencyProtectedTimelockAbi, client: this.core.rpcProvider, }); } async getContractDualGovernance() { const address = await this.getGovernanceAddress(); invariant(address, `Couldn't fetch DualGovernance address on chain ${this.core.chain.name}`, ERROR_CODE.READ_ERROR); return getContract({ address, abi: dualGovernanceAbi, client: this.core.rpcProvider, }); } async getContractVetoSignallingEscrow() { const address = await this.getVetoSignallingEscrowAddress(); invariant(address, `Couldn't fetch VetoSignallingEscrow address on chain ${this.core.chain.name}`, ERROR_CODE.READ_ERROR); return getContract({ address, abi: escrowAbi, client: this.core.rpcProvider, }); } async getContractStETH() { const address = await this.getStETHAddress(); invariant(address, `Couldn't fetch stETH address on chain ${this.core.chain.name}`, ERROR_CODE.READ_ERROR); return getContract({ address, abi: stETH, client: this.core.rpcProvider, }); } async getContractDualGovernanceConfigProvider() { const address = await this.getDualGovernanceConfigProviderAddress(); invariant(address, `Couldn't fetch DualGovernanceConfigProvider address on chain ${this.core.chain.name}`, ERROR_CODE.READ_ERROR); return getContract({ address, abi: dgConfigProviderAbi, client: this.core.rpcProvider, }); } // Views async getGovernanceAddress() { const emergencyProtectedTimelockContract = await this.getContractEmergencyProtectedTimelock(); return await emergencyProtectedTimelockContract.read.getGovernance(); } async getVetoSignallingEscrowAddress() { const dualGovernanceContract = await this.getContractDualGovernance(); return await dualGovernanceContract.read.getVetoSignallingEscrow(); } async getStETHAddress() { const vetoSignallingContract = await this.getContractVetoSignallingEscrow(); return await vetoSignallingContract.read.ST_ETH(); } async getDualGovernanceConfigProviderAddress() { const dualGovernanceContract = await this.getContractDualGovernance(); return await dualGovernanceContract.read.getConfigProvider(); } // Data Preparation async getVetoSignallingEscrowLockedAssets() { const vetoSignallingContract = await this.getContractVetoSignallingEscrow(); return await vetoSignallingContract.read.getSignallingEscrowDetails(); } async getTotalStEthInEscrow() { const stETHContract = await this.getContractStETH(); const vetoSignallingEscrowLockedAssets = await this.getVetoSignallingEscrowLockedAssets(); const unfinalizedShares = vetoSignallingEscrowLockedAssets.totalStETHLockedShares + vetoSignallingEscrowLockedAssets.totalUnstETHUnfinalizedShares; const pooledEthByShares = await stETHContract.read.getPooledEthByShares([ unfinalizedShares, ]); return (pooledEthByShares + vetoSignallingEscrowLockedAssets.totalUnstETHFinalizedETH); } async getDualGovernanceConfig() { const dualGovernanceConfigProvider = await this.getContractDualGovernanceConfigProvider(); return await dualGovernanceConfigProvider.read.getDualGovernanceConfig(); } async getTotalStETHSupply() { const stETHContract = await this.getContractStETH(); return await stETHContract.read.totalSupply(); } async calculateCurrentVetoSignallingThresholdProgress() { const [totalStETHSupply, dualGovernanceConfig, totalStEthInEscrow] = await Promise.all([ this.getTotalStETHSupply(), this.getDualGovernanceConfig(), this.getTotalStEthInEscrow(), ]); const targetPercent = Number(formatUnits(dualGovernanceConfig.firstSealRageQuitSupport, 16)); const currentSupport = totalStEthInEscrow; if (targetPercent < 0 || currentSupport < 0n || totalStETHSupply < 0n) { return { currentSupportPercent: 0 }; } if (totalStETHSupply === 0n) { const percent = currentSupport > 0n ? 100 : 0; return { currentSupportPercent: percent }; } const targetValue = (totalStETHSupply * BigInt(targetPercent)) / 100n; if (targetValue === 0n) { const percent = currentSupport > 0n ? 100 : 0; return { currentSupportPercent: percent }; } const thresholdSupportPercentBigInt = (currentSupport * 100n) / targetValue; let thresholdSupportPercentNumber = Number(thresholdSupportPercentBigInt); if (thresholdSupportPercentNumber > 100) thresholdSupportPercentNumber = 100; if (thresholdSupportPercentNumber < 0) thresholdSupportPercentNumber = 0; return { currentSupportPercent: thresholdSupportPercentNumber, }; } async getDualGovernanceState() { const dualGovernanceContract = await this.getContractDualGovernance(); const { persistedState } = await dualGovernanceContract.read.getStateDetails(); return persistedState; } async getGovernanceWarningStatus({ triggerPercent, }) { const currentGovernanceState = await this.getDualGovernanceState(); const { currentSupportPercent } = await this.calculateCurrentVetoSignallingThresholdProgress(); const NORMAL_STATES = [ GovernanceState.Normal, GovernanceState.VetoCooldown, ]; const BLOCKED_STATES = [ GovernanceState.VetoSignalling, GovernanceState.VetoSignallingDeactivation, GovernanceState.RageQuit, ]; if (BLOCKED_STATES.includes(currentGovernanceState)) { return { state: 'Blocked', currentVetoSupportPercent: currentSupportPercent, }; } if (NORMAL_STATES.includes(currentGovernanceState)) { if (currentSupportPercent > triggerPercent) { return { state: 'Warning', currentVetoSupportPercent: currentSupportPercent, }; } else { return { state: 'Normal', currentVetoSupportPercent: currentSupportPercent, }; } } return { state: 'Unknown', currentVetoSupportPercent: null, }; } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _getContractEmergencyProtectedTimelockAddress_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getContractEmergencyProtectedTimelock_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getContractDualGovernance_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getContractVetoSignallingEscrow_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getContractStETH_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getContractDualGovernanceConfigProvider_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getGovernanceAddress_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getVetoSignallingEscrowAddress_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getStETHAddress_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getDualGovernanceConfigProviderAddress_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getVetoSignallingEscrowLockedAssets_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getTotalStEthInEscrow_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getDualGovernanceConfig_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getTotalStETHSupply_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _calculateCurrentVetoSignallingThresholdProgress_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getDualGovernanceState_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id'])]; _getGovernanceWarningStatus_decorators = [Logger('Views:'), Cache(30 * 60 * 1000, ['core.chain.id', 'triggerPercent'])]; __esDecorate(_a, null, _getContractEmergencyProtectedTimelockAddress_decorators, { kind: "method", name: "getContractEmergencyProtectedTimelockAddress", static: false, private: false, access: { has: obj => "getContractEmergencyProtectedTimelockAddress" in obj, get: obj => obj.getContractEmergencyProtectedTimelockAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractEmergencyProtectedTimelock_decorators, { kind: "method", name: "getContractEmergencyProtectedTimelock", static: false, private: false, access: { has: obj => "getContractEmergencyProtectedTimelock" in obj, get: obj => obj.getContractEmergencyProtectedTimelock }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractDualGovernance_decorators, { kind: "method", name: "getContractDualGovernance", static: false, private: false, access: { has: obj => "getContractDualGovernance" in obj, get: obj => obj.getContractDualGovernance }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractVetoSignallingEscrow_decorators, { kind: "method", name: "getContractVetoSignallingEscrow", static: false, private: false, access: { has: obj => "getContractVetoSignallingEscrow" in obj, get: obj => obj.getContractVetoSignallingEscrow }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractStETH_decorators, { kind: "method", name: "getContractStETH", static: false, private: false, access: { has: obj => "getContractStETH" in obj, get: obj => obj.getContractStETH }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getContractDualGovernanceConfigProvider_decorators, { kind: "method", name: "getContractDualGovernanceConfigProvider", static: false, private: false, access: { has: obj => "getContractDualGovernanceConfigProvider" in obj, get: obj => obj.getContractDualGovernanceConfigProvider }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getGovernanceAddress_decorators, { kind: "method", name: "getGovernanceAddress", static: false, private: false, access: { has: obj => "getGovernanceAddress" in obj, get: obj => obj.getGovernanceAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getVetoSignallingEscrowAddress_decorators, { kind: "method", name: "getVetoSignallingEscrowAddress", static: false, private: false, access: { has: obj => "getVetoSignallingEscrowAddress" in obj, get: obj => obj.getVetoSignallingEscrowAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getStETHAddress_decorators, { kind: "method", name: "getStETHAddress", static: false, private: false, access: { has: obj => "getStETHAddress" in obj, get: obj => obj.getStETHAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getDualGovernanceConfigProviderAddress_decorators, { kind: "method", name: "getDualGovernanceConfigProviderAddress", static: false, private: false, access: { has: obj => "getDualGovernanceConfigProviderAddress" in obj, get: obj => obj.getDualGovernanceConfigProviderAddress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getVetoSignallingEscrowLockedAssets_decorators, { kind: "method", name: "getVetoSignallingEscrowLockedAssets", static: false, private: false, access: { has: obj => "getVetoSignallingEscrowLockedAssets" in obj, get: obj => obj.getVetoSignallingEscrowLockedAssets }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getTotalStEthInEscrow_decorators, { kind: "method", name: "getTotalStEthInEscrow", static: false, private: false, access: { has: obj => "getTotalStEthInEscrow" in obj, get: obj => obj.getTotalStEthInEscrow }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getDualGovernanceConfig_decorators, { kind: "method", name: "getDualGovernanceConfig", static: false, private: false, access: { has: obj => "getDualGovernanceConfig" in obj, get: obj => obj.getDualGovernanceConfig }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getTotalStETHSupply_decorators, { kind: "method", name: "getTotalStETHSupply", static: false, private: false, access: { has: obj => "getTotalStETHSupply" in obj, get: obj => obj.getTotalStETHSupply }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _calculateCurrentVetoSignallingThresholdProgress_decorators, { kind: "method", name: "calculateCurrentVetoSignallingThresholdProgress", static: false, private: false, access: { has: obj => "calculateCurrentVetoSignallingThresholdProgress" in obj, get: obj => obj.calculateCurrentVetoSignallingThresholdProgress }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getDualGovernanceState_decorators, { kind: "method", name: "getDualGovernanceState", static: false, private: false, access: { has: obj => "getDualGovernanceState" in obj, get: obj => obj.getDualGovernanceState }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getGovernanceWarningStatus_decorators, { kind: "method", name: "getGovernanceWarningStatus", static: false, private: false, access: { has: obj => "getGovernanceWarningStatus" in obj, get: obj => obj.getGovernanceWarningStatus }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); export { LidoSDKDualGovernance }; //# sourceMappingURL=dual-governance.js.map