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

File diff suppressed because one or more lines are too long

View File

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