Skip to content

Commit

Permalink
refactor: more explicit share names
Browse files Browse the repository at this point in the history
  • Loading branch information
0xClandestine committed Sep 20, 2024
1 parent 22ae6e8 commit cf7e4bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
33 changes: 17 additions & 16 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down Expand Up @@ -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]
});
Expand Down Expand Up @@ -589,7 +589,7 @@ contract DelegationManager is
address operator,
address staker,
IStrategy strategy,
DepositShares existingDepositShares,
StakeManagerShares existingDepositShares,
Shares addedShares,
uint64 totalMagnitude
) internal {
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)
);
}

/**
Expand All @@ -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)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/interfaces/IDelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions src/contracts/libraries/SlashingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf7e4bb

Please sign in to comment.