Skip to content

Commit

Permalink
fix: add deposit scaling factor
Browse files Browse the repository at this point in the history
  • Loading branch information
gpsanant committed Dec 18, 2024
1 parent 8225301 commit 36e8a85
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ contract DelegationManager is
}

/// @inheritdoc IDelegationManager
function convertToDepositShares(address staker, IStrategy[] memory strategies, uint256[] memory shares) external view returns (uint256[] memory) {
function convertToDepositShares(address staker, IStrategy[] memory strategies, uint256[] memory withdrawableShares) external view returns (uint256[] memory) {
// Get the slashing factors for the staker/operator/strategies
address operator = delegatedTo[staker];
uint256[] memory slashingFactors = _getSlashingFactors(staker, operator, strategies);
Expand All @@ -980,7 +980,7 @@ contract DelegationManager is
uint256[] memory depositShares = new uint256[](strategies.length);
for (uint256 i = 0; i < strategies.length; ++i) {
DepositScalingFactor memory dsf = _depositScalingFactor[staker][strategies[i]];
depositShares[i] = dsf.calcDepositShares(shares[i], slashingFactors[i]);
depositShares[i] = dsf.calcDepositShares(withdrawableShares[i], slashingFactors[i]);
}
return depositShares;
}
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/interfaces/IDelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ interface IDelegationManager is ISignatureUtils, IDelegationManagerErrors, IDele
* @notice Converts shares for a set of strategies to deposit shares, likely in order to input into `queueWithdrawals`
* @param staker the staker to convert shares for
* @param strategies the strategies to convert shares for
* @param shares the shares to convert
* @param withdrawableShares the shares to convert
* @return the deposit shares
* @dev will be a few wei off due to rounding errors
*/
function convertToDepositShares(address staker, IStrategy[] memory strategies, uint256[] memory shares) external view returns (uint256[] memory);
function convertToDepositShares(address staker, IStrategy[] memory strategies, uint256[] memory withdrawableShares) external view returns (uint256[] memory);

/// @notice Returns the keccak256 hash of `withdrawal`.
function calculateWithdrawalRoot(
Expand Down
41 changes: 38 additions & 3 deletions src/test/unit/DelegationUnit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8302,7 +8302,15 @@ contract DelegationManagerUnitTests_ConvertToDepositShares is DelegationManagerU
_delegateToOperatorWhoAcceptsAllStakers(defaultStaker, defaultOperator);
_setOperatorMagnitude(defaultOperator, strategyMock, WAD/3);

_checkDepositSharesConvertCorrectly(strategies, shares);
_checkDepositSharesConvertCorrectly(strategies, shares);

// queue and complete a withdrawal for half the deposit shares
(uint256[] memory withdrawableShares,) = delegationManager.getWithdrawableShares(defaultStaker, strategies);
_queueAndCompleteWithdrawalForSingleStrategy(strategies[0], shares[0] / 2);

// queued a withdrawal for half the deposit shares, and added back as withdrawable shares
shares[0] = shares[0] / 2 + withdrawableShares[0] / 2;
_checkDepositSharesConvertCorrectly(strategies, shares);
}

function test_convertToDepositShares_beaconChainETH() public {
Expand All @@ -8319,15 +8327,22 @@ contract DelegationManagerUnitTests_ConvertToDepositShares is DelegationManagerU
// delegate to an operator and slash
_registerOperatorWithBaseDetails(defaultOperator);
_delegateToOperatorWhoAcceptsAllStakers(defaultStaker, defaultOperator);
_setOperatorMagnitude(defaultOperator, strategyMock, WAD/3);
_setOperatorMagnitude(defaultOperator, beaconChainETHStrategy, WAD/3);

_checkDepositSharesConvertCorrectly(strategies, shares);

// slash on beacon chain by 1/3
_decreaseBeaconChainShares(defaultStaker, int256(shares[0]), shares[0]/3);

_checkDepositSharesConvertCorrectly(strategies, shares);


// queue and complete a withdrawal for half the deposit shares
(uint256[] memory withdrawableShares,) = delegationManager.getWithdrawableShares(defaultStaker, strategies);
_queueAndCompleteWithdrawalForSingleStrategy(strategies[0], shares[0] / 2);

// queued a withdrawal for half the deposit shares, and added back as withdrawable shares
shares[0] = shares[0] / 2 + withdrawableShares[0] / 2;
_checkDepositSharesConvertCorrectly(strategies, shares);
}

function _checkDepositSharesConvertCorrectly(IStrategy[] memory strategies, uint256[] memory expectedDepositShares) public {
Expand Down Expand Up @@ -8367,5 +8382,25 @@ contract DelegationManagerUnitTests_ConvertToDepositShares is DelegationManagerU
);
}
}

function _queueAndCompleteWithdrawalForSingleStrategy(IStrategy strategy, uint256 shares) public {
IStrategy[] memory strategies = new IStrategy[](1);
strategies[0] = strategy;
uint256[] memory depositShares = uint256(shares).toArrayU256();

(QueuedWithdrawalParams[] memory queuedWithdrawalParams, Withdrawal memory withdrawal, bytes32 withdrawalRoot) = _setUpQueueWithdrawalsSingleStrat({
staker: defaultStaker,
withdrawer: defaultStaker,
strategy: strategy,
depositSharesToWithdraw: shares
});

cheats.prank(defaultStaker);
delegationManager.queueWithdrawals(queuedWithdrawalParams);

cheats.roll(block.number + delegationManager.minWithdrawalDelayBlocks());
cheats.prank(defaultStaker);
delegationManager.completeQueuedWithdrawal(withdrawal, tokenMock.toArray(), false);
}
}

0 comments on commit 36e8a85

Please sign in to comment.