-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from La-DAO/feat/helper-for-ui
Feat/helper for UI
- Loading branch information
Showing
6 changed files
with
249 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {Application, ApplicationStatus, CreditTalentCenter} from "./CreditTalentCenter.sol"; | ||
import {IMorpho, Id, MarketParams, Market, Position} from "@morpho/contracts/interfaces/IMorpho.sol"; | ||
import {MarketParamsLib} from "@morpho/contracts/libraries/MarketParamsLib.sol"; | ||
import {SharesMathLib} from "@morpho/contracts/libraries/SharesMathLib.sol"; | ||
import {IIrm} from "@morpho/contracts/interfaces/IIrm.sol"; | ||
|
||
contract UICreditTalentHelper { | ||
using MarketParamsLib for MarketParams; | ||
using SharesMathLib for uint256; | ||
|
||
// Number of seconds in a year (365 days) | ||
uint256 private constant SECONDS_PER_YEAR = 365 days; | ||
|
||
// Scaling factors | ||
uint256 private constant SCALING_FACTOR = 1e18; | ||
uint256 private constant RAY = 1e27; | ||
|
||
/** | ||
* @notice Get user loan information | ||
* @param user from whom to get loan information | ||
* @return creditLine amount approved by underwriter to user | ||
* @return debtBalance borrowed + accrued interest amount by user currently | ||
* @return interestRatePerSecond scaled 1e18 (see https://docs.morpho.org/morpho/contracts/irm/#borrow-apy to convert to APY) | ||
*/ | ||
function getUserLoanInfo(address creditCenter, address user) | ||
external | ||
view | ||
returns (uint256 creditLine, uint256 debtBalance, uint256 interestRatePerSecond) | ||
{ | ||
CreditTalentCenter creditTalent = CreditTalentCenter(creditCenter); | ||
Application memory application; | ||
(,,, application.underwriter, application.status, application.irm) = creditTalent.applicationInfo(user); | ||
if (application.underwriter == address(0) || application.status != ApplicationStatus.Approved) { | ||
return (0, 0, 0); | ||
} | ||
creditLine = creditTalent.creditShares(application.underwriter); | ||
address irm = application.irm == address(0) ? creditTalent.adpativeIrm() : application.irm; | ||
|
||
IMorpho morpho = creditTalent.morpho(); | ||
|
||
MarketParams memory marketParams = MarketParams( | ||
creditTalent.underwritingAsset(), | ||
creditTalent.creditPoints(), | ||
address(creditTalent), | ||
irm, | ||
creditTalent.DEFAULT_LLTV() | ||
); | ||
Position memory morphoPosition = morpho.position(marketParams.id(), user); | ||
Market memory market = morpho.market(marketParams.id()); | ||
debtBalance = | ||
uint256(morphoPosition.borrowShares).toAssetsUp(market.totalBorrowAssets, market.totalBorrowShares); | ||
interestRatePerSecond = IIrm(irm).borrowRateView(marketParams, market); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {IMorpho} from "@morpho/contracts/interfaces/IMorpho.sol"; | ||
|
||
enum ApplicationStatus { | ||
None, | ||
Pending, | ||
Approved, | ||
Rejected | ||
} | ||
|
||
struct Underwriter { | ||
address underwriter; | ||
uint256 approvalAmount; | ||
address[] approvedApplicants; | ||
} | ||
|
||
struct Application { | ||
uint256 id; | ||
address applicant; | ||
bytes32 dataHash; | ||
address underwriter; | ||
ApplicationStatus status; | ||
address irm; | ||
} | ||
|
||
interface ICreditTalent { | ||
/// Events | ||
event ApplicationCreated(uint256 id, address indexed applicant, bytes32 dataHash); | ||
event ApplicationApproved( | ||
uint256 id, address indexed applicant, address indexed underwriter, uint256 amount, uint256 interestRate | ||
); | ||
event ApplicationRejected(uint256 id, address indexed applicant, address indexed underwriter, string reason); | ||
event UnderwriterSet(address indexed account, uint256 approvalPower); | ||
event FixedRateIrmSet(uint256 indexed interestRate, address irm); | ||
|
||
/// Custom errors | ||
error CrediTalentCenter_zeroAddress(); | ||
error CrediTalentCenter_applicationAlreadyExists(); | ||
error CrediTalentCenter_fixedRateIrmAlreadyExists(); | ||
error CreditTalentCenter_invalidApplicationId(); | ||
error CreditTalentCenter_applicationNotPending(); | ||
error CreditTalentCenter_insufficientUnderwritingPower(); | ||
error CreditTalentCenter_invalidInterestRate(); | ||
|
||
/// View | ||
function underwritingAsset() external view returns (address); | ||
function creditPoints() external view returns (address); | ||
function DEFAULT_LLTV() external view returns (uint256); | ||
function adpativeIrm() external view returns (address); | ||
function morpho() external view returns (IMorpho); | ||
function applicationInfo(address user) | ||
external | ||
view | ||
returns ( | ||
uint256 id, | ||
address applicant, | ||
bytes32 dataHash, | ||
address underwriter, | ||
ApplicationStatus status, | ||
address irm | ||
); | ||
function creditShares(address underwriter) external view returns (uint256); | ||
} |
Oops, something went wrong.