update lending contract
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,14 +10,14 @@ contract LendingConfiguration {
|
||||
address asset; // 资产地址
|
||||
address priceFeed; // 价格预言机地址
|
||||
uint8 decimals; // 小数位数
|
||||
uint64 borrowCollateralFactor; // 借款抵押率 (例: 0.8e18 = 80%)
|
||||
uint64 liquidateCollateralFactor; // 清算抵押率 (例: 0.85e18 = 85%)
|
||||
uint64 liquidationFactor; // 清算激励 (例: 1.05e18 = 5%折扣)
|
||||
uint64 borrowCollateralFactor; // 借款抵押率
|
||||
uint64 liquidateCollateralFactor; // 清算抵押率
|
||||
uint64 liquidationFactor; // 清算折扣
|
||||
uint128 supplyCap; // 供应上限
|
||||
}
|
||||
|
||||
struct Configuration {
|
||||
address baseToken; // 基础资产(借出的资产,如 USDC)
|
||||
address baseToken; // 基础资产
|
||||
address baseTokenPriceFeed; // 基础资产价格预言机
|
||||
|
||||
// 利率模型参数
|
||||
|
||||
@@ -35,6 +35,7 @@ interface ILending {
|
||||
error InvalidBorrowCollateralFactor();
|
||||
error InvalidLiquidateCollateralFactor();
|
||||
error InsufficientReserves();
|
||||
error NotForSale();
|
||||
|
||||
// ========== Core Functions ==========
|
||||
|
||||
|
||||
Reference in New Issue
Block a user