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: fixed audit findings #23

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions contracts/DebtLocker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ contract DebtLocker is IDebtLocker, DebtLockerStorage, MapleProxied {
require(amount_ == uint256(0) || ERC20Helper.transfer(loan_.fundsAsset(), address(_loan), amount_));

loan_.acceptNewTerms(refinancer_, calls_, uint256(0));

_principalRemainingAtLastClaim = loan_.principal();
}

function setFundsToCapture(uint256 amount_) override external {
Expand Down Expand Up @@ -209,19 +211,24 @@ contract DebtLocker is IDebtLocker, DebtLockerStorage, MapleProxied {
require(!_isLiquidationActive(), "DL:HCOR:LIQ_NOT_FINISHED");

address fundsAsset = IMapleLoanLike(_loan).fundsAsset();
uint256 recoveredFunds = IERC20Like(fundsAsset).balanceOf(address(this)); // Funds recovered from liquidation and any unclaimed previous payment amounts
uint256 principalToCover = _principalRemainingAtLastClaim; // Principal remaining at time of liquidation
uint256 principalToCover = _principalRemainingAtLastClaim; // Principal remaining at time of liquidation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs another space before comment

uint256 fundsCaptured = _fundsToCapture;

// Funds recovered from liquidation and any unclaimed previous payment amounts
uint256 recoveredFunds = IERC20Like(fundsAsset).balanceOf(address(this)) - fundsCaptured;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space


// If `recoveredFunds` is greater than `principalToCover`, the remaining amount is treated as interest in the context of the pool.
// If `recoveredFunds` is less than `principalToCover`, the difference is registered as a shortfall.
details_[0] = recoveredFunds;
details_[0] = recoveredFunds + fundsCaptured;
details_[1] = recoveredFunds > principalToCover ? recoveredFunds - principalToCover : 0;
details_[2] = fundsCaptured;
details_[5] = recoveredFunds > principalToCover ? principalToCover : recoveredFunds;
details_[6] = principalToCover > recoveredFunds ? principalToCover - recoveredFunds : 0;

require(ERC20Helper.transfer(fundsAsset, _pool, recoveredFunds), "DL:HCOR:TRANSFER");
require(ERC20Helper.transfer(fundsAsset, _pool, recoveredFunds + fundsCaptured), "DL:HCOR:TRANSFER");

_repossessed = false;
_fundsToCapture = uint256(0);
_repossessed = false;
}

function _handleClaim() internal returns (uint256[7] memory details_) {
Expand Down
38 changes: 37 additions & 1 deletion contracts/test/DebtLocker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ contract DebtLockerTest is TestUtils {

uint256 principalAfter = loan.principal();

assertEq(principalBefore + principalIncrease_, principalAfter);
assertEq(principalBefore + principalIncrease_, principalAfter);
assertEq(debtLocker.principalRemainingAtLastClaim(), principalAfter);
}

function test_fundsToCaptureForNextClaim() public {
Expand Down Expand Up @@ -590,4 +591,39 @@ contract DebtLockerTest is TestUtils {
pool.claim(address(debtLocker));
}

function test_fundsToCaptureWhileInDefault() public {
( loan, debtLocker ) = _createFundAndDrawdownLoan(1_000_000);

// Prepare additional amount to be captured
fundsAsset.mint(address(debtLocker), 500_000);

assertEq(fundsAsset.balanceOf(address(debtLocker)), 500_000);
assertEq(fundsAsset.balanceOf(address(pool)), 0);
assertEq(debtLocker.principalRemainingAtLastClaim(), loan.principalRequested());
assertEq(debtLocker.fundsToCapture(), 0);

//trigger default

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces after all //

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also sentence case

hevm.warp(loan.nextPaymentDueDate() + loan.gracePeriod() + 1);

pool.triggerDefault(address(debtLocker));

//After triggering default, set funds to capture
poolDelegate.debtLocker_setFundsToCapture(address(debtLocker), 500_000);

//claim
uint256[7] memory details = pool.claim(address(debtLocker));

assertEq(fundsAsset.balanceOf(address(debtLocker)), 0);
assertEq(fundsAsset.balanceOf(address(pool)), 500_000);
assertEq(debtLocker.fundsToCapture(), 0);

assertEq(details[0], 500_000);
assertEq(details[1], 0);
assertEq(details[2], 500_000);
assertEq(details[3], 0);
assertEq(details[4], 0);
assertEq(details[5], 0);
assertEq(details[6], loan.principalRequested()); // No principal was recovered
}

}