Skip to content

Commit

Permalink
audit refix l12
Browse files Browse the repository at this point in the history
  • Loading branch information
lsqrl committed Jan 15, 2024
1 parent 04cd8b4 commit 36f7dc6
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/Manager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ contract Manager is IManager, Ownable {
abi.encodePacked(type(Vault).creationCode, abi.encode(IERC20Metadata(token)))
);
vaults[token] = vault;
// deposit 1 token unit to avoid the typical ERC4626 issue
// deposit 1000 wei unit to avoid the typical ERC4626 issue
// by placing the resulting iToken in the manager, it becomes unredeemable
// therefore, the Vault is guaranteed to always stay in a healthy status
IERC20 tkn = IERC20(token);
tkn.safeTransferFrom(msg.sender, address(this), 1);
tkn.approve(vault, 1);
IVault(vault).deposit(1, address(this));
tkn.safeTransferFrom(msg.sender, address(this), 1000);
tkn.approve(vault, 1000);
IVault(vault).deposit(1000, address(this));

emit VaultCreated(token, vault);

Expand Down
14 changes: 7 additions & 7 deletions test/Manager.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ contract ManagerTest is Test {
secondToken.mint(tokenSink, type(uint256).max);
spuriousToken.mint(tokenSink, type(uint256).max);
vm.startPrank(tokenSink);
firstToken.transfer(address(this), 1);
secondToken.transfer(address(this), 1);
firstToken.transfer(address(this), 1000);
secondToken.transfer(address(this), 1000);
vm.stopPrank();
firstToken.approve(address(manager), 1);
secondToken.approve(address(manager), 1);
firstToken.approve(address(manager), 1000);
secondToken.approve(address(manager), 1000);
}

