Skip to content

Commit

Permalink
tests: add referral deposit test and remove max mint one
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtekgrinder committed Aug 6, 2024
1 parent 3dca55a commit 18491d6
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions test/foundry/AngeRouterMainnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,35 +147,36 @@ contract AngleRouterMainnetTest is BaseTest {
assertEq(token.balanceOf(address(to)), 0);
}

function testMint4626MaxBalance(
function testDeposit4626GoodPractice(
uint256 initShares,
uint256 maxAmount,
uint256 amount,
uint256 minSharesOut,
address to,
uint256 gainOrLoss
) public {
address to = address(router);
vm.assume(to != address(0) && to != address(router) && to != address(savingsRate));

uint256 balanceUsers = BASE_TOKENS * 1 ether;
deal(address(token), address(_alice), balanceUsers);

_randomizeSavingsRate(gainOrLoss, initShares);

uint256 shares = savingsRate.previewDeposit(balanceUsers);
uint256 previewMint = savingsRate.previewMint(shares);
amount = bound(amount, 0, balanceUsers);
uint256 previewDeposit = savingsRate.previewDeposit(amount);

// this can be done with foundry though
// https://book.getfoundry.sh/tutorials/testing-eip712?highlight=permit#diving-in
PermitType[] memory paramsPermit = new PermitType[](0);
ActionType[] memory actionType = new ActionType[](2);
bytes[] memory data = new bytes[](2);

actionType[0] = ActionType.transfer;
data[0] = abi.encode(token, router, previewMint);
actionType[1] = ActionType.mint4626;
data[1] = abi.encode(token, savingsRate, type(uint256).max, to, maxAmount);
data[0] = abi.encode(token, router, amount);
actionType[1] = ActionType.deposit4626;
data[1] = abi.encode(token, savingsRate, amount, to, minSharesOut);

vm.startPrank(_alice);
token.approve(address(router), type(uint256).max);
// as this is a mock vault, previewMint is exactly what is needed to mint
if (maxAmount < previewMint) {
if (previewDeposit < minSharesOut) {
vm.expectRevert(BaseRouter.TooSmallAmountOut.selector);
router.mixer(paramsPermit, actionType, data);
return;
Expand All @@ -184,21 +185,22 @@ contract AngleRouterMainnetTest is BaseTest {
}
vm.stopPrank();

assertEq(savingsRate.balanceOf(address(to)), shares);
assertEq(savingsRate.balanceOf(address(router)), 0);
assertEq(savingsRate.balanceOf(address(_alice)), 0);
assertEq(savingsRate.balanceOf(address(to)), previewDeposit);

assertEq(token.balanceOf(address(router)), 0);
assertEq(token.balanceOf(address(_alice)), balanceUsers - previewMint);
assertEq(token.balanceOf(address(_alice)), balanceUsers - amount);
assertEq(token.balanceOf(address(to)), 0);
}

function testDeposit4626GoodPractice(
function testDeposit4626MaxBalance(
uint256 initShares,
uint256 amount,
uint256 minSharesOut,
address to,
uint256 gainOrLoss
) public {
vm.assume(to != address(0) && to != address(router) && to != address(savingsRate));
address to = address(router);

uint256 balanceUsers = BASE_TOKENS * 1 ether;
deal(address(token), address(_alice), balanceUsers);
Expand All @@ -215,7 +217,9 @@ contract AngleRouterMainnetTest is BaseTest {
actionType[0] = ActionType.transfer;
data[0] = abi.encode(token, router, amount);
actionType[1] = ActionType.deposit4626;
data[1] = abi.encode(token, savingsRate, amount, to, minSharesOut);
data[1] = abi.encode(token, savingsRate, type(uint256).max, to, minSharesOut);

uint256 mintedShares = savingsRate.convertToShares(amount);

vm.startPrank(_alice);
token.approve(address(router), type(uint256).max);
Expand All @@ -229,16 +233,14 @@ contract AngleRouterMainnetTest is BaseTest {
}
vm.stopPrank();

assertEq(savingsRate.balanceOf(address(router)), 0);
assertEq(savingsRate.balanceOf(address(_alice)), 0);
assertEq(savingsRate.balanceOf(address(to)), previewDeposit);
assertEq(savingsRate.balanceOf(address(to)), mintedShares);

assertEq(token.balanceOf(address(router)), 0);
assertEq(token.balanceOf(address(_alice)), balanceUsers - amount);
assertEq(token.balanceOf(address(to)), 0);
}

function testDeposit4626MaxBalance(
function testDeposit4626ForgotFunds(
uint256 initShares,
uint256 amount,
uint256 minSharesOut,
Expand All @@ -261,9 +263,7 @@ contract AngleRouterMainnetTest is BaseTest {
actionType[0] = ActionType.transfer;
data[0] = abi.encode(token, router, amount);
actionType[1] = ActionType.deposit4626;
data[1] = abi.encode(token, savingsRate, type(uint256).max, to, minSharesOut);

uint256 mintedShares = savingsRate.convertToShares(amount);
data[1] = abi.encode(token, savingsRate, amount, to, minSharesOut);

vm.startPrank(_alice);
token.approve(address(router), type(uint256).max);
Expand All @@ -278,17 +278,17 @@ contract AngleRouterMainnetTest is BaseTest {
vm.stopPrank();

assertEq(savingsRate.balanceOf(address(to)), previewDeposit);
assertEq(savingsRate.balanceOf(address(to)), mintedShares);

assertEq(token.balanceOf(address(router)), 0);
assertEq(token.balanceOf(address(_alice)), balanceUsers - amount);
}

function testDeposit4626ForgotFunds(
function testDepositReferral4626MaxBalance(
uint256 initShares,
uint256 amount,
uint256 minSharesOut,
uint256 gainOrLoss
uint256 gainOrLoss,
address referrer
) public {
address to = address(router);

Expand All @@ -306,8 +306,10 @@ contract AngleRouterMainnetTest is BaseTest {

actionType[0] = ActionType.transfer;
data[0] = abi.encode(token, router, amount);
actionType[1] = ActionType.deposit4626;
data[1] = abi.encode(token, savingsRate, amount, to, minSharesOut);
actionType[1] = ActionType.deposit4626Referral;
data[1] = abi.encode(token, savingsRate, type(uint256).max, to, minSharesOut, referrer);

uint256 mintedShares = savingsRate.convertToShares(amount);

vm.startPrank(_alice);
token.approve(address(router), type(uint256).max);
Expand All @@ -322,6 +324,7 @@ contract AngleRouterMainnetTest is BaseTest {
vm.stopPrank();

assertEq(savingsRate.balanceOf(address(to)), previewDeposit);
assertEq(savingsRate.balanceOf(address(to)), mintedShares);

assertEq(token.balanceOf(address(router)), 0);
assertEq(token.balanceOf(address(_alice)), balanceUsers - amount);
Expand Down

0 comments on commit 18491d6

Please sign in to comment.