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,44 +208,52 @@ contract CunaFinanceSonic is Initializable, ReentrancyGuardUpgradeable {
emit FundsWithdrawn(msg.sender, SONIC_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 {
withdrawLiabilities[token] = newAmount;
}
function setPriceOracle(address _token, address _oracle) external onlyOwner {
priceOracles[_token] = _oracle;
}
/// @notice Set unlock schedule for a token using percentage-based steps
/// @param _token The token address to set the schedule for
/// @param _lockTime The initial lock time in seconds
/// @param _percentagePerStep The percentage to unlock at each step (scaled by 10000)
function setUnlockScheduleByPercentage(address _token, uint256 _lockTime, uint256 _percentagePerStep) external onlyOwner {
require(_token != address(0), "Invalid token address");
require(_percentagePerStep > 0 && _percentagePerStep <= 10000, "Invalid percentage");
// /// @notice Set unlock schedule for a token using percentage-based steps
// /// @param _token The token address to set the schedule for
// /// @param _lockTime The initial lock time in seconds
// /// @param _percentagePerStep The percentage to unlock at each step (scaled by 10000)
// function setUnlockScheduleByPercentage(address _token, uint256 _lockTime, uint256 _percentagePerStep) external onlyOwner {
// require(_token != address(0), "Invalid token address");
// require(_percentagePerStep > 0 && _percentagePerStep <= 10000, "Invalid percentage");
// Clear existing schedule
delete unlockSchedules[_token];
// // Clear existing schedule
// delete unlockSchedules[_token];
uint256 totalPercentage = 0;
uint256 timeOffset = _lockTime;
// uint256 totalPercentage = 0;
// uint256 timeOffset = _lockTime;
// Create unlock steps until we reach 100%
while (totalPercentage < 10000) {
uint256 stepPercentage = _percentagePerStep;
// // Create unlock steps until we reach 100%
// while (totalPercentage < 10000) {
// uint256 stepPercentage = _percentagePerStep;
// Adjust last step to exactly reach 100%
if (totalPercentage + stepPercentage > 10000) {
stepPercentage = 10000 - totalPercentage;
}
// // Adjust last step to exactly reach 100%
// if (totalPercentage + stepPercentage > 10000) {
// stepPercentage = 10000 - totalPercentage;
// }
unlockSchedules[_token].push(UnlockStep({
timeOffset: timeOffset,
percentage: stepPercentage
}));
// unlockSchedules[_token].push(UnlockStep({
// timeOffset: timeOffset,
// percentage: stepPercentage
// }));
totalPercentage += stepPercentage;
timeOffset += _lockTime; // Each step adds the same time interval
}
// totalPercentage += stepPercentage;
// timeOffset += _lockTime; // Each step adds the same time interval
// }
emit UnlockScheduleSet(_token);
}
// emit UnlockScheduleSet(_token);
// }
// /// @notice Set custom unlock schedule for a token with specific steps
// /// @param _token The token address to set the schedule for
@@ -357,6 +365,48 @@ contract CunaFinanceSonic 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 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 +502,8 @@ contract CunaFinanceSonic 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 +557,35 @@ contract CunaFinanceSonic 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 +1251,8 @@ contract CunaFinanceSonic is Initializable, ReentrancyGuardUpgradeable {
// Only update liabilities and emit event if something was actually claimed
if (totalBonusClaimed > 0) {
// Increment withdraw liabilities for SONIC_TOKEN
withdrawLiabilities[SONIC_TOKEN] += totalBonusClaimed;
// Increment withdraw liabilities for SONIC_TOKEN (convert from 18 decimals to 6 decimals)
withdrawLiabilities[SONIC_TOKEN] += totalBonusClaimed / 1e12;
emit BonusClaimed(msg.sender, totalBonusClaimed);
}
}
@@ -1284,44 +1334,11 @@ contract CunaFinanceSonic 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 Test function for upgrade verification
/// @return Returns a constant value to verify upgrade worked
function testUpgradeFunction() external pure returns (uint256) {
return 999; // Different value from bsc_paca to distinguish contracts
return 1002; // Added adjustWithdrawLiability admin function
}
}