Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/withdrawal tests #207

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions contracts/tests/LineOfCredit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ contract LineTest is Test, Events {
lentAmount,
"Line balance should be 1e18 / 2"
);
uint256 lineBalanceBefore = supportedToken1.balanceOf(address(line));
emit log_named_uint("lineBalanceBefore", lineBalanceBefore);

hoax(lender);
line.withdraw(id, withdrawalAmount);
assertEq(
Expand All @@ -444,6 +447,11 @@ contract LineTest is Test, Events {
"Line balance should be 1e18 + 0.4"
);

uint256 lineBalanceAfter = supportedToken1.balanceOf(address(line));
emit log_named_uint("lineBalanceAfter", lineBalanceAfter);

assertEq(lineBalanceBefore, lineBalanceAfter + withdrawalAmount);

emit log_named_uint("lender balance", supportedToken1.balanceOf(lender));
emit log_named_uint("lent amnt", lentAmount);
emit log_named_uint("withdraw amnt", withdrawalAmount);
Expand Down Expand Up @@ -1252,6 +1260,54 @@ contract LineTest is Test, Events {
assertEq(uint256(line.status()), uint256(LineLib.STATUS.REPAID));
}

function test_balance_is_correct_after_repayment_and_withdrawal(uint256 credit, uint256 ttl) public {
credit = bound(credit, 1 ether, mintAmount);
ttl = bound(ttl, 1 days, 365 days);

_addCredit(address(supportedToken1), credit);
bytes32 id = line.ids(0);
hoax(borrower);
line.borrow(id, credit);

vm.warp(block.timestamp + ttl);
(uint256 p, uint256 i) = line.updateOutstandingDebt();
emit log_named_uint("principal", p);
emit log_named_uint("interest", i);


uint256 borrowerBalance = supportedToken1.balanceOf(borrower);

(,uint256 principalRemaining, uint256 interestAccrued,,,,,) = line.credits(id);

uint256 repayment = borrowerBalance > principalRemaining + interestAccrued ? principalRemaining + interestAccrued : borrowerBalance;

uint256 lineBalanceBefore = supportedToken1.balanceOf(address(line));
emit log_named_uint("line balance before repayment", lineBalanceBefore);
vm.startPrank(borrower);
line.depositAndRepay(repayment);
vm.stopPrank();

uint256 lineBalanceAfter = supportedToken1.balanceOf(address(line));
emit log_named_uint("line balance after repayment", lineBalanceAfter);

uint256 lenderBalanceBefore = supportedToken1.balanceOf(lender);
emit log_named_uint("lenderBalance before withdrawal", lenderBalanceBefore);

vm.startPrank(lender);
line.withdraw(id, repayment);
vm.stopPrank();

uint256 lenderBalanceAfter = supportedToken1.balanceOf(lender);
emit log_named_uint("lenderBalance after withdrawal", lenderBalanceAfter);

assertEq(lenderBalanceAfter, lenderBalanceBefore + repayment);

uint256 lineBalance = supportedToken1.balanceOf(address(line));
emit log_named_uint("line balance after withdrawal", lineBalance);

assertEq(lineBalance, lineBalanceAfter - repayment);
}

// Uncomment to check gas limit threshhold for ids
// function test_max_lenders_can_exist_before_contract_gets_bricked() public {
// for (uint maxPossible;; ++maxPossible) {
Expand Down