This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
GEN-1630: increase test coverage #12
Draft
fedealconada
wants to merge
4
commits into
main
Choose a base branch
from
GEN-1630-more-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# interfaces are automatically ignored by forge coverage | ||
ignore: | ||
- "**/**.t*.sol" # ignore test files (ending in t*.sol) | ||
- "**/**.s.sol" # ignore script files (ending in s.sol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,8 +133,8 @@ contract AutoRollerTest is DSTestPlus { | |
vm.stopPrank(); | ||
|
||
// Mint Target | ||
target.mint(address(this), 2e18); | ||
target.approve(address(autoRoller), 2e18); | ||
target.mint(address(this), 10e18); | ||
target.approve(address(autoRoller), 10e18); | ||
|
||
// Mint Stake | ||
stake.mint(address(this), 1e18); | ||
|
@@ -152,6 +152,42 @@ contract AutoRollerTest is DSTestPlus { | |
|
||
// Auth | ||
|
||
function testUpdateAdminParams() public { | ||
vm.record(); | ||
|
||
// 1. Impersonate owner and try to update admin params | ||
vm.startPrank(address(this)); | ||
|
||
autoRoller.setParam("SPACE_FACTORY", address(0xbabe)); | ||
|
||
autoRoller.setParam("PERIPHERY", address(0xbabe)); | ||
|
||
vm.expectRevert(); | ||
autoRoller.setParam("UNKNOWN", address(0xbabe)); | ||
|
||
autoRoller.setParam("OWNER", address(0xbabe)); | ||
|
||
vm.stopPrank(); | ||
|
||
// 2. Impersonate new owner try to update rest of admin params | ||
vm.startPrank(address(0xbabe)); | ||
|
||
autoRoller.setParam("MAX_RATE", 1337); | ||
|
||
autoRoller.setParam("TARGET_DURATION", 1337); | ||
|
||
autoRoller.setParam("COOLDOWN", 1337); | ||
|
||
vm.expectRevert(); | ||
autoRoller.setParam("UNKNOWN", 1337); | ||
|
||
vm.stopPrank(); | ||
|
||
(, bytes32[] memory writes) = vm.accesses(address(autoRoller)); | ||
// Check that the expected number of storage slots were written to | ||
assertEq(writes.length, 6); | ||
} | ||
|
||
function testFuzzUpdateAdminParams(address lad) public { | ||
vm.record(); | ||
vm.assume(lad != address(this)); // For any address other than the testing contract | ||
|
@@ -167,6 +203,9 @@ contract AutoRollerTest is DSTestPlus { | |
vm.expectRevert(); | ||
autoRoller.setParam("OWNER", address(0xbabe)); | ||
|
||
vm.expectRevert(); | ||
autoRoller.setParam("UNKNOWN", address(0xbabe)); | ||
|
||
vm.expectRevert(); | ||
autoRoller.setParam("MAX_RATE", 1337); | ||
|
||
|
@@ -176,6 +215,9 @@ contract AutoRollerTest is DSTestPlus { | |
vm.expectRevert(); | ||
autoRoller.setParam("COOLDOWN", 1337); | ||
|
||
vm.expectRevert(); | ||
autoRoller.setParam("UNKNOWN", 1337); | ||
|
||
(, bytes32[] memory writes) = vm.accesses(address(autoRoller)); | ||
// Check that no storage slots were written to | ||
assertEq(writes.length, 0); | ||
|
@@ -936,12 +978,187 @@ contract AutoRollerTest is DSTestPlus { | |
utils.getNewTargetedRate(0, address(mockAdapter), maturity, space); | ||
|
||
vm.warp(maturity); | ||
|
||
// Scale goes down | ||
mockAdapter.setScale(0.9e18); | ||
autoRoller.settle(); | ||
|
||
// Targeted rate is 0 if scale has gone down. | ||
mockAdapter.setScale(0.9e18); | ||
uint256 targetedRate = utils.getNewTargetedRate(0, address(mockAdapter), maturity, space); | ||
assertEq(targetedRate, 0); | ||
|
||
vm.warp(maturity + autoRoller.cooldown()); | ||
|
||
// Roll into new series | ||
autoRoller.roll(); | ||
|
||
// Scale goes up | ||
mockAdapter.setScale(1.9e18); | ||
|
||
maturity = autoRoller.maturity(); | ||
vm.warp(maturity); | ||
|
||
autoRoller.settle(); | ||
|
||
// Targeted rate is not 0 TODO: maybe assert against the rate that should be | ||
targetedRate = utils.getNewTargetedRate(0, address(mockAdapter), maturity, space); | ||
assertGt(targetedRate, 0); | ||
} | ||
|
||
// OTHER TESTS (LINES NOT COVERED ACCORDING CODECOV) TODO: re-order these tests? | ||
|
||
function testPreviewWithdrawInsufficentLiquidity(uint256 amount) public { | ||
// 1. Roll into the first Series. | ||
autoRoller.roll(); | ||
|
||
// 2. Make a deposit. | ||
autoRoller.deposit(1e18, address(this)); | ||
|
||
// 3. Expect revert because of InsufficientLiquidity. | ||
vm.expectRevert(abi.encodeWithSelector(AutoRoller.InsufficientLiquidity.selector)); | ||
autoRoller.previewWithdraw(100e18); | ||
} | ||
|
||
function testMaxWithdrawDuringCooldown(uint256 amount) public { | ||
// 1. Make a deposit durring cooldown. | ||
autoRoller.deposit(1e18, address(this)); | ||
|
||
// 2. Assert max withdraw is same as deposit. | ||
uint256 maxWithdraw = autoRoller.maxWithdraw(address(this)); | ||
assertEq(maxWithdraw, 1e18); | ||
} | ||
|
||
function testRedeemWithYTsExcess() public { | ||
// 1. Deposit during the initial cooldown phase. | ||
autoRoller.deposit(2e18, address(this)); | ||
assertEq(autoRoller.balanceOf(address(this)), 2e18); | ||
|
||
// 2. Roll the Target into the first Series. | ||
autoRoller.roll(); | ||
|
||
// 3. Deposit during the first active phase. | ||
autoRoller.deposit(3e18, address(this)); | ||
assertRelApproxEq(autoRoller.balanceOf(address(this)), 5e18, 0.0001e18 /* 0.01% */); | ||
|
||
// 4. Redeem all shares while still in the active phase. | ||
uint256 targetBalPre = target.balanceOf(address(this)); | ||
vm.expectCall(address(periphery), abi.encodeWithSelector(periphery.swapYTsForTarget.selector)); | ||
autoRoller.redeem(autoRoller.balanceOf(address(this)), address(this), address(this)); | ||
uint256 targetBalPost = target.balanceOf(address(this)); | ||
uint256 scalingFactor = 10**(18 - autoRoller.decimals()); | ||
uint256 firstDeposit = (0.01e18 - 1) / scalingFactor + 1; | ||
// assertRelApproxEq(targetBalPost - targetBalPre, 5e18 - firstDeposit, 0.0001e18 /* 0.01% */); // TODO: what should it be the expected value? | ||
|
||
// Check that the Target dust leftover is small | ||
assertLt(target.balanceOf(address(autoRoller)), 1e9); | ||
} | ||
|
||
function testMaxRedeemDuringCooldown(uint256 amount) public { | ||
// 1. Make a deposit during cooldown. | ||
uint256 sharesOut = autoRoller.deposit(0.1e18, address(this)); | ||
|
||
// 2. Assert max withdraw is same sharesOut. | ||
assertEq(autoRoller.maxRedeem(address(this)), sharesOut); | ||
|
||
// 3. Roll into the first Series. | ||
autoRoller.roll(); | ||
|
||
// 4. Make another deposit during cooldown. | ||
sharesOut += autoRoller.deposit(0.1e18, address(this)); | ||
|
||
vm.warp(autoRoller.maturity()); | ||
autoRoller.settle(); | ||
|
||
// 5. Assert max withdraw is same total shares. | ||
assertEq(autoRoller.maxRedeem(address(this)), sharesOut); | ||
} | ||
|
||
function testMaxRedeemWithPTsExcess(uint256 amount) public { | ||
// 1. Make a deposit durring cooldown. | ||
uint256 sharesOut = autoRoller.deposit(1e18, address(this)); | ||
|
||
// 2. Roll into the first Series. | ||
autoRoller.roll(); | ||
|
||
// 3. Issue PTs. | ||
target.mint(address(this), 1e18); | ||
target.approve(address(divider), 1e18); | ||
divider.issue(address(mockAdapter), autoRoller.maturity(), 1e18); | ||
ERC20 pt = ERC20(divider.pt(address(mockAdapter), autoRoller.maturity())); | ||
|
||
// 4. Swap PTs in to generate excess of PTs and that the number of PTs we can sell | ||
// is less than the diff between YTs and PTs. | ||
Space space = Space(spaceFactory.pools(address(mockAdapter), autoRoller.maturity())); | ||
pt.approve(address(balancerVault), 1e18); | ||
_swap( | ||
BalancerVault.SingleSwap({ | ||
poolId: space.getPoolId(), | ||
kind: BalancerVault.SwapKind.GIVEN_IN, | ||
assetIn: address(pt), | ||
assetOut: address(autoRoller.asset()), | ||
amount: 0.001e18, // TODO: how do I calculate a number such that it would make the maxPTSale < diff? This number works though | ||
userData: hex"" | ||
}) | ||
); | ||
|
||
// 5. Assert max redeem is same as sharesOut. | ||
assertEq(autoRoller.maxRedeem(address(this)), sharesOut); | ||
|
||
// 6. Swap more PTs in to generate excess of PTs and that the number of PTs we can sell | ||
// is more than the diff between YTs and PTs. | ||
_swap( | ||
BalancerVault.SingleSwap({ | ||
poolId: space.getPoolId(), | ||
kind: BalancerVault.SwapKind.GIVEN_IN, | ||
assetIn: address(pt), | ||
assetOut: address(autoRoller.asset()), | ||
amount: 0.01e18, // TODO: how do I calculate a number such that it would make the maxPTSale >= diff? This number works though | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, the only case where this isn't true IIRC is if order we'd have to swap here such that the pool becomes very close to the Note that you can check the rate "(pt reserves + total supply / (target reserve * scale))" |
||
userData: hex"" | ||
}) | ||
); | ||
|
||
// 5. Assert max redeem is same as sharesOut. | ||
sharesOut = autoRoller.maxRedeem(address(this)); | ||
assertEq(autoRoller.maxRedeem(address(this)), sharesOut); | ||
|
||
// 6. TODO: add missing scenario: `if (ptReserves >= diff)`. | ||
} | ||
|
||
function testEjectWithPTsExcess() public { | ||
// 1. Deposit during the initial cooldown phase. | ||
autoRoller.deposit(1e18, address(this)); | ||
|
||
// 2. Roll into the first Series. | ||
autoRoller.roll(); | ||
|
||
// 3. Swap PTs in to generate excess of PTs. | ||
target.mint(address(this), 1e18); | ||
target.approve(address(divider), 1e18); | ||
divider.issue(address(mockAdapter), autoRoller.maturity(), 1e18); | ||
|
||
ERC20 pt = ERC20(divider.pt(address(mockAdapter), autoRoller.maturity())); | ||
Space space = Space(spaceFactory.pools(address(mockAdapter), autoRoller.maturity())); | ||
|
||
pt.approve(address(balancerVault), 1e18); | ||
_swap( | ||
BalancerVault.SingleSwap({ | ||
poolId: space.getPoolId(), | ||
kind: BalancerVault.SwapKind.GIVEN_IN, | ||
assetIn: address(pt), | ||
assetOut: address(autoRoller.asset()), | ||
amount: 0.001e18, | ||
userData: hex"" | ||
}) | ||
); | ||
|
||
// 4. Eject everything. | ||
uint256 ptBalBefore = pt.balanceOf(address(this)); | ||
( , uint256 excessBal, bool isExcessPTs) = autoRoller.eject(autoRoller.balanceOf(address(this)), address(this), address(this)); | ||
|
||
// Expect just a little PT excess. | ||
assertBoolEq(isExcessPTs, true); | ||
assertLt(excessBal, 1e16); | ||
assertEq(excessBal, pt.balanceOf(address(this)) - ptBalBefore); | ||
} | ||
|
||
// function testRedeemPreviewReversion() public { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice check here 👌