Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ethereumdegen committed May 9, 2023
2 parents 0cfdd40 + be429f2 commit 59f52aa
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
99 changes: 99 additions & 0 deletions packages/contracts/contracts/RepaymentTool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

// Interfaces
import "./interfaces/ICollateralManager.sol";
import "./interfaces/ITellerV2.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";

/*
Provides functions to allow for loan repayment and collateral withdraw in the same transaction
*/

contract RepaymentTool is Initializable {

ITellerV2 public tellerV2;
ICollateralManager public collateralManager;


/**
* @notice Initializes the proxy.
*/
function initialize(address _tellerV2, address _collateralManager) external initializer {
tellerV2 = ITellerV2(_tellerV2);
collateralManager = ICollateralManager(_collateralManager);
}

function repayLoan(uint256 _bidId, uint256 _amount, bool _claimCollateral)
external
{

address lendingToken = tellerV2.getLoanLendingToken(_bidId);

IERC20(lendingToken).safeTransferFrom(
_msgSenderForMarket(bid.marketplaceId),
address(this),
_amount
);

IERC20(lendingToken).approve(address(tellerV2),_amount);

tellerV2.repayLoan(_bidId, _amount);

if(_claimCollateral){
//issue: we may want to allow only the lender to call this but then this breaks
collateralManager.withdraw(_bidId);
}
}

function repayLoanFull(uint256 _bidId, bool _claimCollateral)
external
{

address lendingToken = tellerV2.getLoanLendingToken(_bidId);


(uint256 owedPrincipal, , uint256 interest) = V2Calculations
.calculateAmountOwed(
bids[_bidId],
block.timestamp,
bidPaymentCycleType[_bidId]
);

uint256 paymentAmount = (owedPrincipal + interest);

IERC20(lendingToken).safeTransferFrom(
_msgSenderForMarket(bid.marketplaceId),
address(this),
paymentAmount
);

IERC20(lendingToken).approve(address(tellerV2),paymentAmount);

tellerV2.repayLoanFull(_bidId);

if(_claimCollateral){

//issue: we may want to allow only the lender to call this but then this breaks
collateralManager.withdraw(_bidId);
}
}



function liquidateLoanFull(uint256 _bidId, bool _claimCollateral)
external
{
tellerV2.liquidateLoanFull(_bidId);

address liquidator = msg.sender; //_msgSenderForMarket(bid.marketplaceId);

if(_claimCollateral){
collateralManager.liquidateCollateral(_bidId, liquidator);
}
}


}
8 changes: 4 additions & 4 deletions packages/contracts/contracts/TellerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ contract TellerV2 is
_repayLoan(
_bidId,
Payment({ principal: duePrincipal, interest: interest }),

owedPrincipal + interest

);
}

Expand Down Expand Up @@ -646,7 +648,7 @@ contract TellerV2 is

_repayLoan(
_bidId,
Payment({ principal: _amount - interest, interest: interest }),
Payment({ principal: _amount - interest, interest: interest }),
owedPrincipal + interest
);
}
Expand Down Expand Up @@ -687,7 +689,7 @@ contract TellerV2 is
_repayLoan(
_bidId,
Payment({ principal: owedPrincipal, interest: interest }),
owedPrincipal + interest
owedPrincipal + interest
);

bid.state = BidState.LIQUIDATED;
Expand Down Expand Up @@ -727,8 +729,6 @@ contract TellerV2 is
// Remove borrower's active bid
_borrowerBidsActive[bid.borrower].remove(_bidId);



emit LoanRepaid(_bidId);
} else {
emit LoanRepayment(_bidId);
Expand Down

0 comments on commit 59f52aa

Please sign in to comment.