Skip to content

Commit

Permalink
Merge pull request #77 from euler-xyz/gulp-fix
Browse files Browse the repository at this point in the history
cap gulp without reverts
  • Loading branch information
dglowinski authored Mar 27, 2024
2 parents 7d2408d + c6bd456 commit 2065d38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/Synths/EulerSavingsRate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {
uint256 totalAssetsDeposited;

error Reentrancy();
error E_GulpTooMuch();

/// @notice Modifier to require an account status check on the EVC.
/// @dev Calls `requireAccountStatusCheck` function from EVC for the specified account after the function body.
Expand Down Expand Up @@ -133,10 +132,12 @@ contract EulerSavingsRate is EVCUtil, ERC4626 {
uint256 assetBalance = IERC20(asset()).balanceOf(address(this));
uint256 toGulp = assetBalance - totalAssetsDeposited - esrSlotCache.interestLeft;

if (toGulp > type(uint168).max - esrSlotCache.interestLeft) revert E_GulpTooMuch();
uint256 maxGulp = type(uint168).max - esrSlotCache.interestLeft;
if (toGulp > maxGulp) toGulp = maxGulp; // cap interest, allowing the vault to function

esrSlotCache.interestSmearEnd = uint40(block.timestamp + INTEREST_SMEAR);
esrSlotCache.interestLeft += uint168(toGulp);
esrSlotCache.interestLeft += uint168(toGulp); // toGulp <= maxGulp <= max uint168

// write esrSlotCache back to storage in a single SSTORE
esrSlot = esrSlotCache;
}
Expand Down
8 changes: 6 additions & 2 deletions test/unit/esr/ESR.Fuzz.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ contract ESRFuzzTest is ESRTest {
// this tests shows that when you have a very small deposit and a very large interestAmount minted to the contract
function testFuzz_gulp_under_uint168(uint256 interestAmount, uint256 depositAmount, uint256 timePassed) public {
depositAmount = bound(depositAmount, 0, type(uint112).max);
interestAmount = bound(interestAmount, 0, type(uint112).max - depositAmount); // this makes sure that the mint won't cause overflow
interestAmount = bound(interestAmount, 0, type(uint256).max - depositAmount); // this makes sure that the mint won't cause overflow
timePassed = bound(timePassed, block.timestamp, type(uint40).max);
asset.mint(address(esr), interestAmount);

doDeposit(user, depositAmount);
esr.gulp();

EulerSavingsRate.ESRSlot memory esrSlot = esr.updateInterestAndReturnESRSlotCache();
assertEq(esrSlot.interestLeft, interestAmount);
if (interestAmount <= type(uint168).max - depositAmount) {
assertEq(esrSlot.interestLeft, interestAmount);
} else {
assertEq(esrSlot.interestLeft, type(uint168).max);
}
}

// fuzz test that any deposits added are added to the totalAssetsDeposited
Expand Down

0 comments on commit 2065d38

Please sign in to comment.