refactor unlock percentage
This commit is contained in:
@@ -302,35 +302,20 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
|
|||||||
|
|
||||||
// Epoch-based Staking Functions
|
// Epoch-based Staking Functions
|
||||||
|
|
||||||
/// @notice Internal function to calculate unlock percentage based on TVL/liability ratio improvement
|
/// @notice Internal function to calculate unlock percentage based on ratio improvement vs historical high
|
||||||
/// @dev Formula: (current_tvl / current_liability) - (last_tvl / last_liability) * payback_percent
|
/// @dev Formula: (current_ratio - highest_ratio) * payback_percent
|
||||||
function calculateUnlockPercentage(
|
function calculateUnlockPercentage(
|
||||||
uint256 currentTvl,
|
uint256 currentRatio,
|
||||||
uint256 currentLiability,
|
|
||||||
uint256 lastTvl,
|
|
||||||
uint256 lastLiability,
|
|
||||||
uint256 paybackPercent
|
uint256 paybackPercent
|
||||||
) internal view 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
|
// Check if current ratio is below the highest ratio ever achieved
|
||||||
if (currentRatio < highestRatio) {
|
if (currentRatio <= highestRatio) {
|
||||||
return 0; // No unlock if we're below historical high
|
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
|
// Ratio improvement * payback percentage
|
||||||
uint256 ratioImprovement = currentRatio - lastRatio;
|
uint256 ratioImprovement = currentRatio - highestRatio;
|
||||||
uint256 unlockPercentage = (ratioImprovement * paybackPercent) / 10000;
|
uint256 unlockPercentage = (ratioImprovement * paybackPercent) / 10000;
|
||||||
|
|
||||||
return unlockPercentage;
|
return unlockPercentage;
|
||||||
@@ -349,19 +334,10 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
|
|||||||
if (currentRatio > highestRatio) {
|
if (currentRatio > highestRatio) {
|
||||||
highestRatio = currentRatio;
|
highestRatio = currentRatio;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (currentEpochId > 0) {
|
if (currentEpochId > 0) {
|
||||||
// Get previous epoch data
|
unlockPercentage = calculateUnlockPercentage(currentRatio, _paybackPercent);
|
||||||
Epoch storage lastEpoch = epochs[currentEpochId - 1];
|
}
|
||||||
|
|
||||||
unlockPercentage = calculateUnlockPercentage(
|
|
||||||
currentTreasuryTvl,
|
|
||||||
_currentLiability,
|
|
||||||
lastEpoch.currentTreasuryTvl,
|
|
||||||
lastEpoch.totalLiability,
|
|
||||||
_paybackPercent
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that unlock percentage doesn't exceed maximum
|
// Check that unlock percentage doesn't exceed maximum
|
||||||
|
|||||||
Reference in New Issue
Block a user