Skip to content

Commit

Permalink
feat: add fee test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Grimal committed Dec 11, 2023
1 parent ffaa6bf commit 251c77c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 53 deletions.
68 changes: 68 additions & 0 deletions test/forge/FeeTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract FeeTest is IntegrationTest {
using Math for uint256;
using MathLib for uint256;
using MarketParamsLib for MarketParams;
using MorphoBalancesLib for IMorpho;

function setUp() public override {
super.setUp();
Expand Down Expand Up @@ -348,4 +349,71 @@ contract FeeTest is IntegrationTest {
assertEq(assets, expectedAssets, "assets");
assertGe(assets, assetsBefore, "assets increased");
}

struct testUpdateWithdrawQueueRemovingDisabledMarketVars {
uint256 firstAmountSupplied;
uint256 secondAmountSupplied;
uint256 firstAmountBorrowed;
uint256 secondAmountBorrowed;
uint256 blocks;
}

function testUpdateWithdrawQueueRemovingDisabledMarketWithFee(
uint256 firstAmountSupplied,
uint256 secondAmountSupplied,
address newFeeRecipient,
uint256 blocks
) public {
firstAmountSupplied = bound(firstAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
secondAmountSupplied = bound(secondAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
blocks = _boundBlocks(blocks);
vm.assume(newFeeRecipient != FEE_RECIPIENT && newFeeRecipient != address(0));

_setCap(allMarkets[0], firstAmountSupplied);
_setCap(allMarkets[1], secondAmountSupplied);

Id[] memory supplyQueue = new Id[](2);
supplyQueue[0] = allMarkets[0].id();
supplyQueue[1] = allMarkets[1].id();

vm.prank(ALLOCATOR);
vault.setSupplyQueue(supplyQueue);

loanToken.setBalance(SUPPLIER, firstAmountSupplied + secondAmountSupplied);

vm.prank(SUPPLIER);
vault.deposit(firstAmountSupplied + secondAmountSupplied, ONBEHALF);

_setCap(allMarkets[1], 0);

vm.prank(CURATOR);
vault.submitMarketRemoval(allMarkets[1].id());

vm.warp(block.timestamp + TIMELOCK);

uint256[] memory indexes = new uint256[](2);
indexes[0] = 0;
indexes[1] = 1;

vm.prank(ALLOCATOR);
vault.updateWithdrawQueue(indexes);

assertApproxEqAbs(vault.lastTotalAssets(), firstAmountSupplied, 1, "lastTotalAssets");

_forward(blocks);

uint256 expectedTotalAssets = morpho.expectedSupplyAssets(allMarkets[0], address(vault));
uint256 expectedFeeAssets = (expectedTotalAssets - vault.lastTotalAssets()).mulDiv(FEE, WAD);
uint256 expectedFeeShares = expectedFeeAssets.mulDiv(
vault.totalSupply() + 10 ** ConstantsLib.DECIMALS_OFFSET,
expectedTotalAssets - expectedFeeAssets + 1,
Math.Rounding.Floor
);

// Set new fee recipient to accrue fee.
vm.prank(OWNER);
vault.setFeeRecipient(newFeeRecipient);

assertEq(vault.balanceOf(FEE_RECIPIENT), expectedFeeShares, "feeShares");
}
}
54 changes: 1 addition & 53 deletions test/forge/MarketTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,59 +152,7 @@ contract MarketTest is IntegrationTest {
assertEq(Id.unwrap(vault.withdrawQueue(3)), Id.unwrap(expectedWithdrawQueue[3]));
}

function testUpdateWithdrawQueueRemovingDisabledMarket(uint256 firstAmountSupplied, uint256 secondAmountSupplied)
public
{
firstAmountSupplied = bound(firstAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
secondAmountSupplied = bound(secondAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);

_setCap(allMarkets[0], firstAmountSupplied);
_setCap(allMarkets[1], secondAmountSupplied);

Id[] memory supplyQueue = new Id[](2);
supplyQueue[0] = allMarkets[0].id();
supplyQueue[1] = allMarkets[1].id();

_setCap(allMarkets[0], firstAmountSupplied);
_setCap(allMarkets[1], secondAmountSupplied);
vm.prank(ALLOCATOR);
vault.setSupplyQueue(supplyQueue);

loanToken.setBalance(SUPPLIER, firstAmountSupplied + secondAmountSupplied);

vm.prank(SUPPLIER);
vault.deposit(firstAmountSupplied + secondAmountSupplied, ONBEHALF);

_setCap(allMarkets[1], 0);

vm.prank(CURATOR);
vault.submitMarketRemoval(allMarkets[1].id());

vm.warp(block.timestamp + TIMELOCK);

uint256[] memory indexes = new uint256[](3);
indexes[0] = 0;
indexes[1] = 1;
indexes[2] = 3;

Id[] memory expectedWithdrawQueue = new Id[](3);
expectedWithdrawQueue[0] = idleParams.id();
expectedWithdrawQueue[1] = allMarkets[0].id();
expectedWithdrawQueue[2] = allMarkets[2].id();

vm.expectEmit();
emit EventsLib.SetWithdrawQueue(ALLOCATOR, expectedWithdrawQueue);
vm.prank(ALLOCATOR);
vault.updateWithdrawQueue(indexes);

assertEq(Id.unwrap(vault.withdrawQueue(0)), Id.unwrap(expectedWithdrawQueue[0]));
assertEq(Id.unwrap(vault.withdrawQueue(1)), Id.unwrap(expectedWithdrawQueue[1]));
assertEq(Id.unwrap(vault.withdrawQueue(2)), Id.unwrap(expectedWithdrawQueue[2]));
assertFalse(vault.config(allMarkets[1].id()).enabled);
assertEq(vault.lastTotalAssets(), firstAmountSupplied);
}

function testUpdateWithdrawQueueRemovingDisabledMarketWithSupply() public {
function testUpdateWithdrawQueueRemovingDisabledMarket() public {
_setCap(allMarkets[2], 0);

vm.prank(CURATOR);
Expand Down

0 comments on commit 251c77c

Please sign in to comment.