NetStake changes

This commit is contained in:
2025-09-15 20:34:01 +02:00
parent 8e0b6a58bd
commit 71f7d498b2
2 changed files with 82 additions and 15 deletions

File diff suppressed because one or more lines are too long

View File

@@ -96,11 +96,13 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
// Epoch-based staking variables
mapping(uint256 => Epoch) public epochs;
mapping(address => uint256) public userBigStake; // User's main stake amount
mapping(address => uint256) public userOriginalStake; // User's original stake amount (never changes)
mapping(address => uint256) public userLastClaimedEpoch; // Last epoch user claimed from
mapping(address => WithdrawStake[]) public withdrawStakes; // User's withdrawable stakes
uint256 public currentEpochId;
uint256 public totalBigStakes; // Total liability (sum of all user stakes)
uint256 public instantBuyoutPercent;// Percentage for instant buyout (e.g., 8000 = 80%)
uint256 public maxUnlockPercentage; // Maximum unlock percentage per epoch (e.g., 100 = 1%)
// Marketplace variables
mapping(address => mapping(uint256 => SellStake)) public sellStakes; // seller => stakeId => SellStake
@@ -162,6 +164,7 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
totalBigStakes += 10000 * 1e18;
unlockDelay = 60 * 60 * 36;
maxUnlockPercentage = 100; // 1% maximum unlock per epoch
}
// Ownership Management
@@ -187,6 +190,11 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
function updateUnlockDelay(uint256 _delay) external onlyOwner {
unlockDelay = _delay;
}
function updateMaxUnlockPercentage(uint256 _maxPercentage) external onlyOwner {
require(_maxPercentage > 0, "Max percentage must be greater than 0");
maxUnlockPercentage = _maxPercentage;
}
function withdrawFromVestingPool(address _token, uint256 _amount) external onlyOwner {
IERC20(_token).safeTransfer(msg.sender, _amount);
@@ -338,6 +346,9 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
);
}
// Check that unlock percentage doesn't exceed maximum
require(unlockPercentage <= maxUnlockPercentage, "Unlock percentage high");
// Create new epoch entry
epochs[currentEpochId] = Epoch({
estDaysRemaining: estDaysRemaining,
@@ -354,7 +365,7 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
/// @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 = userBigStake[user];
uint256 remainingStake = getNetStake(user);
uint256 startEpoch = userLastClaimedEpoch[user];
for (uint256 i = startEpoch; i < currentEpochId; i++) {
@@ -370,23 +381,24 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
/// @notice Get user's net stake (big stake minus unclaimed funds minus pending sell stakes)
function getNetStake(address user) public view returns (uint256) {
uint256 bigStake = userBigStake[user];
uint256 unclaimed = calculateUnclaimedFunds(user);
uint256 pending = pendingSellStakes[user];
uint256 committed = unclaimed + pending;
uint256 committed = pendingSellStakes[user];
return bigStake > committed ? bigStake - committed : 0;
}
/// @notice Get comprehensive user stake information
function getUserStakeInfo(address user) external view returns (
uint256 netStake, // Current "active" stake amount
uint256 bigStake, // Current "active" stake amount
uint256 unclaimedFunds, // Available to claim
uint256 totalOriginalStake // Original big number (for reference)
uint256 netStake, // Available for new actions (minus pending sells)
uint256 originalStake // Original stake amount (never changes)
) {
uint256 unclaimed = calculateUnclaimedFunds(user);
uint256 net = getNetStake(user);
return (
userBigStake[user] - unclaimed, // Net stake
unclaimed, // Unclaimed
userBigStake[user] // Original
userBigStake[user],
unclaimed,
net,
userOriginalStake[user]
);
}
@@ -505,6 +517,11 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
// Update total stakes accounting
totalBigStakes = totalBigStakes - userBigStake[user] + amount;
// Set original stake only if this is the first time (never changes after)
if (userOriginalStake[user] == 0) {
userOriginalStake[user] = amount;
}
// Set user's big stake
userBigStake[user] = amount;
@@ -732,8 +749,8 @@ contract CunaFinanceBsc is Initializable, ReentrancyGuardUpgradeable {
userBigStake[seller] -= value;
userBigStake[msg.sender] += buyerStake;
pendingSellStakes[seller] -= value;
// Note: totalBigStakes decreases by protocolShare (value - buyerStake)
totalBigStakes -= (value - buyerStake);
// Note: totalBigStakes decreases by protocolShare
totalBigStakes -= protocolShare;
// Track marketplace sales for seller
marketplace_sales[seller] += salePrice;