function setUp() public {
Expand Down Expand Up @@ -78,15 +78,15 @@ contract ManagerTest is Test {

function testCreate() public {
vm.prank(tokenSink);
spuriousToken.transfer(address(this), 1);
spuriousToken.approve(address(manager), 1);
spuriousToken.transfer(address(this), 1000);
spuriousToken.approve(address(manager), 1000);
address spuriousVault = manager.create(address(spuriousToken));
assertTrue(manager.vaults(address(spuriousToken)) == spuriousVault);
}

function _setupArbitraryState(uint256 previousDeposit, uint256 cap) private returns (uint256) {
address vaultAddress = manager.vaults(address(firstToken));
if (previousDeposit == type(uint256).max) previousDeposit--;
if (previousDeposit >= type(uint256).max - 999) previousDeposit = type(uint256).max - 1000;
vm.startPrank(tokenSink);
firstToken.approve(vaultAddress, previousDeposit);
IVault(vaultAddress).deposit(previousDeposit, anyAddress);
Expand Down
4 changes: 2 additions & 2 deletions test/services/AaveService.economic.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ contract AaveEconomicTest is Test, IERC721Receiver {
for (uint256 i = 0; i < loanLength; i++) {
// Create Vault: DAI
vm.prank(whales[loanTokens[i]]);
IERC20(loanTokens[i]).transfer(admin, 1);
IERC20(loanTokens[i]).transfer(admin, 1000);
vm.startPrank(admin);
IERC20(loanTokens[i]).approve(address(manager), 1);
IERC20(loanTokens[i]).approve(address(manager), 1000);
manager.create(loanTokens[i]);
// No caps for this service -> 100% of the liquidity can be used initially
manager.setCap(address(service), loanTokens[i], GeneralMath.RESOLUTION, type(uint256).max);
Expand Down
4 changes: 2 additions & 2 deletions test/services/AaveService.general.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ contract AaveGeneralTest is Test, IERC721Receiver {
for (uint256 i = 0; i < loanLength; i++) {
// Create Vault: DAI
vm.prank(whales[loanTokens[i]]);
IERC20(loanTokens[i]).transfer(admin, 1);
IERC20(loanTokens[i]).transfer(admin, 1000);
vm.startPrank(admin);
IERC20(loanTokens[i]).approve(address(manager), 1);
IERC20(loanTokens[i]).approve(address(manager), 1000);
manager.create(loanTokens[i]);
// No caps for this service -> 100% of the liquidity can be used initially
manager.setCap(address(service), loanTokens[i], GeneralMath.RESOLUTION, type(uint256).max);
Expand Down
4 changes: 2 additions & 2 deletions test/services/BaseIntegrationServiceTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ contract BaseIntegrationServiceTest is Test, IERC721Receiver {
for (uint256 i = 0; i < loanLength; i++) {
if (manager.vaults(loanTokens[i]) == address(0)) {
vm.prank(whales[loanTokens[i]]);
IERC20(loanTokens[i]).transfer(admin, 1);
IERC20(loanTokens[i]).transfer(admin, 1000);
vm.startPrank(admin);
IERC20(loanTokens[i]).approve(address(manager), 1);
IERC20(loanTokens[i]).approve(address(manager), 1000);
manager.create(loanTokens[i]);
vm.stopPrank();
}
Expand Down
21 changes: 12 additions & 9 deletions test/services/DebitCredit.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ contract DebitCreditTest is Test, IERC721Receiver {
// recall that the admin needs 1e-6 USDC to create the vault
vm.stopPrank();
vm.prank(usdcWhale);
IERC20(usdc).transfer(address(admin), 1);
IERC20(usdc).transfer(address(admin), 1000);

vm.startPrank(admin);
IERC20(usdc).approve(address(manager), 1);
IERC20(usdc).approve(address(manager), 1000);
manager.create(usdc);

// first price is 0.2 USDC: we need to double it in the constructor
Expand Down Expand Up @@ -239,7 +239,7 @@ contract DebitCreditTest is Test, IERC721Receiver {
assertLe(collaterals[1].amount, (depositedAmount * _rewards[monthsLocked]) / initialPrice);
// price was bumped by at least the allocation percentage (virtually) bougth
assertGe(callOptionService.currentPrice(), initialPrice);
assertEq(vault.freeLiquidity(), 4e9 + 1);
assertEq(vault.freeLiquidity(), 4e9 + 1000);
// free liquidity = 4000 USDC

// now depositedAmount are in the vault -> we take an Aave position
Expand All @@ -250,7 +250,7 @@ contract DebitCreditTest is Test, IERC721Receiver {
uint256 minCollateral = loan + margin;
IERC20(usdc).approve(address(aaveService), margin);
_openAavePosition(margin, loan, minCollateral);
assertEq(vault.freeLiquidity(), 3.6e9 + 1);
assertEq(vault.freeLiquidity(), 3.6e9 + 1000);
vm.stopPrank();
// now, the free liquidity is 3600 USDC

Expand Down Expand Up @@ -281,8 +281,11 @@ contract DebitCreditTest is Test, IERC721Receiver {
vault.balanceOf(treasury),
collaterals[0].amount - vault.convertToShares(((depositedAmount * (1e18 - calledPortion)) / 1e18))
);
assertEq(vault.freeLiquidity(), depositedAmount - loan - (depositedAmount * (1e18 - calledPortion)) / 1e18 + 1);
assertEq(vault.freeLiquidity(), 3.2e9 + 1);
assertEq(
vault.freeLiquidity(),
depositedAmount - loan - (depositedAmount * (1e18 - calledPortion)) / 1e18 + 1000
);
assertEq(vault.freeLiquidity(), 3.2e9 + 1000);
assertEq(vault.balanceOf(treasury), 3.6e9);
vm.stopPrank();
// The free liquidity is now 3200 USDC and treasury has 3600 USDC worth of iTokens
Expand All @@ -293,7 +296,7 @@ contract DebitCreditTest is Test, IERC721Receiver {
monthsLocked = 7;
IERC20(usdc).approve(address(callOptionService), depositedAmount);
_openCallOption(depositedAmount, monthsLocked);
assertEq(vault.freeLiquidity(), 3.5e9 + 1);
assertEq(vault.freeLiquidity(), 3.5e9 + 1000);
vm.stopPrank();
// Now free liquidity is 3500 USDC, and 400 are taken as loan
// Total assets are 3900 USDC, so we can still take 380 USDC as loan
Expand All @@ -312,7 +315,7 @@ contract DebitCreditTest is Test, IERC721Receiver {
// not all liquidity can be withdrawn, let us just redeem 2900 iUSDC
vault.redeem(2.9e9, treasury, treasury);
// Which makes the free liquidity to be 220 USDC
assertEq(vault.freeLiquidity(), 2.2e8 + 1);
assertEq(vault.freeLiquidity(), 2.2e8 + 1000);
vm.stopPrank();

vm.startPrank(callOptionSigner);
Expand Down Expand Up @@ -342,7 +345,7 @@ contract DebitCreditTest is Test, IERC721Receiver {
vm.startPrank(aaveUser);
aaveService.close(0, abi.encode(0));
// it used to be 400 USDC, so now we have them back
assertEq(vault.freeLiquidity(), 6.2e8 + 1);
assertEq(vault.freeLiquidity(), 6.2e8 + 1000);
vm.stopPrank();

// At this point, fees are harvested
Expand Down
8 changes: 4 additions & 4 deletions test/services/GmxService.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ contract GmxServiceTest is BaseIntegrationServiceTest {
usdc.approve(address(service), type(uint256).max);

vm.startPrank(whale);
weth.transfer(admin, 1);
weth.transfer(admin, 1000);
weth.transfer(address(mockRouter), 1e18);
vm.stopPrank();
vm.prank(usdcWhale);
usdc.transfer(admin, 1);
usdc.transfer(admin, 1000);
vm.startPrank(admin);
weth.approve(address(manager), 1);
usdc.approve(address(manager), 1);
weth.approve(address(manager), 1000);
usdc.approve(address(manager), 1000);
manager.create(address(weth));
manager.setCap(address(service), address(weth), GeneralMath.RESOLUTION, type(uint256).max);
manager.create(address(usdc));
Expand Down
4 changes: 2 additions & 2 deletions test/services/SeniorCallOption.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ contract CallOptionTest is BaseIntegrationServiceTest {
vm.stopPrank();

vm.prank(whales[loanTokens[0]]);
IERC20(loanTokens[0]).transfer(admin, 1);
IERC20(loanTokens[0]).transfer(admin, 1000);

vm.startPrank(admin);
IERC20(loanTokens[0]).approve(address(manager), 1);
IERC20(loanTokens[0]).approve(address(manager), 1000);
manager.create(loanTokens[0]);

service = new CallOption(address(manager), address(ithil), 4e17, 1, 86400 * 30, 86400 * 30, 0, loanTokens[0]);
Expand Down
4 changes: 2 additions & 2 deletions test/services/WhitelistedService.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ contract WhitelistedTest is Test, IERC721Receiver {
token.approve(address(service), type(uint256).max);

vm.prank(whitelistedUser);
token.transfer(admin, 1);
token.transfer(admin, 1000);
vm.startPrank(admin);
token.approve(address(manager), 1);
token.approve(address(manager), 1000);
manager.create(address(token));
manager.setCap(address(service), address(token), GeneralMath.RESOLUTION, type(uint256).max);
vm.stopPrank();
Expand Down

0 comments on commit 36f7dc6

Please sign in to comment.