Skip to content

Commit

Permalink
test: add withdraw reserve test
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargavaparoksham committed Jan 21, 2025
1 parent 444d33e commit 69a828f
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/AssetPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,43 @@ contract AssetPoolTest is Test {
assertEq(pool.cycleRedemptionRequests(pool.cycleIndex(), user1), burnAmount);
}

function testCancelBurn() public {
// Setup: Complete a cycle first to have assets to burn
setupCompleteDepositCycle();

pool.mintAsset(user1);

// Verify user has assets before burning
uint256 userBalance = assetToken.balanceOf(user1);
assertGt(userBalance, 0, "User should have assets");

uint256 burnAmount = userBalance / 2; // Burn half of the balance

vm.startPrank(user1);
assetToken.approve(address(pool), burnAmount);
pool.burnAsset(burnAmount);
assertEq(pool.cycleRedemptionRequests(pool.cycleIndex(), user1), burnAmount);

// Cancel burn
pool.cancelBurn();
vm.stopPrank();

assertEq(pool.cycleRedemptionRequests(pool.cycleIndex(), user1), 0);
}

function testWithdrawReserve() public {
// complete the deposit & burn cycle
setupCompleteBurnCycle();

// withdraw the reserve tokens
pool.withdrawReserve(user1);
assertEq(pool.cycleRedemptionRequests(pool.cycleIndex(), user1), 0);

// assert that the user has received the reserve tokens
uint256 userBalance = reserveToken.balanceOf(user1);
assertGt(userBalance, 0, "User should have received reserve tokens");
}

// --------------------------------------------------------------------------------
// GOVERNANCE TESTS
// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -307,6 +344,51 @@ contract AssetPoolTest is Test {
vm.warp(block.timestamp + REBALANCE_PERIOD + 1);

}

function setupCompleteBurnCycle() internal {

setupCompleteDepositCycle();

pool.mintAsset(user1);

// Verify user has assets before burning
uint256 userBalance = assetToken.balanceOf(user1);
assertGt(userBalance, 0, "User should have assets");

uint256 burnAmount = userBalance / 2; // Burn half of the balance

vm.startPrank(user1);
assetToken.approve(address(pool), burnAmount);
pool.burnAsset(burnAmount);

// Move to after rebalance start
vm.warp(block.timestamp + CYCLE_PERIOD + 1);

// Complete rebalancing
pool.initiateRebalance();

// Get rebalance info
(, , , int256 rebalanceAmount) = pool.getLPInfo();
uint256 expectedAmount = uint256(rebalanceAmount > 0 ? rebalanceAmount : -rebalanceAmount) / 2;
bool isDeposit = rebalanceAmount > 0;
uint256 rebalancePrice = 2e18;

// LP1 rebalance
vm.startPrank(lp1);
reserveToken.approve(address(pool), expectedAmount);
pool.rebalancePool(lp1, rebalancePrice, expectedAmount, isDeposit);
vm.stopPrank();

// LP2 rebalance
vm.startPrank(lp2);
reserveToken.approve(address(pool), expectedAmount);
pool.rebalancePool(lp2, rebalancePrice, expectedAmount, isDeposit);
vm.stopPrank();

// Move to next cycle start
vm.warp(block.timestamp + REBALANCE_PERIOD + 1);

}
}

// Mock ERC20 contract for testing
Expand Down

0 comments on commit 69a828f

Please sign in to comment.