Skip to content

Commit

Permalink
test: unit tests for withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
antoncoding committed Dec 12, 2022
1 parent 40bae47 commit 8f361ee
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ UNIT_LendingAssetHook:testCannotCallHandleAdjustmentFromNonAccount() (gas: 11405
UNIT_LendingAssetHook:testCannotExecuteHandleAdjustmentIfManagerIsNotWhitelisted() (gas: 16113)
UNIT_LendingAssetHook:testChangeManagerHookRevertOnNonWhitelistedManager() (gas: 15173)
UNIT_LendingAssetHook:testWillNotRevertOnLegalManagerUpdate() (gas: 38184)
UNIT_LendingWithdraw:testBorrowIfManagerAllows() (gas: 200467)
UNIT_LendingWithdraw:testCanWithdrawByAccountAmount() (gas: 84264)
UNIT_LendingWithdraw:testCannotWithdrawFromAccountNotControlledByTrustedManager() (gas: 117776)
UNIT_LendingWithdraw:testCannotWithdrawFromOthersAccount() (gas: 18522)
DecimalMathTest:testConversionSameDecimals() (gas: 5772)
DecimalMathTest:testConversionScaleDown() (gas: 7972)
DecimalMathTest:testConversionScaleUp() (gas: 7965)
6 changes: 3 additions & 3 deletions src/assets/Lending.sol
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ contract Lending is Owned, IAsset {
/**
* @dev get current account cash balance
*/
function _getStaleBalance(uint accountId) internal view returns (int balance) {
balance = account.getBalance(accountId, IAsset(address(this)), 0);
}
// function _getStaleBalance(uint accountId) internal view returns (int balance) {
// balance = account.getBalance(accountId, IAsset(address(this)), 0);
// }

////////////////
// Errors //
Expand Down
1 change: 0 additions & 1 deletion test/assets/lending/unit-tests/Deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "forge-std/console2.sol";

import "../../../shared/mocks/MockERC20.sol";
import "../../../shared/mocks/MockManager.sol";
Expand Down
92 changes: 92 additions & 0 deletions test/assets/lending/unit-tests/Withdraw.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "forge-std/console2.sol";

import "../../../shared/mocks/MockERC20.sol";
import "../../../shared/mocks/MockManager.sol";

import "../../../../src/assets/Lending.sol";
import "../../../../src/Account.sol";

/**
* @dev we deploy actual Account contract in these tests to simplify verification process
*/
contract UNIT_LendingWithdraw is Test {
Lending lending;
MockERC20 usdc;
MockManager manager;
MockManager badManager;
Account account;
address badActor = address(0x0fac);

uint accountId;
uint depositedAmount;

function setUp() public {
account = new Account("Lyra Margin Accounts", "LyraMarginNFTs");

manager = new MockManager(address(account));
badManager = new MockManager(address(account));

usdc = new MockERC20("USDC", "USDC");

lending = new Lending(address(account), address(usdc));

lending.setWhitelistManager(address(manager), true);

// 10000 USDC with 18 decimals
depositedAmount = 1000_000 ether;
usdc.mint(address(this), depositedAmount);
usdc.approve(address(lending), type(uint).max);

accountId = account.createAccount(address(this), manager);

lending.deposit(accountId, depositedAmount);
}

function testCanWithdrawByAccountAmount() public {
uint withdrawAmount = 100 ether;
uint usdcBefore = usdc.balanceOf(address(this));
lending.withdraw(accountId, withdrawAmount, address(this));
uint usdcAfter = usdc.balanceOf(address(this));

assertEq(usdcAfter - usdcBefore, withdrawAmount);

// cash balance updated in account
int accBalance = account.getBalance(accountId, lending, 0);
assertEq(accBalance, int(depositedAmount - withdrawAmount));
}

function testCannotWithdrawFromOthersAccount() public {
vm.prank(badActor);
vm.expectRevert(Lending.LA_OnlyAccountOwner.selector);
lending.withdraw(accountId, 100 ether, address(this));
}

function testCannotWithdrawFromAccountNotControlledByTrustedManager() public {
uint badAccount = account.createAccount(address(this), badManager);
vm.expectRevert(Lending.LA_UnknownManager.selector);
lending.withdraw(badAccount, 100 ether, address(this));
}

function testBorrowIfManagerAllows() public {
// user with an empty account (no cash balance) can withdraw USDC
// essentially borrow from the lending contract
uint emptyAccount = account.createAccount(address(this), manager);

uint amountToBorrow = 1000 ether;

uint usdcBefore = usdc.balanceOf(address(this));
lending.withdraw(emptyAccount, amountToBorrow, address(this));
uint usdcAfter = usdc.balanceOf(address(this));

assertEq(usdcAfter - usdcBefore, amountToBorrow);

int accBalance = account.getBalance(emptyAccount, lending, 0);

// todo: number might change based on interest
assertEq(accBalance, -(int(amountToBorrow)));
}
}

0 comments on commit 8f361ee

Please sign in to comment.