Highest Ratio Change

This commit is contained in:
2025-09-27 23:40:28 +02:00
parent c329181447
commit f6e338e287
2 changed files with 93 additions and 100 deletions

View File

@@ -114,6 +114,7 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
mapping(address => mapping(uint256 => uint256)) private sellStakeKeyIndex; // Track position in keys array
MarketplaceHistory[] public marketplaceHistory; // Complete history of all transactions
mapping(address => uint256) public totalClaimed; // Track total amount claimed and sent to withdrawStakes per user
uint256 public highestRatio; // Track the highest TVL/liability ratio ever achieved (scaled by 10000)
// Events
event VestingClaimed(address indexed user, uint256 amount, uint256 bonus);
@@ -196,6 +197,10 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
maxUnlockPercentage = _maxPercentage;
}
function updateHighestRatio(uint256 _highestRatio) external onlyOwner {
highestRatio = _highestRatio;
}
function withdrawFromVestingPool(address _token, uint256 _amount) external onlyOwner {
IERC20(_token).safeTransfer(msg.sender, _amount);
emit FundsWithdrawn(msg.sender, _token, _amount);
@@ -300,29 +305,34 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
/// @notice Internal function to calculate unlock percentage based on TVL/liability ratio improvement
/// @dev Formula: (current_tvl / current_liability) - (last_tvl / last_liability) * payback_percent
function calculateUnlockPercentage(
uint256 currentTvl,
uint256 currentTvl,
uint256 currentLiability,
uint256 lastTvl,
uint256 lastTvl,
uint256 lastLiability,
uint256 paybackPercent
) internal pure returns (uint256) {
) internal view returns (uint256) {
if (lastLiability == 0 || currentLiability == 0) {
return 0; // Safety check
}
// Calculate ratios (scaled by 10000 for precision)
uint256 currentRatio = (currentTvl * 10000) / currentLiability;
uint256 lastRatio = (lastTvl * 10000) / lastLiability;
// Check if current ratio is below the highest ratio ever achieved
if (currentRatio < highestRatio) {
return 0; // No unlock if we're below historical high
}
if (currentRatio <= lastRatio) {
return 0; // No unlock if ratio didn't improve
}
// Ratio improvement * payback percentage
uint256 ratioImprovement = currentRatio - lastRatio;
uint256 unlockPercentage = (ratioImprovement * paybackPercent) / 10000;
return unlockPercentage;
}
@@ -332,11 +342,19 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
/// @param _paybackPercent Percentage multiplier for unlock calculation (scaled by 10000)
function endEpoch(uint256 estDaysRemaining, uint256 currentTreasuryTvl, uint256 _paybackPercent, uint256 _currentLiability) external onlyOwner {
uint256 unlockPercentage = 0;
// Calculate current ratio and update highest ratio if necessary
if (_currentLiability > 0) {
uint256 currentRatio = (currentTreasuryTvl * 10000) / _currentLiability;
if (currentRatio > highestRatio) {
highestRatio = currentRatio;
}
}
if (currentEpochId > 0) {
// Get previous epoch data
Epoch storage lastEpoch = epochs[currentEpochId - 1];
unlockPercentage = calculateUnlockPercentage(
currentTreasuryTvl,
_currentLiability,
@@ -345,20 +363,20 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
_paybackPercent
);
}
// Check that unlock percentage doesn't exceed maximum
require(unlockPercentage <= maxUnlockPercentage, "Unlock percentage high");
// Create new epoch entry
epochs[currentEpochId] = Epoch({
estDaysRemaining: estDaysRemaining,
currentTreasuryTvl: currentTreasuryTvl,
totalLiability: totalBigStakes,
totalLiability: _currentLiability,
paybackPercent: _paybackPercent,
unlockPercentage: unlockPercentage,
timestamp: block.timestamp
});
emit EpochEnded(currentEpochId, currentTreasuryTvl, unlockPercentage, _paybackPercent);
currentEpochId++;
}
@@ -1241,39 +1259,39 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
return totalClaimed[user];
}
/// @notice Search marketplace history for stakes where address was seller or buyer
/// @param targetAddress The address to search for as seller or buyer
/// @return Array of MarketplaceHistory structs where address was involved
function searchMarketplaceHistory(address targetAddress) external view returns (MarketplaceHistory[] memory) {
require(targetAddress != address(0), "Invalid address");
// Count matches first to size the result array properly
uint256 matchCount = 0;
for (uint256 i = 0; i < marketplaceHistory.length; i++) {
if (marketplaceHistory[i].seller == targetAddress || marketplaceHistory[i].buyer == targetAddress) {
matchCount++;
}
}
// Return empty array if no matches
if (matchCount == 0) {
return new MarketplaceHistory[](0);
}
// Create result array with exact size needed
MarketplaceHistory[] memory result = new MarketplaceHistory[](matchCount);
uint256 resultIndex = 0;
// Populate result array
for (uint256 i = 0; i < marketplaceHistory.length; i++) {
if (marketplaceHistory[i].seller == targetAddress || marketplaceHistory[i].buyer == targetAddress) {
result[resultIndex] = marketplaceHistory[i];
resultIndex++;
}
}
return result;
}
// /// @notice Search marketplace history for stakes where address was seller or buyer
// /// @param targetAddress The address to search for as seller or buyer
// /// @return Array of MarketplaceHistory structs where address was involved
// function searchMarketplaceHistory(address targetAddress) external view returns (MarketplaceHistory[] memory) {
// require(targetAddress != address(0), "Invalid address");
// // Count matches first to size the result array properly
// uint256 matchCount = 0;
// for (uint256 i = 0; i < marketplaceHistory.length; i++) {
// if (marketplaceHistory[i].seller == targetAddress || marketplaceHistory[i].buyer == targetAddress) {
// matchCount++;
// }
// }
// // Return empty array if no matches
// if (matchCount == 0) {
// return new MarketplaceHistory[](0);
// }
// // Create result array with exact size needed
// MarketplaceHistory[] memory result = new MarketplaceHistory[](matchCount);
// uint256 resultIndex = 0;
// // Populate result array
// for (uint256 i = 0; i < marketplaceHistory.length; i++) {
// if (marketplaceHistory[i].seller == targetAddress || marketplaceHistory[i].buyer == targetAddress) {
// result[resultIndex] = marketplaceHistory[i];
// resultIndex++;
// }
// }
// return result;
// }
/// @notice Test function for upgrade verification
/// @return Returns a constant value to verify upgrade worked