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

feat: max deposit #99

Merged
merged 7 commits into from
Sep 9, 2024
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
7 changes: 7 additions & 0 deletions src/TokenizedStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ contract TokenizedStrategy {
) external nonReentrant returns (uint256 shares) {
// Get the storage slot for all following calls.
StrategyData storage S = _strategyStorage();

// Deposit full balance if using max uint.
if (assets == type(uint256).max) {
assets = S.asset.balanceOf(msg.sender);
}

// Checking max deposit will also check if shutdown.
require(
assets <= _maxDeposit(S, receiver),
Expand Down Expand Up @@ -524,6 +530,7 @@ contract TokenizedStrategy {
) external nonReentrant returns (uint256 assets) {
// Get the storage slot for all following calls.
StrategyData storage S = _strategyStorage();

// Checking max mint will also check if shutdown.
require(shares <= _maxMint(S, receiver), "ERC4626: mint more than max");
// Check for rounding error.
Expand Down
31 changes: 31 additions & 0 deletions src/test/Accounting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,35 @@ contract AccountingTest is Setup {
assertEq(strategy.pricePerShare(), wad);
checkStrategyTotals(strategy, 0, 0, 0, 0);
}

function test_maxUintDeposit_depositsBalance(
address _address,
uint256 _amount
) public {
_amount = bound(_amount, minFuzzAmount, maxFuzzAmount);
vm.assume(
_address != address(0) &&
_address != address(strategy) &&
_address != address(yieldSource)
);

asset.mint(_address, _amount);

vm.prank(_address);
asset.approve(address(strategy), _amount);

assertEq(asset.balanceOf(_address), _amount);

vm.prank(_address);
strategy.deposit(type(uint256).max, _address);

// Should just deposit the available amount.
checkStrategyTotals(strategy, _amount, _amount, 0, _amount);

assertEq(asset.balanceOf(_address), 0);
assertEq(strategy.balanceOf(_address), _amount);
assertEq(asset.balanceOf(address(strategy)), 0);

assertEq(asset.balanceOf(address(yieldSource)), _amount);
}
}
18 changes: 18 additions & 0 deletions src/test/ERC4626Std.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ contract ERC4626StdTest is ERC4626Test, Setup {
_vaultMayBeEmpty = true;
_unlimitedAmount = true;
}

//Avoid special case for deposits of uint256 max
function test_previewDeposit(
Init memory init,
uint256 assets
) public override {
if (assets == type(uint256).max) assets -= 1;
super.test_previewDeposit(init, assets);
}

function test_deposit(
Init memory init,
uint256 assets,
uint256 allowance
) public override {
if (assets == type(uint256).max) assets -= 1;
super.test_deposit(init, assets, allowance);
}
}
Loading