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

VAL-133 Send late fees to the FL Vault (not the Pool) #177

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 2 additions & 8 deletions contracts/Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,7 @@ contract Loan is ILoan, BeaconImplementation {
_fees
);

LoanLib.completePayment(
liquidityAsset,
_pool,
_fees.interestPayment + _fees.latePaymentFee
);
LoanLib.completePayment(liquidityAsset, _pool, _fees.interestPayment);
paymentsRemaining -= 1;
paymentDueDate += settings.paymentPeriod * 1 days;
return payment;
Expand Down Expand Up @@ -499,9 +495,7 @@ contract Loan is ILoan, BeaconImplementation {
LoanLib.completePayment(
liquidityAsset,
_pool,
outstandingPrincipal.add(_fees.interestPayment).add(
_fees.latePaymentFee
)
outstandingPrincipal.add(_fees.interestPayment)
);
IPool(_pool).onLoanPrincipalReturned(outstandingPrincipal);

Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/LoanLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ library LoanLib {
IERC20(asset).safeTransferFrom(
msg.sender,
firstLossVault,
fees.firstLossFee
fees.firstLossFee + fees.latePaymentFee
);
}

Expand Down
64 changes: 61 additions & 3 deletions test/Loan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ describe("Loan", () => {
await poolController.connect(poolAdmin).fundLoan(loan.address);
await loan.connect(borrower).drawdown(await loan.principal());

// Advance time to drop dead timestamp
// Advance time to payment due date
const foo = await loan.paymentDueDate();
await time.increaseTo(foo.add(100));

Expand All @@ -1306,13 +1306,71 @@ describe("Loan", () => {
const tx = loan.connect(borrower).completeNextPayment();
await expect(tx).to.not.be.reverted;
await expect(tx).to.changeTokenBalance(liquidityAsset, borrower, -3083);
await expect(tx).to.changeTokenBalance(liquidityAsset, pool, 1979 + 1000);
await expect(tx).to.changeTokenBalance(liquidityAsset, firstLoss, 104);
await expect(tx).to.changeTokenBalance(liquidityAsset, pool, 1979);
await expect(tx).to.changeTokenBalance(
liquidityAsset,
firstLoss,
104 + 1000
);
expect(await loan.paymentsRemaining()).to.equal(5);
const newDueDate = await loan.paymentDueDate();
expect(newDueDate).to.equal(dueDate.add(THIRTY_DAYS));
});

it("paying off the entire loan, late, incurs one late fee", async () => {
const {
borrower,
collateralAsset,
liquidityAsset,
loan,
pool,
poolController,
poolAdmin
} = await loadFixture(deployFixtureWithLateFees);

// Setup
await collateralAsset.connect(borrower).approve(loan.address, 100);
await loan
.connect(borrower)
.postFungibleCollateral(collateralAsset.address, 100);
await poolController.connect(poolAdmin).fundLoan(loan.address);
await loan.connect(borrower).drawdown(await loan.principal());

// Mint additional tokens to cover the interest payments + late fee
const interestWithLateFees = 12498 + 1000;
await liquidityAsset.mint(borrower.address, interestWithLateFees);

// Advance past payment due date
const dueDate = await loan.paymentDueDate();
await time.increaseTo(dueDate.add(100));

// Make payment
await liquidityAsset
.connect(borrower)
.approve(loan.address, interestWithLateFees + 500_000);
const tx = loan.connect(borrower).completeFullPayment();
await expect(tx).to.not.be.reverted;
await expect(tx).to.changeTokenBalance(
liquidityAsset,
borrower,
-12498 - 500_000 - 1000 // interest + principal + late fee
);
await expect(tx).to.changeTokenBalance(
liquidityAsset,
pool,
12498 + 500_000 - 624 // interest + principal - protocol-wide FL fee of 5%
);
const firstLoss = await pool.firstLossVault();
await expect(tx).to.changeTokenBalance(
liquidityAsset,
firstLoss,
624 + 1000 // protocol-wise FL fee + late fee
);

expect(await loan.paymentsRemaining()).to.equal(0);
expect(await loan.state()).to.equal(5);
});

it("can payoff the entire loan at once", async () => {
const {
borrower,
Expand Down