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

test: partial withdraw #7

Merged
merged 2 commits into from
Nov 3, 2023
Merged
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
93 changes: 91 additions & 2 deletions src/test/Operation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,95 @@ contract OperationTest is Setup {
);
}

function test_partialWithdraw_highLTV(uint256 _amount) public {
vm.assume(_amount > minFuzzAmount && _amount < maxFuzzAmount);

uint256 targetLTV = (strategy.getLiquidateCollateralFactor() *
strategy.targetLTVMultiplier()) / MAX_BPS;
// Deposit into strategy
mintAndDepositIntoStrategy(strategy, user, _amount);

checkStrategyTotals(strategy, _amount, _amount, 0);
assertRelApproxEq(strategy.getCurrentLTV(), targetLTV, 1000);
assertEq(strategy.balanceOfCollateral(), _amount, "collateral");
assertApproxEq(
strategy.balanceOfDebt(),
strategy.balanceOfDepositor(),
3
);
// Earn Interest
skip(1 days);

// Increase LTV
uint256 toBorrow = (strategy.balanceOfCollateral() *
((strategy.getLiquidateCollateralFactor() *
(strategy.targetLTVMultiplier() + 500)) / MAX_BPS)) / 1e18;

toBorrow = _fromUsd(_toUsd(toBorrow, address(asset)), baseToken);

vm.startPrank(address(strategy));
Comet(comet).withdraw(
address(baseToken),
toBorrow - strategy.balanceOfDebt()
);
vm.stopPrank();

assertGt(strategy.getCurrentLTV(), targetLTV);

uint256 balanceBefore = asset.balanceOf(user);

// Withdraw all funds
vm.prank(user);
strategy.redeem(_amount / 2, user, user, 0);

assertGe(
asset.balanceOf(user),
balanceBefore + (_amount / 2),
"!final balance"
);
}

function test_partialWithdraw_lowerLTV(uint256 _amount) public {
vm.assume(_amount > minFuzzAmount && _amount < maxFuzzAmount);

uint256 targetLTV = (strategy.getLiquidateCollateralFactor() *
strategy.targetLTVMultiplier()) / MAX_BPS;
// Deposit into strategy
mintAndDepositIntoStrategy(strategy, user, _amount);

checkStrategyTotals(strategy, _amount, _amount, 0);
assertRelApproxEq(strategy.getCurrentLTV(), targetLTV, 1000);
assertEq(strategy.balanceOfCollateral(), _amount, "collateral");
assertApproxEq(
strategy.balanceOfDebt(),
strategy.balanceOfDepositor(),
3
);
// Earn Interest
skip(1 days);

// lower LTV
uint256 borrowed = strategy.balanceOfDebt();
airdrop(ERC20(baseToken), address(strategy), borrowed / 4);

vm.prank(management);
strategy.manualRepayDebt();

assertLt(strategy.getCurrentLTV(), targetLTV);

uint256 balanceBefore = asset.balanceOf(user);

// Withdraw all funds
vm.prank(user);
strategy.redeem(_amount / 2, user, user, 0);

assertGe(
asset.balanceOf(user),
balanceBefore + (_amount / 2),
"!final balance"
);
}

function test_healthcheck(uint256 _amount) public {
vm.assume(_amount > minFuzzAmount && _amount < maxFuzzAmount);

Expand Down Expand Up @@ -247,7 +336,7 @@ contract OperationTest is Setup {
assertApproxEq(
strategy.balanceOfDebt(),
strategy.balanceOfDepositor(),
2
3
);
// Earn unrealized profit.
skip(1 days);
Expand Down Expand Up @@ -280,7 +369,7 @@ contract OperationTest is Setup {
assertApproxEq(
strategy.balanceOfDebt(),
strategy.balanceOfDepositor(),
2
3
);
// Earn unrealized profit.
skip(1 days);
Expand Down
Loading