Skip to content

Commit

Permalink
fix(maxDeposit): round up
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Aug 15, 2023
1 parent c864cd7 commit b7fcb00
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"plugins": ["prettier"],
"rules": {
"avoid-low-level-calls": "off",
"compiler-version": ["error", "0.8.17"],
"compiler-version": ["error", "0.8.19"],
"func-visibility": "off",
"no-empty-blocks": "off",
"no-inline-assembly": "off"
Expand Down
6 changes: 4 additions & 2 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ contract Vault is ERC4626, ERC20Permit, ILiquidationSource, Ownable {
function maxDeposit(address recipient) public view virtual override returns (uint256) {
if (!_isVaultCollateralized()) return 0;

uint256 _vaultMaxDeposit = type(uint96).max - _convertToAssets(balanceOf(recipient), Math.Rounding.Down);
uint256 _vaultMaxDeposit = type(uint96).max -
_convertToAssets(balanceOf(recipient), Math.Rounding.Up);
uint256 _yieldVaultMaxDeposit = _yieldVault.maxDeposit(address(this));

return _yieldVaultMaxDeposit < _vaultMaxDeposit ? _yieldVaultMaxDeposit : _vaultMaxDeposit;
Expand Down Expand Up @@ -1126,7 +1127,8 @@ contract Vault is ERC4626, ERC20Permit, ILiquidationSource, Ownable {
* @dev Updates the exchange rate.
*/
function _mint(address _receiver, uint256 _shares) internal virtual override {
if (_shares > maxMint(_receiver)) revert MintMoreThanMax(_receiver, _shares, maxMint(_receiver));
if (_shares > maxMint(_receiver))
revert MintMoreThanMax(_receiver, _shares, maxMint(_receiver));

_twabController.mint(_receiver, SafeCast.toUint96(_shares));
_updateExchangeRate();
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/external/FixedMathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ library FixedMathLib {
require(a <= type(uint224).max, "FixedMathLib/a-less-than-224-bits");
return (a * multiplier) / UFixed32x4.unwrap(b);
}
}
}
2 changes: 1 addition & 1 deletion test/contracts/external/LiquidatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,4 @@ library LiquidatorLib {
_minK
);
}
}
}
2 changes: 1 addition & 1 deletion test/contracts/mock/MockLiquidatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ contract MockLiquidatorLib {
uint256 amountIn = LiquidatorLib.getAmountIn(amountOut, virtualReserveIn, virtualReserveOut);
return amountIn;
}
}
}
17 changes: 12 additions & 5 deletions test/unit/Vault/Deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ contract VaultDepositTest is UnitBaseSetup, BrokenToken {
underlyingAsset.mint(alice, _amount);
underlyingAsset.approve(address(vault), type(uint256).max);

vm.expectRevert(abi.encodeWithSelector(DepositMoreThanMax.selector, alice, _amount, type(uint96).max));
vm.expectRevert(
abi.encodeWithSelector(DepositMoreThanMax.selector, alice, _amount, type(uint96).max)
);
vault.deposit(_amount, alice);

vm.stopPrank();
Expand All @@ -190,7 +192,6 @@ contract VaultDepositTest is UnitBaseSetup, BrokenToken {
function testDepositMoreThanYieldVaultMax() external {
vm.startPrank(alice);


uint256 _amount = uint256(type(uint88).max) + 1;

underlyingAsset.mint(alice, _amount);
Expand All @@ -202,7 +203,9 @@ contract VaultDepositTest is UnitBaseSetup, BrokenToken {
abi.encode(type(uint88).max)
);

vm.expectRevert(abi.encodeWithSelector(DepositMoreThanMax.selector, alice, _amount, type(uint88).max));
vm.expectRevert(
abi.encodeWithSelector(DepositMoreThanMax.selector, alice, _amount, type(uint88).max)
);
vault.deposit(_amount, alice);

vm.stopPrank();
Expand Down Expand Up @@ -415,7 +418,9 @@ contract VaultDepositTest is UnitBaseSetup, BrokenToken {
underlyingAsset.mint(alice, _amount);
underlyingAsset.approve(address(vault), type(uint256).max);

vm.expectRevert(abi.encodeWithSelector(MintMoreThanMax.selector, alice, _amount, type(uint96).max));
vm.expectRevert(
abi.encodeWithSelector(MintMoreThanMax.selector, alice, _amount, type(uint96).max)
);

vault.mint(_amount, alice);

Expand All @@ -436,7 +441,9 @@ contract VaultDepositTest is UnitBaseSetup, BrokenToken {
abi.encode(type(uint88).max)
);

vm.expectRevert(abi.encodeWithSelector(MintMoreThanMax.selector, alice, _amount, type(uint88).max));
vm.expectRevert(
abi.encodeWithSelector(MintMoreThanMax.selector, alice, _amount, type(uint88).max)
);

vault.mint(_amount, alice);

Expand Down
24 changes: 4 additions & 20 deletions test/unit/Vault/Liquidate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ contract VaultLiquidateTest is UnitBaseSetup {

vm.startPrank(address(liquidationPair));

vm.expectRevert(
abi.encodeWithSelector(
VaultUnderCollateralized.selector
)
);
vm.expectRevert(abi.encodeWithSelector(VaultUnderCollateralized.selector));

vault.liquidate(address(this), address(0), 0, address(vault), 0);

Expand Down Expand Up @@ -432,11 +428,7 @@ contract VaultLiquidateTest is UnitBaseSetup {

uint256 _amountIn = liquidationPair.computeExactAmountIn(_amountOut);

IERC20(address(prizeToken)).transferFrom(
alice,
address(prizePool),
_amountIn
);
IERC20(address(prizeToken)).transferFrom(alice, address(prizePool), _amountIn);

vm.startPrank(address(liquidationPair));

Expand Down Expand Up @@ -475,13 +467,7 @@ contract VaultLiquidateTest is UnitBaseSetup {

uint256 _liquidatableYield = vault.liquidatableBalanceOf(address(vault));

_liquidate(
liquidationRouter,
liquidationPair,
prizeToken,
_liquidatableYield,
alice
);
_liquidate(liquidationRouter, liquidationPair, prizeToken, _liquidatableYield, alice);

vm.stopPrank();

Expand All @@ -492,9 +478,7 @@ contract VaultLiquidateTest is UnitBaseSetup {

vault.deposit(vault.maxDeposit(bob), bob);

vm.expectRevert(
abi.encodeWithSelector(MintMoreThanMax.selector, bob, 1e18, 0)
);
vm.expectRevert(abi.encodeWithSelector(MintMoreThanMax.selector, bob, 1e18, 0));

vault.mintYieldFee(1e18);

Expand Down
8 changes: 6 additions & 2 deletions test/unit/Vault/Withdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ contract VaultWithdrawTest is UnitBaseSetup {
underlyingAsset.mint(alice, _amount);
_deposit(underlyingAsset, vault, _amount, alice);

vm.expectRevert(abi.encodeWithSelector(WithdrawMoreThanMax.selector, alice, _amount + 1, _amount));
vm.expectRevert(
abi.encodeWithSelector(WithdrawMoreThanMax.selector, alice, _amount + 1, _amount)
);
vault.withdraw(_amount + 1, alice, alice);

vm.stopPrank();
Expand Down Expand Up @@ -283,7 +285,9 @@ contract VaultWithdrawTest is UnitBaseSetup {
underlyingAsset.mint(alice, _amount);
uint256 _shares = _deposit(underlyingAsset, vault, _amount, alice);

vm.expectRevert(abi.encodeWithSelector(RedeemMoreThanMax.selector, alice, _shares + 1, _shares));
vm.expectRevert(
abi.encodeWithSelector(RedeemMoreThanMax.selector, alice, _shares + 1, _shares)
);
vault.redeem(_shares + 1, alice, alice);

vm.stopPrank();
Expand Down

0 comments on commit b7fcb00

Please sign in to comment.