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

Complete full payment #119

Merged
merged 2 commits into from
Dec 1, 2022
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
52 changes: 25 additions & 27 deletions contracts/Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,15 @@ contract Loan is ILoan {
_serviceConfiguration.firstLossFeeBps(),
IPool(_pool).poolFeePercentOfInterest(),
block.timestamp,
paymentDueDate
paymentDueDate,
RAY
);

LoanLib.payFees(
liquidityAsset,
IPool(_pool).firstLossVault(),
_fees.firstLossFee,
IPool(_pool).feeVault(),
_fees.serviceFee,
_fees.originationFee
_fees
);

LoanLib.completePayment(
Expand Down Expand Up @@ -394,7 +393,8 @@ contract Loan is ILoan {
_serviceConfiguration.firstLossFeeBps(),
IPool(_pool).poolFeePercentOfInterest(),
block.timestamp,
paymentDueDate
paymentDueDate,
RAY
);
}

Expand All @@ -407,42 +407,40 @@ contract Loan is ILoan {
atState(ILoanLifeCycleState.Active)
returns (uint256)
{
uint256 amount = payment.mul(paymentsRemaining);
uint256 scalingValue = RAY;

// We will pro-rate open term loans for their last month of service
// If payment is overdue, we use default value of RAY. scalingValue is in RAYS.
if (
settings.loanType == ILoanType.Open &&
paymentDueDate > block.timestamp
) {
// Calculate the scaling value
// RAY - ((paymentDueDate - blocktimestamp) * RAY / paymentPeriod (seconds))
scalingValue = RAY.sub(
(paymentDueDate - block.timestamp).mul(RAY).div(
settings.paymentPeriod * 1 days
)
);
// Adjust payment accordingly
amount = (payment * scalingValue) / RAY;
if (settings.loanType == ILoanType.Open) {
// If an open term loan payment is not overdue, we will prorate the
// payment
if (paymentDueDate > block.timestamp) {
// Calculate the scaling value
// RAY - ((paymentDueDate - blocktimestamp) * RAY / paymentPeriod (seconds))
scalingValue = RAY.sub(
(paymentDueDate - block.timestamp).mul(RAY).div(
settings.paymentPeriod * 1 days
)
);
}
} else {
// Fixed term loans must pay all outstanding interest payments and fees.
scalingValue = RAY.mul(paymentsRemaining);
}

ILoanFees memory _fees = LoanLib.previewFees(
settings,
amount,
payment,
_serviceConfiguration.firstLossFeeBps(),
IPool(_pool).poolFeePercentOfInterest(),
block.timestamp,
paymentDueDate
paymentDueDate,
scalingValue
);

LoanLib.payFees(
liquidityAsset,
IPool(_pool).firstLossVault(),
_fees.firstLossFee,
IPool(_pool).feeVault(),
_fees.serviceFee,
_fees.originationFee.mul(scalingValue).div(RAY)
_fees
);

LoanLib.completePayment(
Expand All @@ -458,7 +456,7 @@ contract Loan is ILoan {
_state = ILoanLifeCycleState.Matured;

IPool(_pool).notifyLoanStateTransitioned();
return amount;
return payment;
}

/**
Expand Down
49 changes: 22 additions & 27 deletions contracts/libraries/LoanLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,16 @@ library LoanLib {
return RAY.mul(payment).mul(serviceFeeBps).div(100_00).div(RAY);
}

function previewOriginationFee(ILoanSettings calldata settings)
public
pure
returns (uint256)
{
uint256 numOfPayments = settings.duration.div(settings.paymentPeriod);

function previewOriginationFee(
ILoanSettings calldata settings,
uint256 scalingValue
) public pure returns (uint256) {
return
settings
.principal
.mul(settings.originationBps)
.mul(settings.duration.mul(RAY).div(360))
.div(numOfPayments)
.mul(settings.duration.mul(scalingValue).div(360))
.div(settings.duration.div(settings.paymentPeriod))
.div(RAY)
.div(10000);
}
Expand All @@ -336,13 +333,16 @@ library LoanLib {
uint256 firstLoss,
uint256 poolFeePercentOfInterest,
uint256 blockTimestamp,
uint256 paymentDueDate
uint256 paymentDueDate,
uint256 scalingValue
) public pure returns (ILoanFees memory) {
// If there is a scaling value
payment = payment.mul(scalingValue).div(RAY);
ILoanFees memory fees;
fees.payment = payment;
fees.firstLossFee = previewFirstLossFee(payment, firstLoss);
fees.serviceFee = previewServiceFee(payment, poolFeePercentOfInterest);
fees.originationFee = previewOriginationFee(settings);
fees.originationFee = previewOriginationFee(settings, scalingValue);
fees.latePaymentFee = previewLatePaymentFee(
settings,
blockTimestamp,
Expand All @@ -356,30 +356,25 @@ library LoanLib {
function payFees(
address asset,
address firstLossVault,
uint256 firstLoss,
address poolAdmin,
uint256 poolFeePercentOfInterest,
uint256 originationFee
address feeVault,
ILoanFees calldata fees
) public {
if (firstLoss > 0) {
if (fees.firstLossFee > 0) {
IERC20(asset).safeTransferFrom(
msg.sender,
firstLossVault,
firstLoss
);
}
if (poolFeePercentOfInterest > 0) {
IERC20(asset).safeTransferFrom(
msg.sender,
poolAdmin,
poolFeePercentOfInterest
fees.firstLossFee
);
}
if (originationFee > 0) {

// The FeeVault holds the balance of fees intended for the PoolAdmin.
// This include both the service fee and origiantion fees.
uint256 feeVaultAmount = fees.serviceFee + fees.originationFee;
if (feeVaultAmount > 0) {
IERC20(asset).safeTransferFrom(
msg.sender,
poolAdmin,
originationFee
feeVault,
feeVaultAmount
);
}
}
Expand Down
66 changes: 60 additions & 6 deletions test/Loan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,55 @@ describe("Loan", () => {
const newDueDate = await loan.paymentDueDate();
expect(newDueDate).to.equal(dueDate.add(THIRTY_DAYS));
});

it("can collect origination fees from the full payment", async () => {
const {
borrower,
collateralAsset,
liquidityAsset,
loan,
pool,
poolController,
poolAdmin
} = await loadFixture(deployFixtureOriginationFees);

// 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());

// 500,000 token loan with 180 day term
// Loan has 100bps origination fee
await liquidityAsset.mint(borrower.address, 2_500);
// Loan has 500bps interest
await liquidityAsset.mint(borrower.address, 12_498);

// Make payment
const firstLoss = await poolController.firstLossVault();
const feeVault = await pool.feeVault();
await liquidityAsset
.connect(borrower)
.approve(loan.address, 12498 + 500_000 + 2_500);
const tx = loan.connect(borrower).completeFullPayment();
await expect(tx).to.not.be.reverted;
await expect(tx).to.changeTokenBalance(
liquidityAsset,
borrower,
-12498 - 500_000 - 2_500
);
await expect(tx).to.changeTokenBalance(
liquidityAsset,
pool,
500_000 + 12_498 - 624
);
await expect(tx).to.changeTokenBalance(liquidityAsset, feeVault, 2_500);
await expect(tx).to.changeTokenBalance(liquidityAsset, firstLoss, 624);
expect(await loan.paymentsRemaining()).to.equal(0);
expect(await loan.state()).to.equal(5);
});
});

describe("callbacks", () => {
Expand Down Expand Up @@ -1281,7 +1330,7 @@ describe("Loan", () => {
expect(await liquidityAsset.balanceOf(fundingVault)).to.equal(0);

// Mint additional tokens to cover the interest payments
await liquidityAsset.mint(borrower.address, 12498);
await liquidityAsset.mint(borrower.address, 12498 + 50_000);

// Repay some of the principal
const prepaidPrincipal = 1_000;
Expand All @@ -1306,8 +1355,8 @@ describe("Loan", () => {
// Relative to a full month payments, the fees will be halved
await time.increase(THIRTY_DAYS / 2);
const firstLossFee = 52;
const poolAdminFee = 208;
const interestPayment = 1041;
const serviceFee = 208;
const interestPayment = 989;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is different because first loss fee is now separate from interest fees.

const principal = 500_000;
const originationFee = 208;

Expand All @@ -1316,12 +1365,17 @@ describe("Loan", () => {
await expect(tx).to.changeTokenBalance(
liquidityAsset,
borrower,
0 - interestPayment - principal - originationFee + prepaidPrincipal
0 -
interestPayment -
principal -
originationFee -
firstLossFee +
prepaidPrincipal
);
await expect(tx).to.changeTokenBalance(
liquidityAsset,
pool,
interestPayment + principal - prepaidPrincipal - firstLossFee
interestPayment + principal - prepaidPrincipal
);

const firstLoss = await pool.firstLossVault();
Expand All @@ -1333,7 +1387,7 @@ describe("Loan", () => {
await expect(tx).to.changeTokenBalance(
liquidityAsset,
await pool.feeVault(),
poolAdminFee
serviceFee
);

expect(await loan.paymentsRemaining()).to.equal(0);
Expand Down