fix contract
This commit is contained in:
@@ -543,8 +543,9 @@ contract Lending is
|
||||
|
||||
function getBalance(address account) external view override returns (int256) {
|
||||
int104 principal = userBasic[account].principal;
|
||||
// 使用 supplyIndex 计算实际余额(含利息)
|
||||
return LendingMath.principalToBalance(principal, supplyIndex);
|
||||
// 根据余额正负使用对应的索引:正余额用supplyIndex,负余额用borrowIndex
|
||||
uint256 index = principal >= 0 ? supplyIndex : borrowIndex;
|
||||
return LendingMath.principalToBalance(principal, index);
|
||||
}
|
||||
|
||||
function supplyBalanceOf(address account) external view override returns (uint256) {
|
||||
|
||||
@@ -9,10 +9,14 @@ import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interf
|
||||
contract LendingPriceFeed is OwnableUpgradeable, UUPSUpgradeable {
|
||||
address public usdcAddress;
|
||||
AggregatorV3Interface internal usdcPriceFeed;
|
||||
|
||||
/// @notice 价格过期阈值(秒)
|
||||
uint256 public priceStalenesThreshold;
|
||||
|
||||
error InvalidUsdcAddress();
|
||||
error InvalidUsdcPriceFeedAddress();
|
||||
error InvalidChainlinkPrice();
|
||||
error StalePrice();
|
||||
|
||||
/// @custom:oz-upgrades-unsafe-allow constructor
|
||||
constructor() {
|
||||
@@ -26,6 +30,7 @@ contract LendingPriceFeed is OwnableUpgradeable, UUPSUpgradeable {
|
||||
if (_usdcPriceFeed == address(0)) revert InvalidUsdcPriceFeedAddress();
|
||||
usdcAddress = _usdcAddress;
|
||||
usdcPriceFeed = AggregatorV3Interface(_usdcPriceFeed);
|
||||
priceStalenesThreshold = 3600; // 默认1小时
|
||||
}
|
||||
|
||||
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
|
||||
@@ -35,6 +40,15 @@ contract LendingPriceFeed is OwnableUpgradeable, UUPSUpgradeable {
|
||||
usdcAddress = _usdcAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice 设置价格过期阈值
|
||||
* @param _threshold 阈值(秒),例如:3600 = 1小时,86400 = 24小时
|
||||
*/
|
||||
function setPriceStalenessThreshold(uint256 _threshold) external onlyOwner {
|
||||
require(_threshold > 0 && _threshold <= 7 days, "Invalid threshold");
|
||||
priceStalenesThreshold = _threshold;
|
||||
}
|
||||
|
||||
function getPrice(address _token) external view returns (uint256) {
|
||||
if (_token == usdcAddress) {
|
||||
return _getUSDCPrice();
|
||||
@@ -44,15 +58,21 @@ contract LendingPriceFeed is OwnableUpgradeable, UUPSUpgradeable {
|
||||
|
||||
function _getUSDCPrice() internal view returns (uint256) {
|
||||
(
|
||||
/* uint80 roundId */,
|
||||
uint80 roundId,
|
||||
int256 price,
|
||||
/* uint256 startedAt */,
|
||||
/* uint256 updatedAt */,
|
||||
/* uint80 answeredInRound */
|
||||
uint256 updatedAt,
|
||||
uint80 answeredInRound
|
||||
) = usdcPriceFeed.latestRoundData();
|
||||
|
||||
// 价格有效性检查
|
||||
if (price <= 0) revert InvalidChainlinkPrice();
|
||||
|
||||
// 新鲜度检查:确保价格数据不过期
|
||||
if (updatedAt == 0) revert StalePrice();
|
||||
if (answeredInRound < roundId) revert StalePrice();
|
||||
if (block.timestamp - updatedAt > priceStalenesThreshold) revert StalePrice();
|
||||
|
||||
return uint256(price) * 1e22; // 1e22 = 10^(30-8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user