Skip to content

Commit

Permalink
Update withdraw tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kphed committed Apr 11, 2024
1 parent a4b8fec commit adb39fc
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions test/MinerETHv2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ contract MinerETHv2Test is Test {
}

function testMineFuzz(uint256 skipSeconds) external {
skipSeconds = bound(skipSeconds, 1, 1_000);
skipSeconds = bound(skipSeconds, 60, 1_000);

// Forward timestamp to accrue interest.
skip(skipSeconds);
Expand Down Expand Up @@ -472,18 +472,13 @@ contract MinerETHv2Test is Test {
uint256 tokenBalanceBefore = miner.balanceOf(address(this));
uint256 minerTotalSupply = miner.totalSupply();
(
uint256 estimatedRedepositShares,
,
,
uint256 estimatedRewards,
,

uint256 estimatedRewardsAccrued
) = _getEstimates();
uint256 removedShares = BRR_ETH_V2.convertToShares(
amount + MOONWELL_SLIPPAGE
);
uint256 rewardsAccruedBefore = rewardsDistributor.rewardsAccrued(
address(this)
);
uint256 dynamicRewardsBalance = ELON.balanceOf(dynamicRewards);

vm.expectEmit(true, true, true, true, address(miner));

Expand All @@ -502,24 +497,33 @@ contract MinerETHv2Test is Test {
// Invariant.
assertEq(tokenBalanceBefore - amount, miner.balanceOf(address(this)));
assertEq(minerTotalSupply - amount, minerTotalSupplyAfter);
assertLt(
minerTotalSupplyAfter -
_calculateAssetsFromRedeem(minerSharesBalance),
MOONWELL_SLIPPAGE
);
assertLt(0, minerTotalSupplyAfter);
assertLt(0, minerSharesBalance);
assertLt(0, principalWithSlippage);
assertLt(0, estimatedRedepositShares);
assertLt(0, estimatedRewards);
assertLt(0, estimatedRewardsAccrued);

// Estimates.
assertLe(
principalWithSlippage,
BRR_ETH_V2.convertToAssets(minerSharesBalance)
_calculateAssetsFromRedeem(minerSharesBalance)
);
assertLe(
estimatedRewards + dynamicRewardsBalance,
ELON.balanceOf(address(dynamicRewards))
);
assertLe(estimatedRedepositShares - removedShares, minerSharesBalance);
assertLe(estimatedRewards + rewardsAccruedBefore, rewardsAccruedAfter);
assertLe(rewardsAccruedAfter, ELON.balanceOf(address(dynamicRewards)));
assertLe(estimatedRewardsAccrued, rewardsAccruedAfter);
}

function testWithdrawFuzz(uint256 amount, uint256 skipSeconds) external {
amount = bound(amount, 1e2, 1_000 ether);
skipSeconds = bound(skipSeconds, 1, 365 days);
amount = bound(amount, 1e9, 1_000 ether);
skipSeconds = bound(skipSeconds, 60, 365 days);

deal(address(this), amount);

Expand All @@ -532,18 +536,13 @@ contract MinerETHv2Test is Test {
uint256 tokenBalanceBefore = miner.balanceOf(address(this));
uint256 minerTotalSupply = miner.totalSupply();
(
uint256 estimatedRedepositShares,
,
,
uint256 estimatedRewards,
,

uint256 estimatedRewardsAccrued
) = _getEstimates();
uint256 removedShares = BRR_ETH_V2.convertToShares(
amount + MOONWELL_SLIPPAGE
);
uint256 rewardsAccruedBefore = rewardsDistributor.rewardsAccrued(
address(this)
);
uint256 dynamicRewardsBalance = ELON.balanceOf(dynamicRewards);

vm.expectEmit(true, true, true, true, address(miner));

Expand All @@ -562,28 +561,37 @@ contract MinerETHv2Test is Test {
// Invariant.
assertEq(tokenBalanceBefore - amount, miner.balanceOf(address(this)));
assertEq(minerTotalSupply - amount, minerTotalSupplyAfter);
assertLt(
minerTotalSupplyAfter -
_calculateAssetsFromRedeem(minerSharesBalance),
MOONWELL_SLIPPAGE
);
assertLt(0, minerTotalSupplyAfter);
assertLt(0, minerSharesBalance);
assertLt(0, principalWithSlippage);
assertLt(0, estimatedRedepositShares);
assertLt(0, estimatedRewards);
assertLt(0, estimatedRewardsAccrued);

// Estimates.
assertLe(
principalWithSlippage,
BRR_ETH_V2.convertToAssets(minerSharesBalance)
_calculateAssetsFromRedeem(minerSharesBalance)
);
assertLe(
estimatedRewards + dynamicRewardsBalance,
ELON.balanceOf(address(dynamicRewards))
);
assertLe(estimatedRedepositShares - removedShares, minerSharesBalance);
assertLe(estimatedRewards + rewardsAccruedBefore, rewardsAccruedAfter);
assertLe(rewardsAccruedAfter, ELON.balanceOf(address(dynamicRewards)));
assertLe(estimatedRewardsAccrued, rewardsAccruedAfter);
}

function testWithdrawPartialFuzz(
uint256 amount,
uint256 skipSeconds,
uint256 withdrawalDivisor
) external {
amount = bound(amount, 1e3, 1_000 ether);
skipSeconds = bound(skipSeconds, 1, 365 days);
amount = bound(amount, 1e12, 1_000 ether);
skipSeconds = bound(skipSeconds, 60, 365 days);
withdrawalDivisor = bound(withdrawalDivisor, 1, type(uint8).max);

deal(address(this), amount);
Expand All @@ -594,22 +602,20 @@ contract MinerETHv2Test is Test {

BRR_ETH_V2.harvest();

uint256 withdrawalAmount = amount / withdrawalDivisor;
uint256 _withdrawalAmount = amount / withdrawalDivisor;
uint256 withdrawalAmount = _withdrawalAmount > 1e9
? _withdrawalAmount
: 1e9;
uint256 tokenBalanceBefore = miner.balanceOf(address(this));
uint256 minerTotalSupply = miner.totalSupply();
(
uint256 estimatedRedepositShares,
,
,
uint256 estimatedRewards,
,

uint256 estimatedRewardsAccrued
) = _getEstimates();
uint256 removedShares = BRR_ETH_V2.convertToShares(
amount + MOONWELL_SLIPPAGE
);
uint256 rewardsAccruedBefore = rewardsDistributor.rewardsAccrued(
address(this)
);
uint256 dynamicRewardsBalance = ELON.balanceOf(dynamicRewards);

vm.expectEmit(true, true, true, true, address(miner));

Expand All @@ -631,19 +637,28 @@ contract MinerETHv2Test is Test {
miner.balanceOf(address(this))
);
assertEq(minerTotalSupply - withdrawalAmount, minerTotalSupplyAfter);
assertLt(
minerTotalSupplyAfter -
_calculateAssetsFromRedeem(minerSharesBalance),
MOONWELL_SLIPPAGE
);
assertLt(0, minerTotalSupplyAfter);
assertLt(0, minerSharesBalance);
assertLt(0, principalWithSlippage);
assertLt(0, estimatedRedepositShares);
assertLt(0, estimatedRewards);
assertLt(0, estimatedRewardsAccrued);

// Estimates.
assertLe(
principalWithSlippage,
BRR_ETH_V2.convertToAssets(minerSharesBalance)
_calculateAssetsFromRedeem(minerSharesBalance)
);
assertLe(
estimatedRewards + dynamicRewardsBalance,
ELON.balanceOf(address(dynamicRewards))
);
assertLe(estimatedRedepositShares - removedShares, minerSharesBalance);
assertLe(estimatedRewards + rewardsAccruedBefore, rewardsAccruedAfter);
assertLe(rewardsAccruedAfter, ELON.balanceOf(address(dynamicRewards)));
assertLe(estimatedRewardsAccrued, rewardsAccruedAfter);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -687,8 +702,8 @@ contract MinerETHv2Test is Test {
uint256 amount,
uint256 skipSeconds
) external {
amount = bound(amount, 1e2, 1_000 ether);
skipSeconds = bound(skipSeconds, 1, 365 days);
amount = bound(amount, 1e12, 1_000 ether);
skipSeconds = bound(skipSeconds, 60, 365 days);

deal(address(this), amount);

Expand Down

0 comments on commit adb39fc

Please sign in to comment.