Skip to content

Commit

Permalink
handle div by zero edge case in maxRedeem
Browse files Browse the repository at this point in the history
  • Loading branch information
trmid committed Jun 21, 2024
1 parent 41ed556 commit a812f89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PrizeVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ contract PrizeVault is TwabERC20, Claimable, IERC4626, ILiquidationSource, Ownab
}

/// @notice Converts assets to shares with the given vault state and rounding direction.
/// @dev Returns zero if the vault is in a lossy state AND there are no more assets.
/// @param _assets The assets to convert
/// @param _totalAssets The total assets that the vault controls
/// @param _totalDebt The total debt the vault owes
Expand All @@ -873,7 +874,7 @@ contract PrizeVault is TwabERC20, Claimable, IERC4626, ILiquidationSource, Ownab
// If the vault controls less assets than what has been deposited a share will be worth a
// proportional amount of the total assets. This can happen due to fees, slippage, or loss
// of funds in the underlying yield vault.
return _assets.mulDiv(_totalDebt, _totalAssets, _rounding);
return _totalAssets == 0 ? 0 : _assets.mulDiv(_totalDebt, _totalAssets, _rounding);
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/unit/PrizeVault/PrizeVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,25 @@ contract PrizeVaultTest is UnitBaseSetup {
assertEq(vault.maxRedeem(alice), 0);
}

function testMaxRedeem_ReturnsZeroIfLossyAndNoTotalAssets() public {
// deposit some assets
underlyingAsset.mint(alice, 1e18);
vm.startPrank(alice);
underlyingAsset.approve(address(vault), 1e18);
vault.deposit(1e18, alice);
vm.stopPrank();

assertGt(vault.maxRedeem(alice), 0);

// yield vault loses all funds
underlyingAsset.burn(address(yieldVault), underlyingAsset.balanceOf(address(yieldVault)));
assertEq(underlyingAsset.balanceOf(address(yieldVault)), 0);
assertEq(underlyingAsset.balanceOf(address(vault)), 0);
assertEq(vault.totalPreciseAssets(), 0);

assertEq(vault.maxRedeem(alice), 0);
}

/* ============ previewWithdraw ============ */

function testPreviewWithdraw() public {
Expand Down

0 comments on commit a812f89

Please sign in to comment.