update lending contract
This commit is contained in:
@@ -31,7 +31,6 @@ interface ILending {
|
||||
function withdraw(uint256 amount) external;
|
||||
function supplyCollateral(address asset, uint256 amount) external;
|
||||
function withdrawCollateral(address asset, uint256 amount) external;
|
||||
function borrow(uint256 amount) external;
|
||||
function absorb(address borrower) external;
|
||||
function absorbMultiple(address absorber, address[] calldata accounts) external;
|
||||
function buyCollateral(address asset, uint256 minAmount, uint256 baseAmount, address recipient) external;
|
||||
|
||||
@@ -68,8 +68,7 @@ contract Configurator is
|
||||
|
||||
// 防止修改不可变参数
|
||||
if (oldConfiguration.baseToken != address(0) &&
|
||||
(oldConfiguration.baseToken != newConfiguration.baseToken ||
|
||||
oldConfiguration.trackingIndexScale != newConfiguration.trackingIndexScale))
|
||||
oldConfiguration.baseToken != newConfiguration.baseToken)
|
||||
revert ConfigurationAlreadyExists();
|
||||
|
||||
// 删除旧的资产配置
|
||||
@@ -87,7 +86,6 @@ contract Configurator is
|
||||
configuratorParams[lendingProxy].borrowPerYearInterestRateSlopeHigh = newConfiguration.borrowPerYearInterestRateSlopeHigh;
|
||||
configuratorParams[lendingProxy].borrowPerYearInterestRateBase = newConfiguration.borrowPerYearInterestRateBase;
|
||||
configuratorParams[lendingProxy].storeFrontPriceFactor = newConfiguration.storeFrontPriceFactor;
|
||||
configuratorParams[lendingProxy].trackingIndexScale = newConfiguration.trackingIndexScale;
|
||||
configuratorParams[lendingProxy].baseBorrowMin = newConfiguration.baseBorrowMin;
|
||||
configuratorParams[lendingProxy].targetReserves = newConfiguration.targetReserves;
|
||||
|
||||
|
||||
@@ -63,7 +63,6 @@ contract Lending is
|
||||
|
||||
// 设置其他参数
|
||||
storeFrontPriceFactor = config.storeFrontPriceFactor;
|
||||
trackingIndexScale = config.trackingIndexScale;
|
||||
baseBorrowMin = config.baseBorrowMin;
|
||||
targetReserves = config.targetReserves;
|
||||
|
||||
@@ -268,49 +267,6 @@ contract Lending is
|
||||
emit WithdrawCollateral(msg.sender, msg.sender, asset, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice 借款
|
||||
* @dev baseBorrowMin 是用户借款的最小金额,如果用户借款后,余额小于 baseBorrowMin(由正数变为负数同理),则抛出 BorrowTooSmall 错误
|
||||
*/
|
||||
function borrow(uint256 amount) external override nonReentrant whenNotPaused {
|
||||
accrueInterest();
|
||||
|
||||
// 获取用户当前本金
|
||||
UserBasic memory user = userBasic[msg.sender];
|
||||
int104 oldPrincipal = user.principal;
|
||||
|
||||
// 计算当前实际余额(含利息)
|
||||
uint256 index = oldPrincipal >= 0 ? supplyIndex : borrowIndex;
|
||||
int256 oldBalance = LendingMath.principalToBalance(oldPrincipal, index);
|
||||
|
||||
// 计算新余额(减去借款额)
|
||||
int256 newBalance = oldBalance - int256(amount);
|
||||
|
||||
// 检查最小借款额
|
||||
if (newBalance < 0 && uint256(-newBalance) < baseBorrowMin) revert BorrowTooSmall();
|
||||
|
||||
// 转换为新本金(新状态可能从存款变为借款)
|
||||
uint256 newIndex = newBalance >= 0 ? supplyIndex : borrowIndex;
|
||||
int104 newPrincipal = LendingMath.balanceToPrincipal(newBalance, newIndex);
|
||||
|
||||
// 计算提取和借款金额
|
||||
(uint104 withdrawAmount, uint104 borrowAmount) = LendingMath.withdrawAndBorrowAmount(oldPrincipal, newPrincipal);
|
||||
|
||||
// 更新全局状态
|
||||
totalSupplyBase -= withdrawAmount;
|
||||
totalBorrowBase += borrowAmount;
|
||||
|
||||
// 更新用户本金,方便检查更新后的用户本金是否大于还是小于抵押品价值
|
||||
userBasic[msg.sender].principal = newPrincipal;
|
||||
|
||||
// 检查抵押品是否充足
|
||||
if (!_isSolvent(msg.sender)) revert InsufficientCollateral();
|
||||
|
||||
IERC20(baseToken).safeTransfer(msg.sender, amount);
|
||||
|
||||
emit Withdraw(msg.sender, msg.sender, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice 清算不良债务(内部实现)
|
||||
* @dev 当用户抵押品由于乘以liquidateCollateralFactor后,小于债务价值时,会进行清算,清算后,如果实际抵押品价值乘以liquidateCollateralFactor大于债务价值,则将差额部分作为用户本金(本金以baseToken显示),否则将差额部分作为坏账,由协议承担
|
||||
@@ -338,7 +294,7 @@ contract Lending is
|
||||
AssetConfig memory assetConfig = assetConfigs[asset];
|
||||
uint256 assetPrice = IYTLendingPriceFeed(lendingPriceSource).getPrice(asset);
|
||||
|
||||
// 计算抵押品价值(USD,8位精度)- 用于事件记录
|
||||
// 计算抵押品价值-用于事件记录
|
||||
uint256 assetScale = 10 ** assetConfig.decimals;
|
||||
uint256 collateralValueUSD = (collateralAmount * assetPrice) / assetScale;
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ contract LendingConfiguration {
|
||||
|
||||
// 其他核心参数
|
||||
uint64 storeFrontPriceFactor; // 清算价格折扣
|
||||
uint64 trackingIndexScale; // 追踪索引比例
|
||||
uint104 baseBorrowMin; // 最小借款额
|
||||
uint104 targetReserves; // 目标储备金
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ abstract contract LendingStorage is LendingConfiguration {
|
||||
|
||||
// 清算参数
|
||||
uint64 public storeFrontPriceFactor;
|
||||
uint64 public trackingIndexScale;
|
||||
uint104 public baseBorrowMin;
|
||||
uint104 public targetReserves;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user