Skip to content

Commit

Permalink
fix l10
Browse files Browse the repository at this point in the history
  • Loading branch information
OxMarco committed Dec 18, 2023
1 parent 859a29c commit c61ff68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ contract Vault is IVault, ERC4626, ERC20Permit {
return ERC4626.deposit(_assets, receiver);
}

function maxDeposit(address) public view override(IERC4626, ERC4626) returns (uint256) {
if (isLocked) return 0;
return type(uint256).max;
}

function maxMint(address) public view override(IERC4626, ERC4626) returns (uint256) {
if (isLocked) return 0;
return type(uint256).max;
}

function depositWithPermit(
uint256 assets,
address receiver,
Expand All @@ -170,7 +180,7 @@ contract Vault is IVault, ERC4626, ERC20Permit {
) public override(ERC4626, IERC4626) returns (uint256) {
// assets cannot be more than the current free liquidity
uint256 freeLiq = freeLiquidity();
if (assets >= freeLiq) revert InsufficientLiquidity();
if (assets > freeLiq) revert InsufficientLiquidity();

// super.withdraw but we leverage the fact of having already computed freeLiq
uint256 supply = totalSupply();
Expand All @@ -194,7 +204,7 @@ contract Vault is IVault, ERC4626, ERC20Permit {
uint256 supply = totalSupply();

uint256 assets = shares.mulDiv(totalAssetsCache, supply);
if (assets >= freeLiq) revert InsufficientLiquidity();
if (assets > freeLiq) revert InsufficientLiquidity();
// redeem, now all data have been computed
_withdraw(msg.sender, receiver, owner, assets, shares);

Expand Down
16 changes: 10 additions & 6 deletions test/Vault.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ contract VaultTest is Test {
assertTrue(token.balanceOf(tokenSink) == type(uint256).max);
}

function testAccess(uint256 shares, uint256 assets, uint256 debt) public {
function testAccess(uint256 assets, uint256 debt) public {
vm.startPrank(notOwner);
vm.expectRevert(bytes4(keccak256(abi.encodePacked("RestrictedToOwner()"))));
vault.setFeeUnlockTime(1000);
Expand Down Expand Up @@ -422,7 +422,7 @@ contract VaultTest is Test {
uint256 shares = 0;

vm.startPrank(receiver);
if (withdrawn >= vault.freeLiquidity()) {
if (withdrawn > vault.freeLiquidity()) {
vm.expectRevert(bytes4(keccak256(abi.encodePacked("InsufficientLiquidity()"))));
vault.withdraw(withdrawn, receiver, receiver);
withdrawn = 0;
Expand Down Expand Up @@ -597,25 +597,29 @@ contract VaultTest is Test {
}

function testCannotWithdrawMoreThanFreeLiquidity(uint256 amount) public {
vm.assume(amount < type(uint256).max);

vm.startPrank(tokenSink);
token.approve(address(vault), amount);
token.approve(address(vault), type(uint256).max);
vault.deposit(amount, receiver);
vm.stopPrank();

// withdraw without leaving 1 token unit
uint256 vaultBalance = token.balanceOf(address(vault));
vm.expectRevert(IVault.InsufficientLiquidity.selector);
vault.withdraw(vaultBalance, tokenSink, receiver);
vault.withdraw(vaultBalance + 1, tokenSink, receiver);
}

function testCannotBorrowMoreThanFreeLiquidity(uint256 amount) public {
vm.assume(amount < type(uint256).max);

vm.startPrank(tokenSink);
token.approve(address(vault), amount);
token.approve(address(vault), type(uint256).max);
vault.deposit(amount, receiver);
vm.stopPrank();

uint256 vaultBalance = token.balanceOf(address(vault));
vm.expectRevert(IVault.InsufficientFreeLiquidity.selector);
vault.borrow(vaultBalance, vaultBalance, address(this));
vault.borrow(vaultBalance + 1, vaultBalance, address(this));
}
}

0 comments on commit c61ff68

Please sign in to comment.