Update BigStake

This commit is contained in:
2025-10-09 17:14:54 +02:00
parent f37326855e
commit 74296bb16a
4 changed files with 309 additions and 257 deletions

View File

@@ -208,6 +208,16 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
emit FundsWithdrawn(msg.sender, BASE_TOKEN, _amount);
}
/// @notice Admin function to adjust withdraw liabilities (for fixing decimal conversion issues)
/// @param token The token address to adjust
/// @param newAmount The new liability amount (should be in token's native decimals)
function adjustWithdrawLiability(address token, uint256 newAmount) external onlyOwner {
uint256 oldAmount = withdrawLiabilities[token];
withdrawLiabilities[token] = newAmount;
}
function setPriceOracle(address _token, address _oracle) external onlyOwner {
priceOracles[_token] = _oracle;
}
@@ -357,6 +367,54 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
currentEpochId++;
}
/// @notice Re-enter/update the most recent epoch with new values and recalculated unlock percentage
/// @param estDaysRemaining Estimated days remaining for the protocol
/// @param currentTreasuryTvl Current treasury total value locked
/// @param _paybackPercent Percentage multiplier for unlock calculation (scaled by 10000)
/// @param _currentLiability Current total liability amount
function reenterEpoch(uint256 estDaysRemaining, uint256 currentTreasuryTvl, uint256 _paybackPercent, uint256 _currentLiability) external onlyOwner {
require(currentEpochId > 0, "No epochs to update");
uint256 epochId = currentEpochId - 1; // Most recent epoch
uint256 unlockPercentage = 0;
// Calculate current ratio
if (_currentLiability > 0) {
uint256 currentRatio = (currentTreasuryTvl * 10000) / _currentLiability;
if (epochId > 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
require(unlockPercentage <= maxUnlockPercentage, "Unlock percentage high");
// Update the existing epoch entry
epochs[epochId] = Epoch({
estDaysRemaining: estDaysRemaining,
currentTreasuryTvl: currentTreasuryTvl,
totalLiability: _currentLiability,
paybackPercent: _paybackPercent,
unlockPercentage: unlockPercentage,
timestamp: block.timestamp
});
emit EpochEnded(epochId, currentTreasuryTvl, unlockPercentage, _paybackPercent);
}
/// @notice Set a manual currentEpochId
/// @param epochId The epoch ID to set as the currentEpochId
function setEpochId(uint256 epochId) external onlyOwner {
currentEpochId = epochId;
}
/// @notice Calculate total unclaimed funds for a user across all epochs since last claim
function calculateUnclaimedFunds(address user) public view returns (uint256 totalUnclaimed) {
uint256 remainingStake = getNetStake(user);
@@ -452,8 +510,8 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
transferAmount = amount / 1e12; // Convert from 18 decimals to 6 decimals
}
// Decrement withdraw liabilities for all tokens (using original 18-decimal amount)
withdrawLiabilities[token] -= amount;
// Decrement withdraw liabilities (using correct decimal precision)
withdrawLiabilities[token] -= transferAmount;
// Transfer tokens to user based on the specified token (using converted amount)
IERC20(token).safeTransfer(msg.sender, transferAmount);
@@ -507,35 +565,35 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
// Bot Functions for Staking Management
/// @notice Create a withdraw stake for a user (for admin/migration purposes)
/// @dev Only to be used by bots for manual withdraw stake creation
/// @param user The user address to create the withdraw stake for
/// @param amount The amount for the withdraw stake
/// @param unlockTime The unlock timestamp for the withdraw stake
/// @param token The token address for the withdraw stake
/// @param isVesting Whether this is a vesting-related stake (adds 1e6 to stakeId)
function createWithdrawStakeForUser(address user, uint256 amount, uint256 unlockTime, address token, bool isVesting) external onlyBot {
require(user != address(0), "Invalid user address");
require(amount > 0, "Invalid amount");
require(token != address(0), "Invalid token address");
// /// @notice Create a withdraw stake for a user (for admin/migration purposes)
// /// @dev Only to be used by bots for manual withdraw stake creation
// /// @param user The user address to create the withdraw stake for
// /// @param amount The amount for the withdraw stake
// /// @param unlockTime The unlock timestamp for the withdraw stake
// /// @param token The token address for the withdraw stake
// /// @param isVesting Whether this is a vesting-related stake (adds 1e6 to stakeId)
// function createWithdrawStakeForUser(address user, uint256 amount, uint256 unlockTime, address token, bool isVesting) external onlyBot {
// require(user != address(0), "Invalid user address");
// require(amount > 0, "Invalid amount");
// require(token != address(0), "Invalid token address");
// Generate unique stakeId
stakeIdCounter++;
uint256 finalStakeId = isVesting ? stakeIdCounter + 1e6 : stakeIdCounter;
// // Generate unique stakeId
// stakeIdCounter++;
// uint256 finalStakeId = isVesting ? stakeIdCounter + 1e6 : stakeIdCounter;
// Create the withdraw stake
withdrawStakes[user].push(WithdrawStake({
stakeId: finalStakeId,
amount: amount,
unlockTime: unlockTime,
token: token
}));
// // Create the withdraw stake
// withdrawStakes[user].push(WithdrawStake({
// stakeId: finalStakeId,
// amount: amount,
// unlockTime: unlockTime,
// token: token
// }));
// Increment withdraw liabilities for this token
withdrawLiabilities[token] += amount;
// // Increment withdraw liabilities for this token
// withdrawLiabilities[token] += amount;
emit StakeWithdrawn(user, amount, finalStakeId);
}
// emit StakeWithdrawn(user, amount, finalStakeId);
// }
/// @notice Batch create stakes for multiple users (efficient for migration)
/// @dev Only to be used by bots for initial setup
@@ -1201,8 +1259,8 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
// Only update liabilities and emit event if something was actually claimed
if (totalBonusClaimed > 0) {
// Increment withdraw liabilities for BASE_TOKEN
withdrawLiabilities[BASE_TOKEN] += totalBonusClaimed;
// Increment withdraw liabilities for BASE_TOKEN (convert from 18 decimals to 6 decimals)
withdrawLiabilities[BASE_TOKEN] += totalBonusClaimed / 1e12;
emit BonusClaimed(msg.sender, totalBonusClaimed);
}
}
@@ -1321,7 +1379,7 @@ contract CunaFinanceBase is Initializable, ReentrancyGuardUpgradeable {
/// @notice Test function for upgrade verification
/// @return Returns a constant value to verify upgrade worked
function testUpgradeFunction() external pure returns (uint256) {
return 1000; // Updated value to trigger upgrade detection
return 1002; // Added adjustWithdrawLiability admin function
}
}