Skip to content

Instantly share code, notes, and snippets.

@lucas-manuel
Created November 10, 2021 20:56
Show Gist options
  • Save lucas-manuel/beae03ec50d08e4dfa16e7c349f1cfde to your computer and use it in GitHub Desktop.
Save lucas-manuel/beae03ec50d08e4dfa16e7c349f1cfde to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.7;
import { ERC20Helper } from "../modules/erc20-helper/src/ERC20Helper.sol";
import { MapleProxied } from "../modules/maple-proxy-factory/contracts/MapleProxied.sol";
import { IMapleLoan } from "./interfaces/IMapleLoan.sol";
contract MapleBorrowerInternals is MapleProxied {
address internal _owner;
address internal _pendingOwner;
/*************************/
/*** General Functions ***/
/*************************/
function _initialize(address owner_) internal {
_owner = owner_;
}
/*************************/
/*** Ownable Functions ***/
/*************************/
modifier onlyOwner() {
require(msg.sender == _owner, "MBI:NOT_OWNER");
_;
}
/************************/
/*** Borrow Functions ***/
/************************/
function _drawdownFunds(address loan_, uint256 amount_, address destination_) internal {
_postCollateralForDrawdown(loan_, amount_);
IMapleLoan(loan_).drawdownFunds(amount_, destination_);
}
function _makePayment(address loan_) internal {
( uint256 principal, uint256 interest ) = IMapleLoan(loan_).getNextPaymentBreakdown();
uint256 total = principal + interest;
uint256 drawableFunds = IMapleLoan(loan_).drawableFunds();
require(
total <= drawableFunds || ERC20Helper.transferFrom(IMapleLoan(loan_).fundsAsset(), msg.sender, loan_, total - drawableFunds),
"MBI:MP:TRANSFER_FAILED"
);
IMapleLoan(loan_).makePayment(uint256(0));
}
function _makePayments(address loan_, uint256 numberOfPayments_) internal {
while (numberOfPayments_-- > 0) {
_makePayment(loan_);
}
}
function _makePaymentsWithCutoff(address loan_, uint256 cutoffDate_) internal returns (bool hasPaymentsWithinCutoff_) {
uint256 nextPaymentDueDate = IMapleLoan(loan_).nextPaymentDueDate();
if (nextPaymentDueDate > cutoffDate_) return false;
uint256 paymentToMake = 1 + ((cutoffDate_ - nextPaymentDueDate) / IMapleLoan(loan_).paymentInterval());
uint256 paymentsRemaining = IMapleLoan(loan_).paymentsRemaining();
_makePayments(loan_, paymentToMake < paymentsRemaining ? paymentToMake : paymentsRemaining);
return true;
}
function _postCollateral(address loan_, uint256 amount_) internal {
require(ERC20Helper.transferFrom(IMapleLoan(loan_).collateralAsset(), msg.sender, loan_, amount_), "MBI:PC:TRANSFER_FAILED");
IMapleLoan(loan_).postCollateral(uint256(0));
}
function _postCollateralForDrawdown(address loan_, uint256 drawdownAmount_) internal returns (bool additionalCollateralNecessary_) {
uint256 collateral = IMapleLoan(loan_).getAdditionalCollateralRequiredFor(drawdownAmount_);
if (collateral == uint256(0)) return false;
_postCollateral(loan_, collateral);
return true;
}
function _proposeNewTerms(address loan_, address refinancer_, bytes[] calldata calls_) internal {
IMapleLoan(loan_).proposeNewTerms(refinancer_, calls_);
}
function _removeExcessCollateral(address loan_, address destination_) internal returns (bool hasExcessCollateral_) {
uint256 collateral = IMapleLoan(loan_).excessCollateral();
if (collateral == uint256(0)) return false;
_removeCollateral(loan_, collateral, destination_);
return true;
}
function _removeCollateral(address loan_, uint256 amount_, address destination_) internal {
IMapleLoan(loan_).removeCollateral(amount_, destination_);
}
function _returnFunds(address loan_, uint256 amount_) internal {
require(ERC20Helper.transferFrom(IMapleLoan(loan_).fundsAsset(), msg.sender, loan_, amount_), "MBI:RF:TRANSFER_FAILED");
IMapleLoan(loan_).returnFunds(uint256(0));
}
function _returnFundsAndRemoveExcessCollateral(address loan_, uint256 amount_, address destination_) internal returns (bool hasExcessCollateral_) {
_returnFunds(loan_, amount_);
return _removeExcessCollateral(loan_, destination_);
}
function _setBorrower(address loan_, address borrower_) internal {
IMapleLoan(loan_).setBorrower(borrower_);
}
function _upgradeLoan(address loans_, uint256 toVersions_, bytes calldata arguments_) internal {
IMapleLoan(loans_).upgrade(toVersions_, arguments_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment