@@ -437,6 +437,8 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
437437 /// @dev Internal implementation of claim process
438438 function _claim () internal {
439439 uint256 amountToSend = refundValue;
440+ // Nothing to do if no refund value to claim
441+ if (amountToSend == 0 ) return ;
440442 // If node operator has a debt, pay that off first
441443 if (debt > 0 ) {
442444 if (debt > amountToSend) {
@@ -459,21 +461,6 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
459461 emit RewardsClaimed (amountToSend, block .timestamp );
460462 }
461463
462- /// @dev Repays a debt from refund balance
463- function _claimDebt () internal {
464- // Nothing to do with no debt
465- if (debt == 0 ) return ;
466- // Calculate how much to repay
467- uint256 amountToClaim = refundValue;
468- if (amountToClaim > debt) {
469- amountToClaim = debt;
470- }
471- // Send to rETH
472- _repayDebt (amountToClaim);
473- // Reduce refund balance
474- refundValue -= amountToClaim;
475- }
476-
477464 /// @notice Returns the calculated split of pending rewards
478465 function calculatePendingRewards () override external view returns (uint256 nodeRewards , uint256 voterRewards , uint256 rethRewards ) {
479466 return calculateRewards (getPendingRewards ());
@@ -590,7 +577,6 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
590577 uint256 withdrawableEpoch = uint256 (validators[_validatorId].withdrawableEpoch);
591578 uint256 distributableTime = (withdrawableEpoch * secondsPerSlot * slotsPerEpoch + genesisTime) + distributeWindowStart;
592579 require (block .timestamp > distributableTime, "Not enough time has passed " );
593- _claimDebt ();
594580 }
595581 }
596582
@@ -629,8 +615,30 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
629615 }
630616 }
631617 uint256 toUser = withdrawalBalance - toNode;
618+ // Pay off any existing debt and any new debt introduced by this exit
619+ uint256 existingDebt = debt;
620+ uint256 newDebt = existingDebt;
632621 if (toUser < userShare) {
633- _increaseDebt (userShare - toUser);
622+ newDebt += userShare - toUser;
623+ }
624+ if (toNode > 0 && newDebt > 0 ) {
625+ if (toNode > newDebt) {
626+ toNode -= newDebt;
627+ toUser += newDebt;
628+ newDebt = 0 ;
629+ } else {
630+ newDebt -= toNode;
631+ toUser += toNode;
632+ toNode = 0 ;
633+ }
634+ }
635+ // Handle any changes to debt balance
636+ if (newDebt != existingDebt) {
637+ if (newDebt > existingDebt) {
638+ _increaseDebt (newDebt - existingDebt);
639+ } else {
640+ _reduceDebt (existingDebt - newDebt);
641+ }
634642 }
635643 // Send funds
636644 sendToRETH (toUser);
0 commit comments