Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle div by zero edge case in maxRedeem #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading