Skip to content

Commit

Permalink
feat: max deposit (#99)
Browse files Browse the repository at this point in the history
* feat: max uint deposit

* fix: invariant test

* fix: rebase

* fix: dont max mint

* fix: uint 256

Co-authored-by: FP <83050944+fp-crypto@users.noreply.github.com>

* fix: uint 256

Co-authored-by: FP <83050944+fp-crypto@users.noreply.github.com>

* test: check balances

---------

Co-authored-by: FP <83050944+fp-crypto@users.noreply.github.com>
  • Loading branch information
Schlagonia and fp-crypto authored Sep 9, 2024
1 parent 59ece7f commit bbabc72
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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);
}
}

0 comments on commit bbabc72

Please sign in to comment.