update lending contract

This commit is contained in:
2025-12-22 13:25:06 +08:00
parent 95cb192fed
commit 682552bfe6
18 changed files with 41 additions and 41 deletions

View File

@@ -46,9 +46,9 @@ contract Lending is
// 设置基础配置
baseToken = config.baseToken;
baseTokenPriceFeed = config.baseTokenPriceFeed;
// 常量:一年的秒数
uint256 SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
uint64 SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
// 设置利率参数
supplyKink = config.supplyKink;
@@ -113,7 +113,7 @@ contract Lending is
if (timeElapsed == 0) return;
// 计算实际的 totalSupply 和 totalBorrow含利息
// 注意totalSupplyBase 和 totalBorrowBase 都是正数本金
// 注意totalSupplyBase 和 totalBorrowBase 都是正数本金(正数=存款本金,负数=借款本金)
// supplyIndex 用于存款borrowIndex 用于借款
uint256 totalSupply = (uint256(totalSupplyBase) * supplyIndex) / 1e18;
uint256 totalBorrow = (uint256(totalBorrowBase) * borrowIndex) / 1e18;
@@ -137,7 +137,7 @@ contract Lending is
borrowPerSecondInterestRateBase
);
// 更新利息累计因子(使用每秒利率,计算更精确且 Gas 更低
// 更新利息累计因子(使用每秒利率)
supplyIndex = LendingMath.accrueInterest(supplyIndex, supplyRate, timeElapsed);
borrowIndex = LendingMath.accrueInterest(borrowIndex, borrowRate, timeElapsed);
@@ -167,7 +167,7 @@ contract Lending is
uint256 newIndex = newBalance >= 0 ? supplyIndex : borrowIndex;
int104 newPrincipal = LendingMath.balanceToPrincipal(newBalance, newIndex);
// 计算还款和存款金额
// 根据新旧本金,计算还款和存款金额
(uint104 repayAmount, uint104 supplyAmount) = LendingMath.repayAndSupplyAmount(oldPrincipal, newPrincipal);
// 更新全局状态
@@ -307,7 +307,7 @@ contract Lending is
/**
* @notice 清算不良债务(内部实现)
* @dev 当用户抵押品由于乘以liquidateCollateralFactor后小于债务价值时会进行清算清算后如果实际抵押品价值大于债务价值则将差额部分作为用户本金本金以baseToken显示否则将差额部分作为坏账由协议承担
* @dev 当用户抵押品由于乘以liquidateCollateralFactor后小于债务价值时会进行清算清算后如果实际抵押品价值乘以liquidateCollateralFactor大于债务价值则将差额部分作为用户本金本金以baseToken显示否则将差额部分作为坏账由协议承担
*/
function _absorbInternal(address absorber, address borrower) internal {
if (!isLiquidatable(borrower)) revert NotLiquidatable();
@@ -432,7 +432,7 @@ contract Lending is
uint256 totalBorrow = (uint256(totalBorrowBase) * borrowIndex) / 1e18;
int256 currentReserves = int256(balance) - int256(totalSupply) + int256(totalBorrow);
if (currentReserves >= 0 && uint256(currentReserves) >= targetReserves) {
revert InsufficientBalance(); // 储备金充足,无需出售
revert NotForSale(); // 储备金充足,无需出售
}
// 收取清算人支付的资金
@@ -619,6 +619,21 @@ contract Lending is
return perSecondRate * 31536000; // SECONDS_PER_YEAR
}
function getBorrowRate() external view override returns (uint64) {
uint256 totalSupply = (uint256(totalSupplyBase) * supplyIndex) / 1e18;
uint256 totalBorrow = (uint256(totalBorrowBase) * borrowIndex) / 1e18;
uint64 utilization = LendingMath.getUtilization(totalSupply, totalBorrow);
uint64 perSecondRate = LendingMath.getBorrowRate(
utilization,
borrowKink,
borrowPerSecondInterestRateSlopeLow,
borrowPerSecondInterestRateSlopeHigh,
borrowPerSecondInterestRateBase
);
// 转换为年化利率APY
return perSecondRate * 31536000; // SECONDS_PER_YEAR
}
/**
* @notice 提取协议储备金(仅 owner
*/
@@ -638,20 +653,5 @@ contract Lending is
emit WithdrawReserves(to, amount);
}
function getBorrowRate() external view override returns (uint64) {
uint256 totalSupply = (uint256(totalSupplyBase) * supplyIndex) / 1e18;
uint256 totalBorrow = (uint256(totalBorrowBase) * borrowIndex) / 1e18;
uint64 utilization = LendingMath.getUtilization(totalSupply, totalBorrow);
uint64 perSecondRate = LendingMath.getBorrowRate(
utilization,
borrowKink,
borrowPerSecondInterestRateSlopeLow,
borrowPerSecondInterestRateSlopeHigh,
borrowPerSecondInterestRateBase
);
// 转换为年化利率APY
return perSecondRate * 31536000; // SECONDS_PER_YEAR
}
}