Skip to content

Commit

Permalink
adjust locked and unlocked reward
Browse files Browse the repository at this point in the history
  • Loading branch information
uprendis committed Jul 9, 2020
1 parent 438dcb9 commit ec0104f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
62 changes: 31 additions & 31 deletions contracts/sfc/Staker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ contract Stakers is Ownable, StakersConstants {
uint256 stakeTotalAmount;
uint256 delegationsTotalAmount;
uint256 totalSupply;
uint256 totalLockedAmount;
}

struct LockedAmount {
Expand Down Expand Up @@ -384,7 +383,7 @@ contract Stakers is Ownable, StakersConstants {
_syncStaker(to);
}

function _calcRawValidatorEpochReward(uint256 stakerID, uint256 epoch, uint256 unlockedRatio) internal view returns (uint256) {
function _calcRawValidatorEpochReward(uint256 stakerID, uint256 epoch) internal view returns (uint256) {
uint256 totalBaseRewardWeight = epochSnapshots[epoch].totalBaseRewardWeight;
uint256 baseRewardWeight = epochSnapshots[epoch].validators[stakerID].baseRewardWeight;
uint256 totalTxRewardWeight = epochSnapshots[epoch].totalTxRewardWeight;
Expand All @@ -394,9 +393,6 @@ contract Stakers is Ownable, StakersConstants {
uint256 baseReward = 0;
if (baseRewardWeight != 0) {
uint256 totalReward = epochSnapshots[epoch].duration.mul(epochSnapshots[epoch].baseRewardPerSecond);
if (firstLockedUpEpoch > 0 && epoch >= firstLockedUpEpoch) {
totalReward = totalReward.mul(unlockedRatio).div(RATIO_UNIT);
}
baseReward = totalReward.mul(baseRewardWeight).div(totalBaseRewardWeight);
}
// fee reward
Expand All @@ -410,21 +406,28 @@ contract Stakers is Ownable, StakersConstants {
return baseReward.add(txReward);
}

function _calcLockedUpReward(uint256 amount, uint256 epoch) internal view returns (uint256) {
if (epochSnapshots[epoch].totalLockedAmount != 0) {
uint256 totalReward = epochSnapshots[epoch].duration.mul(epochSnapshots[epoch].baseRewardPerSecond);
return totalReward.mul(RATIO_UNIT - unlockedRatio()).div(RATIO_UNIT).mul(amount).div(epochSnapshots[epoch].totalLockedAmount);
}
return 0;
}

function _calcDelegationPenalty(address delegator, uint256 stakerID, uint256 withdrawalAmount) internal view returns (uint256) {
uint256 delegationAmount = delegations_v2[delegator][stakerID].amount;
return delegationEarlyWithdrawalPenalty[delegator][stakerID].mul(withdrawalAmount).div(delegationAmount);
}

function _calcValidatorEpochReward(uint256 stakerID, uint256 epoch, uint256 commission) internal view returns (uint256 unlockedReward, uint256 lockedUpReward) {
uint256 rawReward = _calcRawValidatorEpochReward(stakerID, epoch, unlockedRatio());
function _calcLockingReward(uint256 fullReward, bool isLockingFeatureActive, bool isLockedUp) private pure returns (uint256 unlockedReward, uint256 lockedUpReward) {
if (isLockingFeatureActive) {
unlockedReward = fullReward.mul(unlockedRewardRatio()).div(RATIO_UNIT);
if (isLockedUp) {
lockedUpReward = fullReward - unlockedReward;
} else {
lockedUpReward = 0;
}
} else {
unlockedReward = fullReward;
lockedUpReward = 0;
}
return (unlockedReward, lockedUpReward);
}

function _calcValidatorEpochReward(uint256 stakerID, uint256 epoch, uint256 commission) public view returns (uint256, uint256) {
uint256 rawReward = _calcRawValidatorEpochReward(stakerID, epoch);

uint256 stake = epochSnapshots[epoch].validators[stakerID].stakeAmount;
uint256 delegatedTotal = epochSnapshots[epoch].validators[stakerID].delegatedMe;
Expand All @@ -434,17 +437,15 @@ contract Stakers is Ownable, StakersConstants {
}
uint256 weightedTotalStake = stake.add((delegatedTotal.mul(commission)).div(RATIO_UNIT));

unlockedReward = rawReward.mul(weightedTotalStake).div(totalStake);
if (firstLockedUpEpoch > 0 && epoch >= firstLockedUpEpoch) {
if (lockedStakes[stakerID].fromEpoch <= epoch && lockedStakes[stakerID].endTime > epochSnapshots[epoch.sub(1)].endTime) {
lockedUpReward = _calcLockedUpReward(stake, epoch);
}
}
return (unlockedReward, lockedUpReward);
uint256 fullReward = rawReward.mul(weightedTotalStake).div(totalStake);
bool isLockingFeatureActive = firstLockedUpEpoch > 0 && epoch >= firstLockedUpEpoch;
bool isLockedUp = lockedStakes[stakerID].fromEpoch <= epoch && lockedStakes[stakerID].endTime > epochSnapshots[epoch - 1].endTime;

return _calcLockingReward(fullReward, isLockingFeatureActive, isLockedUp);
}

function _calcDelegationEpochReward(address delegator, uint256 stakerID, uint256 epoch, uint256 commission) internal view returns (uint256 unlockedReward, uint256 lockedUpReward) {
uint256 rawReward = _calcRawValidatorEpochReward(stakerID, epoch, unlockedRatio());
function _calcDelegationEpochReward(address delegator, uint256 stakerID, uint256 epoch, uint256 commission) public view returns (uint256, uint256) {
uint256 rawReward = _calcRawValidatorEpochReward(stakerID, epoch);

uint256 stake = epochSnapshots[epoch].validators[stakerID].stakeAmount;
uint256 delegatedTotal = epochSnapshots[epoch].validators[stakerID].delegatedMe;
Expand All @@ -454,13 +455,12 @@ contract Stakers is Ownable, StakersConstants {
}
uint256 delegationAmount = delegations_v2[delegator][stakerID].amount;
uint256 weightedTotalStake = (delegationAmount.mul(RATIO_UNIT.sub(commission))).div(RATIO_UNIT);
unlockedReward = rawReward.mul(weightedTotalStake).div(totalStake);
if (firstLockedUpEpoch > 0 && epoch >= firstLockedUpEpoch) {
if (lockedDelegations[delegator][stakerID].fromEpoch <= epoch && lockedDelegations[delegator][stakerID].endTime > epochSnapshots[epoch.sub(1)].endTime) {
lockedUpReward = _calcLockedUpReward(delegationAmount, epoch);
}
}
return (unlockedReward, lockedUpReward);

uint256 fullReward = rawReward.mul(weightedTotalStake).div(totalStake);
bool isLockingFeatureActive = firstLockedUpEpoch > 0 && epoch >= firstLockedUpEpoch;
bool isLockedUp = lockedDelegations[delegator][stakerID].fromEpoch <= epoch && lockedDelegations[delegator][stakerID].endTime > epochSnapshots[epoch - 1].endTime;

return _calcLockingReward(fullReward, isLockingFeatureActive, isLockedUp);
}

function withDefault(uint256 a, uint256 defaultA) pure private returns(uint256) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/sfc/StakerConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract StakersConstants {
/**
* @dev The ratio of the reward rate at base rate (no lock), e.g., 30%
*/
function unlockedRatio() public pure returns (uint256) {
function unlockedRewardRatio() public pure returns (uint256) {
return (30 * RATIO_UNIT) / 100; // 30%
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/test/UnitTestStakers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract UnitTestStakers is Stakers {
firstLockedUpEpoch <= currentSealedEpoch &&
lockedStakes[stakerID].fromEpoch <= currentSealedEpoch &&
lockedStakes[stakerID].endTime >= newSnapshot.endTime) {
newSnapshot.totalLockedAmount += stakers[stakerID].stakeAmount;
//newSnapshot.totalLockedAmount += stakers[stakerID].stakeAmount;
}
newSnapshot.validators[stakerID] = ValidatorMerit(
stakers[stakerID].stakeAmount,
Expand All @@ -120,7 +120,7 @@ contract UnitTestStakers is Stakers {
uint256 stakerID = delegationIDsArr[i].stakerID;
if (lockedDelegations[delegator][stakerID].fromEpoch <= currentSealedEpoch &&
lockedDelegations[delegator][stakerID].endTime >= newSnapshot.endTime) {
newSnapshot.totalLockedAmount += delegations_v2[delegator][stakerID].amount;
//newSnapshot.totalLockedAmount += delegations_v2[delegator][stakerID].amount;
}
}
}
Expand All @@ -133,7 +133,7 @@ contract UnitTestStakers is Stakers {
}

function calcRawValidatorEpochReward(uint256 stakerID, uint256 epoch) external view returns (uint256) {
return _calcRawValidatorEpochReward(stakerID, epoch, unlockedRatio());
return _calcRawValidatorEpochReward(stakerID, epoch, unlockedRewardRatio());
}

function calcLockedUpReward(uint256 amount, uint256 epoch) external view returns (uint256) {
Expand Down

0 comments on commit ec0104f

Please # to comment.