update lending contract
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user