Skip to content

Commit

Permalink
refactor: add shares
Browse files Browse the repository at this point in the history
  • Loading branch information
8sunyuan committed Sep 13, 2024
1 parent 8dc6f2f commit 857da0f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 50 deletions.
35 changes: 12 additions & 23 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ contract DelegationManager is
) internal {
// Finalize action by converting scaled shares to tokens for each strategy, or
// by re-awarding shares in each strategy.
for (uint256 i = 0; i < withdrawal.strategies.length; ++i) {
for (uint256 i = 0; i < withdrawal.strategies.length;) {
uint256 sharesToWithdraw;
if (isLegacyWithdrawal) {
// This is a legacy M2 withdrawal. There is no slashing applied to the withdrawn shares.
Expand Down Expand Up @@ -666,6 +666,10 @@ contract DelegationManager is
token: tokens[i]
});
}

unchecked {
++i;
}
}
}

Expand All @@ -685,7 +689,7 @@ contract DelegationManager is
// read delegated operator for scaling and adding shares back if needed
address currentOperator = delegatedTo[msg.sender];

for (uint256 i = 0; i < withdrawal.strategies.length; ++i) {
for (uint256 i = 0; i < withdrawal.strategies.length;) {
// store existing shares to calculate new staker scaling factor later
uint256 existingShares;
uint256 shares;
Expand All @@ -704,6 +708,8 @@ contract DelegationManager is
* Other strategy shares can + will be awarded to the withdrawer.
*/
if (withdrawal.strategies[i] == beaconChainETHStrategy) {
// TODO: REFACTOR EPM AND NEGATIVE SHARES

address staker = withdrawal.staker;
/**
* Update shares amount depending upon the returned value.
Expand All @@ -723,29 +729,12 @@ contract DelegationManager is
});
}
} else {
existingShares = strategyManager.addShares(msg.sender, tokens[i], withdrawal.strategies[i], shares);
// Similar to `isDelegated` logic
if (currentOperator != address(0)) {
_increaseOperatorScaledShares({
operator: currentOperator,
// the 'staker' here is the address receiving new shares
staker: msg.sender,
strategy: withdrawal.strategies[i],
shares: shares,
totalMagnitude: totalMagnitudes[i]
});
}
strategyManager.addShares(msg.sender, tokens[i], withdrawal.strategies[i], shares);
}

// update stakers scaling deposit scaling factor
uint256 newStakerScalingFactor = SlashingLib.calculateStakerScalingFactor({
staker: withdrawal.staker,
currStakerScalingFactor: _getStakerScalingFactor(withdrawal.staker, withdrawal.strategies[i]),
totalMagnitude: totalMagnitudes[i],
existingShares: existingShares,
addedShares: shares
});
stakerScalingFactors[withdrawal.staker][withdrawal.strategies[i]] = newStakerScalingFactor;
unchecked {
++i;
}
}
}

Expand Down
33 changes: 13 additions & 20 deletions src/contracts/core/StrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ contract StrategyManager is
IERC20 token,
IStrategy strategy,
uint256 shares
) external onlyDelegationManager returns (uint256 existingShares) {
uint256 existingShares = _addShares(staker, token, strategy, shares);
return existingShares;
) external onlyDelegationManager {
_addShares(staker, token, strategy, shares);
}

/// @notice Used by the DelegationManager to convert withdrawn shares to tokens and send them to a recipient
Expand Down Expand Up @@ -241,12 +240,7 @@ contract StrategyManager is
* delegated shares are tracked, increases the stored share amount in `stakerStrategyShares[staker][strategy]`, and adds `strategy`
* to the `staker`'s list of strategies, if it is not in the list already.
*/
function _addShares(
address staker,
IERC20 token,
IStrategy strategy,
uint256 shares
) internal returns (uint256 existingShares) {
function _addShares(address staker, IERC20 token, IStrategy strategy, uint256 shares) internal {
// sanity checks on inputs
require(staker != address(0), StakerAddressZero());
require(shares != 0, SharesAmountZero());
Expand All @@ -258,11 +252,18 @@ contract StrategyManager is
}

// add the returned shares to their existing shares for this strategy
existingShares = stakerStrategyShares[staker][strategy];
uint256 existingShares = stakerStrategyShares[staker][strategy];
stakerStrategyShares[staker][strategy] += shares;

// Increase shares delegated to operator, if needed
delegation.increaseDelegatedShares({
staker: staker,
strategy: strategy,
existingShares: existingShares,
addedShares: shares
});

emit Deposit(staker, token, strategy, shares);
return existingShares;
}

/**
Expand All @@ -287,15 +288,7 @@ contract StrategyManager is
uint256 shares = strategy.deposit(token, amount);

// add the returned shares to the staker's existing shares for this strategy
uint256 existingShares = _addShares(staker, token, strategy, shares);

// Increase shares delegated to operator, if needed
delegation.increaseDelegatedShares({
staker: staker,
strategy: strategy,
existingShares: existingShares,
addedShares: shares
});
_addShares(staker, token, strategy, shares);

return shares;
}
Expand Down
7 changes: 1 addition & 6 deletions src/contracts/interfaces/IStrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ interface IStrategyManager {
function removeShares(address staker, IStrategy strategy, uint256 shares) external;

/// @notice Used by the DelegationManager to award a Staker some shares that have passed through the withdrawal queue
function addShares(
address staker,
IERC20 token,
IStrategy strategy,
uint256 shares
) external returns (uint256 existingShares);
function addShares(address staker, IERC20 token, IStrategy strategy, uint256 shares) external;

/// @notice Used by the DelegationManager to convert withdrawn descaled shares to tokens and send them to a recipient
function withdrawSharesAsTokens(address recipient, IStrategy strategy, uint256 shares, IERC20 token) external;
Expand Down
2 changes: 1 addition & 1 deletion src/test/mocks/StrategyManagerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ contract StrategyManagerMock is

function removeShares(address staker, IStrategy strategy, uint256 shares) external {}

function addShares(address staker, IERC20 token, IStrategy strategy, uint256 shares) external returns (uint256 existingShares) {}
function addShares(address staker, IERC20 token, IStrategy strategy, uint256 shares) external {}

function withdrawSharesAsTokens(address recipient, IStrategy strategy, uint256 shares, IERC20 token) external {}

Expand Down

0 comments on commit 857da0f

Please sign in to comment.