update ytLending
This commit is contained in:
@@ -50,8 +50,7 @@ contract Lending is
|
||||
// 常量:一年的秒数
|
||||
uint256 SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
|
||||
|
||||
// 设置利率参数(将年化利率转换为每秒利率,只计算一次)
|
||||
// 这样可以大幅降低每次计提利息的 Gas 成本并提高精度
|
||||
// 设置利率参数
|
||||
supplyKink = config.supplyKink;
|
||||
supplyPerSecondInterestRateSlopeLow = uint64(config.supplyPerYearInterestRateSlopeLow / SECONDS_PER_YEAR);
|
||||
supplyPerSecondInterestRateSlopeHigh = uint64(config.supplyPerYearInterestRateSlopeHigh / SECONDS_PER_YEAR);
|
||||
@@ -78,9 +77,9 @@ contract Lending is
|
||||
AssetConfig memory assetConfig = config.assetConfigs[i];
|
||||
|
||||
// 验证参数合法性(必须 < 1)
|
||||
require(assetConfig.liquidationFactor < 1e18, "Invalid liquidationFactor");
|
||||
require(assetConfig.borrowCollateralFactor < 1e18, "Invalid borrowCF");
|
||||
require(assetConfig.liquidateCollateralFactor < 1e18, "Invalid liquidateCF");
|
||||
if(assetConfig.liquidationFactor >= 1e18) revert InvalidLiquidationFactor();
|
||||
if(assetConfig.borrowCollateralFactor >= 1e18) revert InvalidBorrowCollateralFactor();
|
||||
if(assetConfig.liquidateCollateralFactor >= 1e18) revert InvalidLiquidateCollateralFactor();
|
||||
|
||||
assetConfigs[assetConfig.asset] = assetConfig;
|
||||
assetList.push(assetConfig.asset);
|
||||
@@ -263,6 +262,7 @@ contract Lending is
|
||||
|
||||
/**
|
||||
* @notice 借款
|
||||
* @dev baseBorrowMin 是用户借款的最小金额,如果用户借款后,余额小于 baseBorrowMin(由正数变为负数同理),则抛出 BorrowTooSmall 错误
|
||||
*/
|
||||
function borrow(uint256 amount) external override nonReentrant whenNotPaused {
|
||||
accrueInterest();
|
||||
@@ -294,10 +294,10 @@ contract Lending is
|
||||
totalSupplyBase -= withdrawAmount;
|
||||
totalBorrowBase += borrowAmount;
|
||||
|
||||
// 更新用户本金
|
||||
// 更新用户本金,方便检查更新后的用户本金是否大于还是小于抵押品价值
|
||||
userBasic[msg.sender].principal = newPrincipal;
|
||||
|
||||
// 检查抵押品是否充足
|
||||
// 检查抵押品是否充足
|
||||
if (!_isSolvent(msg.sender)) revert InsufficientCollateral();
|
||||
|
||||
IERC20(baseToken).safeTransfer(msg.sender, amount);
|
||||
@@ -307,6 +307,7 @@ contract Lending is
|
||||
|
||||
/**
|
||||
* @notice 清算不良债务(内部实现)
|
||||
* @dev 当用户抵押品由于乘以liquidateCollateralFactor后,小于债务价值时,会进行清算,清算后,如果实际抵押品价值大于债务价值,则将差额部分作为用户本金(本金以baseToken显示),否则将差额部分作为坏账,由协议承担
|
||||
*/
|
||||
function _absorbInternal(address absorber, address borrower) internal {
|
||||
if (!isLiquidatable(borrower)) revert NotLiquidatable();
|
||||
@@ -438,7 +439,6 @@ contract Lending is
|
||||
IERC20(asset).safeTransfer(recipient, collateralAmount);
|
||||
|
||||
// 注意:收入会自动体现在 getReserves() 中,因为 balance 增加了
|
||||
|
||||
emit BuyCollateral(msg.sender, asset, baseAmount, collateralAmount);
|
||||
}
|
||||
|
||||
@@ -451,7 +451,7 @@ contract Lending is
|
||||
uint256 assetPrice = IPriceFeed(assetConfig.priceFeed).getPrice();
|
||||
uint256 basePrice = IPriceFeed(baseTokenPriceFeed).getPrice();
|
||||
|
||||
// 计算折扣率 - 使用 Compound V3 的 mulFactor 方式
|
||||
// 计算折扣率
|
||||
// discountFactor = storeFrontPriceFactor * (FACTOR_SCALE - liquidationFactor) / FACTOR_SCALE
|
||||
uint256 FACTOR_SCALE = 1e18;
|
||||
uint256 discountFactor = (storeFrontPriceFactor * (FACTOR_SCALE - assetConfig.liquidationFactor)) / FACTOR_SCALE;
|
||||
|
||||
@@ -32,6 +32,8 @@ interface ILending {
|
||||
error NotLiquidatable();
|
||||
error SupplyCapExceeded();
|
||||
error InvalidLiquidationFactor();
|
||||
error InvalidBorrowCollateralFactor();
|
||||
error InvalidLiquidateCollateralFactor();
|
||||
error InsufficientReserves();
|
||||
|
||||
// ========== Core Functions ==========
|
||||
|
||||
Reference in New Issue
Block a user