Skip to content

Commit 3070bec

Browse files
0xClandestineypatil12
authored andcommitted
refactor: getSharesForQueuedWithdrawal (#1130)
**Motivation:** Current fn only returns scaled shares, which leads integrators to making two calls. This is expensive in terms of gas. **Modifications:** - `getSharesFromQueuedWithdrawal` has been renamed to `getQueuedWithdrawalFromRoot` and now also returns `Withdrawal` struct. **Result:** Integrators can fetch both in a single call.
1 parent 25b1376 commit 3070bec

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/contracts/core/DelegationManager.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,10 @@ contract DelegationManager is
972972
}
973973

974974
/// @inheritdoc IDelegationManager
975-
function getSharesFromQueuedWithdrawal(
975+
function getQueuedWithdrawalFromRoot(
976976
bytes32 withdrawalRoot
977-
) external view returns (uint256[] memory shares) {
978-
(, shares) = _getSharesByWithdrawalRoot(withdrawalRoot);
977+
) external view returns (Withdrawal memory withdrawal, uint256[] memory shares) {
978+
(withdrawal, shares) = _getSharesByWithdrawalRoot(withdrawalRoot);
979979
}
980980

981981
/// @inheritdoc IDelegationManager

src/contracts/interfaces/IDelegationManager.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,13 @@ interface IDelegationManager is ISignatureUtilsMixin, IDelegationManagerErrors,
491491
/**
492492
* @notice Returns the withdrawal details and corresponding shares for a specific queued withdrawal.
493493
* @param withdrawalRoot The hash identifying the queued withdrawal.
494+
* @return withdrawal The withdrawal details.
494495
* @return shares Array of shares corresponding to each strategy in the withdrawal.
495496
* @dev The shares are what a user would receive from completing a queued withdrawal, assuming all slashings are applied.
496497
*/
497-
function getSharesFromQueuedWithdrawal(
498+
function getQueuedWithdrawalFromRoot(
498499
bytes32 withdrawalRoot
499-
) external view returns (uint256[] memory shares);
500+
) external view returns (Withdrawal memory withdrawal, uint256[] memory shares);
500501

501502
/// @notice Returns a list of queued withdrawal roots for the `staker`.
502503
/// NOTE that this only returns withdrawals queued AFTER the slashing release.

src/test/unit/DelegationUnit.t.sol

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8670,16 +8670,16 @@ contract DelegationManagerUnitTests_getQueuedWithdrawals is DelegationManagerUni
86708670

86718671
// Get shares from withdrawal - should return 50 shares (100 * 0.5) using original magnitude
86728672
// rather than incorrectly returning 100 shares (100 * 1.0) using new magnitude
8673-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(withdrawalRoot);
8673+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(withdrawalRoot);
86748674
assertEq(shares[0], 50e18, "shares should be 50e18 (100e18 * 0.5) using original magnitude");
86758675
}
86768676
}
86778677

8678-
contract DelegationManagerUnitTests_getSharesFromQueuedWithdrawal is DelegationManagerUnitTests {
8678+
contract DelegationManagerUnitTests_getQueuedWithdrawalFromRoot is DelegationManagerUnitTests {
86798679
using ArrayLib for *;
86808680
using SlashingLib for *;
86818681

8682-
function test_getSharesFromQueuedWithdrawal_Correctness(Randomness r) public rand(r) {
8682+
function test_getQueuedWithdrawalFromRoot_Correctness(Randomness r) public rand(r) {
86838683
// Set up initial deposit
86848684
uint256 depositAmount = r.Uint256(1 ether, 100 ether);
86858685
_depositIntoStrategies(defaultStaker, strategyMock.toArray(), depositAmount.toArrayU256());
@@ -8703,14 +8703,14 @@ contract DelegationManagerUnitTests_getSharesFromQueuedWithdrawal is DelegationM
87038703
delegationManager.queueWithdrawals(queuedWithdrawalParams);
87048704

87058705
// Get shares from queued withdrawal
8706-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(withdrawalRoot);
8706+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(withdrawalRoot);
87078707

87088708
// Verify withdrawal details match
87098709
assertEq(shares.length, 1, "incorrect shares array length");
87108710
assertEq(shares[0], depositAmount, "incorrect shares amount");
87118711
}
87128712

8713-
function test_getSharesFromQueuedWithdrawal_AfterSlashing(Randomness r) public rand(r) {
8713+
function test_getQueuedWithdrawalFromRoot_AfterSlashing(Randomness r) public rand(r) {
87148714
// Set up initial deposit
87158715
uint256 depositAmount = r.Uint256(1 ether, 100 ether);
87168716
_depositIntoStrategies(defaultStaker, strategyMock.toArray(), depositAmount.toArrayU256());
@@ -8739,20 +8739,20 @@ contract DelegationManagerUnitTests_getSharesFromQueuedWithdrawal is DelegationM
87398739
delegationManager.slashOperatorShares(defaultOperator, strategyMock, WAD, 0.5 ether);
87408740

87418741
// Get shares from queued withdrawal
8742-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(withdrawalRoot);
8742+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(withdrawalRoot);
87438743

87448744
// Verify withdrawal details match and shares are slashed
87458745
assertEq(shares.length, 1, "incorrect shares array length");
87468746
assertEq(shares[0], depositAmount / 2, "shares not properly slashed");
87478747
}
87488748

8749-
function test_getSharesFromQueuedWithdrawal_NonexistentWithdrawal() public {
8749+
function test_getQueuedWithdrawalFromRoot_NonexistentWithdrawal() public {
87508750
bytes32 nonexistentRoot = bytes32(uint256(1));
8751-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(nonexistentRoot);
8751+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(nonexistentRoot);
87528752
assertEq(shares.length, 0, "shares array should be empty");
87538753
}
87548754

8755-
function test_getSharesFromQueuedWithdrawal_MultipleStrategies(Randomness r) public rand(r) {
8755+
function test_getQueuedWithdrawalFromRoot_MultipleStrategies(Randomness r) public rand(r) {
87568756
// Set up multiple strategies with deposits
87578757
uint256 numStrategies = r.Uint256(2, 5);
87588758
uint256[] memory depositShares = r.Uint256Array({
@@ -8782,7 +8782,7 @@ contract DelegationManagerUnitTests_getSharesFromQueuedWithdrawal is DelegationM
87828782
delegationManager.queueWithdrawals(queuedWithdrawalParams);
87838783

87848784
// Get shares from queued withdrawal
8785-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(withdrawalRoot);
8785+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(withdrawalRoot);
87868786

87878787
// Verify withdrawal details and shares for each strategy
87888788
assertEq(shares.length, numStrategies, "incorrect shares array length");
@@ -8791,8 +8791,8 @@ contract DelegationManagerUnitTests_getSharesFromQueuedWithdrawal is DelegationM
87918791
}
87928792
}
87938793

8794-
function testFuzz_getSharesFromQueuedWithdrawal_EmptyWithdrawal(bytes32 withdrawalRoot) public {
8795-
uint256[] memory shares = delegationManager.getSharesFromQueuedWithdrawal(withdrawalRoot);
8794+
function testFuzz_getQueuedWithdrawalFromRoot_EmptyWithdrawal(bytes32 withdrawalRoot) public {
8795+
(, uint256[] memory shares) = delegationManager.getQueuedWithdrawalFromRoot(withdrawalRoot);
87968796
assertEq(shares.length, 0, "sanity check");
87978797
}
87988798
}

0 commit comments

Comments
 (0)