fix contract

This commit is contained in:
2026-01-12 14:33:16 +08:00
parent a18b9a42e4
commit d56f83726b
70 changed files with 1988 additions and 142 deletions

View File

@@ -24,6 +24,7 @@ contract YTPriceFeed is Initializable, UUPSUpgradeable {
error SpreadTooHigh();
error InvalidAddress();
error InvalidChainlinkPrice();
error StalePrice();
address public gov;
@@ -35,6 +36,7 @@ contract YTPriceFeed is Initializable, UUPSUpgradeable {
// 价格保护参数
uint256 public maxPriceChangeBps; // 5% 最大价格变动
uint256 public priceStalenesThreshold; // 价格过期阈值(秒)
/// @notice USDC价格Feed
AggregatorV3Interface internal usdcPriceFeed;
@@ -72,6 +74,7 @@ contract YTPriceFeed is Initializable, UUPSUpgradeable {
usdcPriceFeed = AggregatorV3Interface(_usdcPriceFeed);
gov = msg.sender;
maxPriceChangeBps = 500; // 5% 最大价格变动
priceStalenesThreshold = 3600; // 默认1小时
}
/**
@@ -116,6 +119,15 @@ contract YTPriceFeed is Initializable, UUPSUpgradeable {
maxPriceChangeBps = _maxPriceChangeBps;
}
/**
* @notice 设置价格过期阈值
* @param _threshold 阈值例如3600 = 1小时86400 = 24小时
*/
function setPriceStalenessThreshold(uint256 _threshold) external onlyGov {
require(_threshold > 0 && _threshold <= 7 days, "Invalid threshold");
priceStalenesThreshold = _threshold;
}
/**
* @notice 设置代币价差
* @param _token 代币地址
@@ -220,15 +232,21 @@ contract YTPriceFeed is Initializable, 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)
}