diff --git a/contracts/CunaFinanceBsc.sol b/contracts/CunaFinanceBsc.sol index ed73f2b..91047d8 100644 --- a/contracts/CunaFinanceBsc.sol +++ b/contracts/CunaFinanceBsc.sol @@ -328,16 +328,19 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable { function endEpoch(uint256 estDaysRemaining, uint256 currentTreasuryTvl, uint256 _paybackPercent, uint256 _currentLiability) external onlyOwner { uint256 unlockPercentage = 0; - // Calculate current ratio and update highest ratio if necessary + // Calculate current ratio if (_currentLiability > 0) { uint256 currentRatio = (currentTreasuryTvl * 10000) / _currentLiability; - if (currentRatio > highestRatio) { - highestRatio = currentRatio; - } if (currentEpochId > 0) { + // Calculate unlock percentage BEFORE updating highest ratio unlockPercentage = calculateUnlockPercentage(currentRatio, _paybackPercent); } + + // Update highest ratio AFTER calculating unlock percentage + if (currentRatio > highestRatio) { + highestRatio = currentRatio; + } } // Check that unlock percentage doesn't exceed maximum