From 50c2668fc3430ad786902e4b24947c06736dc576 Mon Sep 17 00:00:00 2001 From: Kane Wallmann Date: Tue, 20 Jul 2021 15:29:12 +1000 Subject: [PATCH] Add some additional sanity checks to excess collateral deposit --- .../contract/minipool/RocketMinipoolDelegate.sol | 9 ++++++--- contracts/contract/token/RocketTokenRETH.sol | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/contracts/contract/minipool/RocketMinipoolDelegate.sol b/contracts/contract/minipool/RocketMinipoolDelegate.sol index 1182a19ca..a16155189 100644 --- a/contracts/contract/minipool/RocketMinipoolDelegate.sol +++ b/contracts/contract/minipool/RocketMinipoolDelegate.sol @@ -405,9 +405,12 @@ contract RocketMinipoolDelegate is RocketMinipoolStorageLayout, RocketMinipoolIn // Only the owner can destroy a minipool require(nodeWithdrawalAddress == msg.sender || nodeAddress == msg.sender, "Only node operator can destroy minipool"); // Send any remaining balance to rETH contract - payable(rocketTokenRETH).transfer(address(this).balance.sub(refundAmount)); - // Send any overcollateralised ETH to the deposit pool - RocketTokenRETHInterface(rocketTokenRETH).depositExcessCollateral(); + uint256 userAmount = address(this).balance.sub(refundAmount); + if (userAmount > 0) { + payable(rocketTokenRETH).transfer(userAmount); + // Send any overcollateralised ETH to the deposit pool + RocketTokenRETHInterface(rocketTokenRETH).depositExcessCollateral(); + } // Self destruct the refund amount to node withdrawal address selfdestruct(payable(nodeWithdrawalAddress)); } diff --git a/contracts/contract/token/RocketTokenRETH.sol b/contracts/contract/token/RocketTokenRETH.sol index e25b99d1d..fef74b0f5 100644 --- a/contracts/contract/token/RocketTokenRETH.sol +++ b/contracts/contract/token/RocketTokenRETH.sol @@ -142,11 +142,14 @@ contract RocketTokenRETH is RocketBase, ERC20, RocketTokenRETHInterface { uint256 targetCollateralRate = rocketDAOProtocolSettingsNetwork.getTargetRethCollateralRate(); // Check if we are in excess if (collateralRate > targetCollateralRate) { - // Calculate ETH excess + // Calculate our target collateral in ETH uint256 targetCollateral = address(this).balance.mul(targetCollateralRate).div(collateralRate); - uint256 excessCollateral = address(this).balance.sub(targetCollateral); - // Send excess to deposit pool - rocketDepositPool.recycleExcessCollateral{value: excessCollateral}(); + // If we have excess + if (address(this).balance > targetCollateral) { + // Send that excess to deposit pool + uint256 excessCollateral = address(this).balance.sub(targetCollateral); + rocketDepositPool.recycleExcessCollateral{value: excessCollateral}(); + } } }