LoanUtils.sol (Library for utilities and data structures)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
library LendingLibrary {
// Constants
uint256 constant BASE_PERCENTAGE = 100;
// Functions
/**
* @dev Calculates the origination fee.
* @param loanAmount The principal loan amount.
* @param feeRate The origination fee rate (percentage).
* @return The calculated origination fee.
*/
function calculateOriginationFee(uint256 loanAmount, uint256 feeRate) external pure returns (uint256) {
return (loanAmount * feeRate) / BASE_PERCENTAGE;
}
/**
* @dev Calculates the total repayment amount for a loan.
* @param loanAmount The principal loan amount.
* @param interestRate The annual interest rate (percentage).
* @param term The loan term in seconds.
* @return The total repayment amount.
*/
function calculateTotalRepaymentAmount(
uint256 loanAmount,
uint256 interestRate,
uint256 term
) external pure returns (uint256) {
return loanAmount + ((loanAmount * interestRate * term) / (BASE_PERCENTAGE * 365 * 86400));
}
/**
* @dev Calculates the utilization rate of a liquidity pool.
* @param borrowedFunds The amount of borrowed funds.
* @param availableLiquidity The available liquidity in the pool.
* @return The utilization rate as a percentage (scaled by 1e18).
*/
function calculateUtilizationRate(uint256 borrowedFunds, uint256 availableLiquidity) external pure returns (uint256) {
require(borrowedFunds + availableLiquidity > 0, "No liquidity in pool");
return (borrowedFunds * 1e18) / (borrowedFunds + availableLiquidity);
}
/**
* @dev Calculates the CLR (collateral-to-loan ratio).
* @param collateralAmount The collateral amount.
* @param collateralRateUSD The collateral value in USD.
* @param loanAmount The loan amount.
* @param loanRateUSD The loan value in USD.
* @return The CLR as a percentage (scaled by 1e18).
*/
function calculateCLR(
uint256 collateralAmount,
uint256 collateralRateUSD,
uint256 loanAmount,
uint256 loanRateUSD
) external pure returns (uint256) {
require(loanAmount > 0 && loanRateUSD > 0, "Invalid loan amount or rate");
return (collateralAmount * collateralRateUSD * BASE_PERCENTAGE) / (loanAmount * loanRateUSD);
}
}
Last updated
