From cf7e4bbeaa40c3e5be9f55045545e464813c48f0 Mon Sep 17 00:00:00 2001 From: "clandestine.eth" <96172957+0xClandestine@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:43:21 -0400 Subject: [PATCH] refactor: more explicit share names --- src/contracts/core/DelegationManager.sol | 33 ++++++++++--------- .../interfaces/IDelegationManager.sol | 2 +- src/contracts/libraries/SlashingLib.sol | 28 ++++++++-------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/contracts/core/DelegationManager.sol b/src/contracts/core/DelegationManager.sol index 794260ed1..2ead1ca5e 100644 --- a/src/contracts/core/DelegationManager.sol +++ b/src/contracts/core/DelegationManager.sol @@ -393,7 +393,7 @@ contract DelegationManager is operator: operator, staker: staker, strategy: strategy, - existingDepositShares: DepositShares.wrap(existingDepositShares), + existingDepositShares: StakeManagerShares.wrap(existingDepositShares), addedShares: Shares.wrap(addedShares), totalMagnitude: totalMagnitude }); @@ -516,7 +516,7 @@ contract DelegationManager is operator: operator, staker: staker, strategy: strategies[i], - existingDepositShares: DepositShares.wrap(0), + existingDepositShares: StakeManagerShares.wrap(0), addedShares: Shares.wrap(shares[i]), totalMagnitude: totalMagnitudes[i] }); @@ -589,7 +589,7 @@ contract DelegationManager is address operator, address staker, IStrategy strategy, - DepositShares existingDepositShares, + StakeManagerShares existingDepositShares, Shares addedShares, uint64 totalMagnitude ) internal { @@ -602,7 +602,7 @@ contract DelegationManager is }); // based on total magnitude, update operators stakeShares - uint256 stakeShares = StakeShares.unwrap(addedShares.toStakeShares(totalMagnitude)); + uint256 stakeShares = DelegationManagerShares.unwrap(addedShares.toStakeShares(totalMagnitude)); operatorStakeShares[operator][strategy] += stakeShares; // TODO: What to do about event wrt scaling? @@ -643,7 +643,7 @@ contract DelegationManager is require(staker != address(0), InputAddressZero()); require(strategies.length != 0, InputArrayLengthZero()); - StakeShares[] memory stakeSharesToWithdraw = new StakeShares[](strategies.length); + DelegationManagerShares[] memory stakeSharesToWithdraw = new DelegationManagerShares[](strategies.length); // Remove shares from staker and operator // Each of these operations fail if we attempt to remove more shares than exist @@ -652,14 +652,14 @@ contract DelegationManager is // stakeShares for staker to place into queueWithdrawal stakeSharesToWithdraw[i] = Shares.wrap(sharesToWithdraw[i]).toStakeShares(totalMagnitudes[i]); - DepositShares depositSharesToWithdraw = + StakeManagerShares depositSharesToWithdraw = stakeSharesToWithdraw[i].toDepositShares(depositScalingFactors[staker][strategies[i]]); // TODO: maybe have a getter to get shares for all strategies, like getDelegatableShares // check sharesToWithdraw is valid // but for inputted strategies uint256 depositShares = shareManager.stakerStrategyShares(staker, strategies[i]); - require(DepositShares.unwrap(depositSharesToWithdraw) <= depositShares, WithdrawalExeedsMax()); + require(StakeManagerShares.unwrap(depositSharesToWithdraw) <= depositShares, WithdrawalExeedsMax()); // Similar to `isDelegated` logic if (operator != address(0)) { @@ -668,14 +668,14 @@ contract DelegationManager is operator: operator, staker: staker, strategy: strategies[i], - stakeShares: StakeShares.unwrap(stakeSharesToWithdraw[i]) + stakeShares: DelegationManagerShares.unwrap(stakeSharesToWithdraw[i]) }); } // Remove active shares from EigenPodManager/StrategyManager // EigenPodManager: this call will revert if it would reduce the Staker's virtual beacon chain ETH shares below zero // StrategyManager: this call will revert if `depositSharesToDecrement` exceeds the Staker's current deposit shares in `strategies[i]` - shareManager.removeShares(staker, strategies[i], DepositShares.unwrap(depositSharesToWithdraw)); + shareManager.removeShares(staker, strategies[i], StakeManagerShares.unwrap(depositSharesToWithdraw)); } // Create queue entry and increment withdrawal nonce @@ -734,12 +734,12 @@ contract DelegationManager is address staker, IStrategy strategy, uint64 totalMagnitude, - DepositShares existingDepositShares, + StakeManagerShares existingDepositShares, Shares addedShares ) internal returns (uint256) { uint256 newDepositScalingFactor; - if (DepositShares.unwrap(existingDepositShares) == 0) { + if (StakeManagerShares.unwrap(existingDepositShares) == 0) { // existing shares are 0, meaning no existing delegated shares. In this case, the new depositScalingFactor // is re-initialized to newDepositScalingFactor = WAD / totalMagnitude; @@ -764,7 +764,7 @@ contract DelegationManager is existingDepositShares.toStakeShares(depositScalingFactors[staker][strategy]).toShares(totalMagnitude) ); newDepositScalingFactor = (existingShares + Shares.unwrap(addedShares)) * WAD - / (DepositShares.unwrap(existingDepositShares) + Shares.unwrap(addedShares)) * WAD / totalMagnitude; + / (StakeManagerShares.unwrap(existingDepositShares) + Shares.unwrap(addedShares)) * WAD / totalMagnitude; } // update the staker's depositScalingFactor @@ -859,7 +859,9 @@ contract DelegationManager is /// @notice a legacy function that returns the total delegated shares for an operator and strategy function operatorShares(address operator, IStrategy strategy) public view returns (uint256) { uint64 totalMagnitude = allocationManager.getTotalMagnitude(operator, strategy); - return Shares.unwrap(StakeShares.wrap(operatorStakeShares[operator][strategy]).toShares(totalMagnitude)); + return Shares.unwrap( + DelegationManagerShares.wrap(operatorStakeShares[operator][strategy]).toShares(totalMagnitude) + ); } /** @@ -884,9 +886,8 @@ contract DelegationManager is if (operator != address(0)) { uint64 totalMagnitude = allocationManager.getTotalMagnitude(operator, strategies[i]); shares[i] = Shares.unwrap( - DepositShares.wrap(shares[i]).toStakeShares(depositScalingFactors[staker][strategies[i]]).toShares( - totalMagnitude - ) + StakeManagerShares.wrap(shares[i]).toStakeShares(depositScalingFactors[staker][strategies[i]]) + .toShares(totalMagnitude) ); } } diff --git a/src/contracts/interfaces/IDelegationManager.sol b/src/contracts/interfaces/IDelegationManager.sol index e64de1cdd..04ec99631 100644 --- a/src/contracts/interfaces/IDelegationManager.sol +++ b/src/contracts/interfaces/IDelegationManager.sol @@ -132,7 +132,7 @@ interface IDelegationManager is ISignatureUtils { // Array containing the amount of staker's stakeShares for withdrawal in each Strategy in the `strategies` array // Note that these shares need to be multiplied by the operator's totalMagnitude at completion to include // slashing occurring during the queue withdrawal delay - StakeShares[] stakeShares; + DelegationManagerShares[] stakeShares; } struct QueuedWithdrawalParams { diff --git a/src/contracts/libraries/SlashingLib.sol b/src/contracts/libraries/SlashingLib.sol index 74e083366..1f596c414 100644 --- a/src/contracts/libraries/SlashingLib.sol +++ b/src/contracts/libraries/SlashingLib.sol @@ -43,44 +43,44 @@ uint32 constant DEALLOCATION_DELAY = 17.5 days; type Shares is uint256; -type StakeShares is uint256; +type DelegationManagerShares is uint256; -type DepositShares is uint256; +type StakeManagerShares is uint256; using SlashingLib for Shares global; -using SlashingLib for StakeShares global; -using SlashingLib for DepositShares global; +using SlashingLib for DelegationManagerShares global; +using SlashingLib for StakeManagerShares global; library SlashingLib { using Math for uint256; using SlashingLib for uint256; function toDepositShares( - StakeShares stakeShares, + DelegationManagerShares stakeShares, uint256 depositScalingFactor - ) internal pure returns (DepositShares) { + ) internal pure returns (StakeManagerShares) { if (depositScalingFactor == 0) { depositScalingFactor = WAD; } - return DepositShares.wrap(StakeShares.unwrap(stakeShares).divWad(depositScalingFactor)); + return StakeManagerShares.wrap(DelegationManagerShares.unwrap(stakeShares).divWad(depositScalingFactor)); } function toStakeShares( - DepositShares depositShares, + StakeManagerShares depositShares, uint256 depositScalingFactor - ) internal pure returns (StakeShares) { + ) internal pure returns (DelegationManagerShares) { if (depositScalingFactor == 0) { depositScalingFactor = WAD; } - return StakeShares.wrap(DepositShares.unwrap(depositShares).mulWad(depositScalingFactor)); + return DelegationManagerShares.wrap(StakeManagerShares.unwrap(depositShares).mulWad(depositScalingFactor)); } - function toShares(StakeShares stakeShares, uint256 magnitude) internal pure returns (Shares) { - return Shares.wrap(StakeShares.unwrap(stakeShares).mulWad(magnitude)); + function toShares(DelegationManagerShares stakeShares, uint256 magnitude) internal pure returns (Shares) { + return Shares.wrap(DelegationManagerShares.unwrap(stakeShares).mulWad(magnitude)); } - function toStakeShares(Shares shares, uint256 magnitude) internal pure returns (StakeShares) { - return StakeShares.wrap(Shares.unwrap(shares).divWad(magnitude)); + function toStakeShares(Shares shares, uint256 magnitude) internal pure returns (DelegationManagerShares) { + return DelegationManagerShares.wrap(Shares.unwrap(shares).divWad(magnitude)); } // WAD MATH