Skip to content

Commit f4cc486

Browse files
committed
Refactor debt repayment
1 parent 6f47a13 commit f4cc486

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

contracts/contract/megapool/RocketMegapoolDelegate.sol

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

test/megapool/scenario-exit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import assert from 'assert';
1212
const hre = require('hardhat');
1313
const ethers = hre.ethers;
1414

15-
// Nofiy megapool of exiting validator
15+
// Notify megapool of exiting validator
1616
export async function notifyExitValidator(megapool, validatorId, withdrawalEpoch) {
1717

1818
const rocketMegapoolManager = await RocketMegapoolManager.deployed();
@@ -153,7 +153,7 @@ export async function notifyFinalBalanceValidator(megapool, validatorId, finalBa
153153
if (nodeCalling) {
154154
assertBN.equal(balanceDeltas.rethBalance + balanceDeltas.nodeBalance + balanceDeltas.nodeRefund, finalBalance);
155155
} else {
156-
assertBN.equal(balanceDeltas.rethBalance + balanceDeltas.nodeBalance + balanceDeltas.nodeRefund, finalBalance);
156+
assertBN.equal(balanceDeltas.rethBalance + balanceDeltas.nodeRefund, finalBalance);
157157
}
158158

159159
if (!info.dissolved) {

0 commit comments

Comments
 (0)