|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import { IDelegation } from "../interfaces/IDelegation.sol"; |
| 5 | +import { IEigenServiceManager } from "../interfaces/IEigenServiceManager.sol"; |
| 6 | +import { ISymbioticNetworkMiddleware } from "../interfaces/ISymbioticNetworkMiddleware.sol"; |
| 7 | + |
| 8 | +/// @title CapLens |
| 9 | +/// @notice Immutable lens to query slashable collateral and coverage by agent and epoch diff. Works for both Symbiotic and EigenLayer agents. |
| 10 | +contract CapLens { |
| 11 | + IDelegation public immutable delegation; |
| 12 | + |
| 13 | + constructor(address _delegation) { |
| 14 | + delegation = IDelegation(_delegation); |
| 15 | + } |
| 16 | + |
| 17 | + /// @notice Slashable collateral for an agent at (current epoch + epochDiff). Works for both Symbiotic and EigenLayer agents. |
| 18 | + /// @param _agent Agent address |
| 19 | + /// @param _epochDiff Epoch offset from current (0 = current epoch, +1 = next, -1 = previous) |
| 20 | + /// @return collateralValue Slashable collateral value in USD (8 decimals) |
| 21 | + /// @return collateral Slashable collateral amount in vault/strategy token units (Symbiotic only; 0 for Eigen as slashableCollateralByStrategy returns value only) |
| 22 | + function slashableCollateral(address _agent, int8 _epochDiff) |
| 23 | + external |
| 24 | + view |
| 25 | + returns (uint256 collateralValue, uint256 collateral) |
| 26 | + { |
| 27 | + ( |
| 28 | + bool isEigen, |
| 29 | + address middleware, |
| 30 | + address vault, |
| 31 | + address network, |
| 32 | + address oracle, |
| 33 | + uint48 timestamp, |
| 34 | + address strategy |
| 35 | + ) = _resolveParams(_agent, _epochDiff); |
| 36 | + if (middleware == address(0)) return (0, 0); |
| 37 | + |
| 38 | + if (isEigen) { |
| 39 | + if (strategy == address(0)) return (0, 0); |
| 40 | + collateralValue = IEigenServiceManager(middleware).slashableCollateralByStrategy(_agent, strategy); |
| 41 | + return (collateralValue, 0); |
| 42 | + } |
| 43 | + |
| 44 | + return |
| 45 | + ISymbioticNetworkMiddleware(middleware) |
| 46 | + .slashableCollateralByVault(network, _agent, vault, oracle, timestamp); |
| 47 | + } |
| 48 | + |
| 49 | + /// @notice Coverage for an agent at (current epoch + epochDiff). Works for both Symbiotic and EigenLayer agents. |
| 50 | + /// @param _agent Agent address |
| 51 | + /// @param _epochDiff Epoch offset from current (0 = current epoch, +1 = next, -1 = previous) |
| 52 | + /// @return collateralValue Coverage value in USD (8 decimals) |
| 53 | + /// @return collateral Coverage amount in vault/strategy token units |
| 54 | + function coverage(address _agent, int8 _epochDiff) |
| 55 | + external |
| 56 | + view |
| 57 | + returns (uint256 collateralValue, uint256 collateral) |
| 58 | + { |
| 59 | + ( |
| 60 | + bool isEigen, |
| 61 | + address middleware, |
| 62 | + address vault, |
| 63 | + address network, |
| 64 | + address oracle, |
| 65 | + uint48 timestamp, |
| 66 | + address strategy |
| 67 | + ) = _resolveParams(_agent, _epochDiff); |
| 68 | + if (middleware == address(0)) return (0, 0); |
| 69 | + |
| 70 | + if (isEigen) { |
| 71 | + if (strategy == address(0)) return (0, 0); |
| 72 | + return IEigenServiceManager(middleware).coverageByStrategy(_agent, strategy, oracle); |
| 73 | + } |
| 74 | + |
| 75 | + return ISymbioticNetworkMiddleware(middleware).coverageByVault(network, _agent, vault, oracle, timestamp); |
| 76 | + } |
| 77 | + |
| 78 | + /// @dev Resolve middleware type and all params for either Symbiotic or Eigen. |
| 79 | + /// @return isEigen True if the agent is on EigenLayer, false if Symbiotic |
| 80 | + /// @return middleware Middleware address |
| 81 | + /// @return vault For Symbiotic: vault address; for Eigen: address(0) |
| 82 | + /// @return network For Symbiotic: network address; for Eigen: address(0) |
| 83 | + /// @return oracle For Symbiotic: middleware oracle; for Eigen: IEigenServiceManager(middleware).oracle() |
| 84 | + /// @return timestamp For Symbiotic: timestamp; for Eigen: 0 |
| 85 | + /// @return strategy For Eigen: operator's strategy; for Symbiotic: address(0) |
| 86 | + function _resolveParams(address _agent, int8 _epochDiff) |
| 87 | + internal |
| 88 | + view |
| 89 | + returns ( |
| 90 | + bool isEigen, |
| 91 | + address middleware, |
| 92 | + address vault, |
| 93 | + address network, |
| 94 | + address oracle, |
| 95 | + uint48 timestamp, |
| 96 | + address strategy |
| 97 | + ) |
| 98 | + { |
| 99 | + middleware = delegation.networks(_agent); |
| 100 | + if (middleware == address(0)) return (false, address(0), address(0), address(0), address(0), 0, address(0)); |
| 101 | + |
| 102 | + uint256 epochDuration = delegation.epochDuration(); |
| 103 | + uint256 currentEpoch = block.timestamp / epochDuration; |
| 104 | + int256 targetEpoch = int256(currentEpoch) + int256(int8(_epochDiff)); |
| 105 | + if (targetEpoch >= 0) timestamp = uint48(uint256(targetEpoch) * epochDuration) + 1; |
| 106 | + |
| 107 | + try ISymbioticNetworkMiddleware(middleware).vaults(_agent) returns (address v) { |
| 108 | + vault = v; |
| 109 | + } catch { } |
| 110 | + |
| 111 | + if (vault == address(0)) { |
| 112 | + strategy = IEigenServiceManager(middleware).operatorToStrategy(_agent); |
| 113 | + oracle = IEigenServiceManager(middleware).oracle(); |
| 114 | + return (true, middleware, address(0), address(0), oracle, timestamp, strategy); |
| 115 | + } else { |
| 116 | + network = ISymbioticNetworkMiddleware(middleware).network(); |
| 117 | + oracle = ISymbioticNetworkMiddleware(middleware).oracle(); |
| 118 | + return (false, middleware, vault, network, oracle, timestamp, address(0)); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments