Skip to content

Commit

Permalink
bonding: Skip fee calculation for voting power
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Oct 13, 2023
1 parent 9cb915b commit f52b182
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
8 changes: 1 addition & 7 deletions contracts/bonding/BondingVotes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,7 @@ contract BondingVotes is ManagerProxyTarget, IBondingVotes {
return bond.bondedAmount;
}

(uint256 stakeWithRewards, ) = EarningsPoolLIP36.delegatorCumulativeStakeAndFees(
startPool,
endPool,
bond.bondedAmount,
0
);
return stakeWithRewards;
return EarningsPoolLIP36.delegatorCumulativeStake(startPool, endPool, bond.bondedAmount);
}

/**
Expand Down
63 changes: 52 additions & 11 deletions contracts/bonding/libraries/EarningsPoolLIP36.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ library EarningsPoolLIP36 {

/**
* @notice Calculates a delegator's cumulative stake and fees using the LIP-36 earnings claiming algorithm.
* @dev This internally calls {delegatorCumulativeStake} and {delegatorCumulativeFees} to calculate stake and fees.
* @param _startPool The earning pool from the start round for the start cumulative factors. Normally this is the
* earning pool from the {Delegator-lastclaimRound}+1 round, as the round where `bondedAmount` was measured.
* earning pool from the {Delegator-lastClaimRound} round, as the round where `_stake` was measured.
* @param _endPool The earning pool from the end round for the end cumulative factors
* @param _stake The delegator initial stake before including earned rewards. Normally the {Delegator-bondedAmount}
* @param _stake The delegator stake at the start round, before earned rewards. Normally {Delegator-bondedAmount}.
* @param _fees The delegator's initial fees before including earned fees
* @return cStake , cFees where cStake is the delegator's cumulative stake including earned rewards and cFees is the
* delegator's cumulative fees including earned fees
Expand All @@ -74,6 +75,23 @@ library EarningsPoolLIP36 {
uint256 _stake,
uint256 _fees
) internal pure returns (uint256 cStake, uint256 cFees) {
cStake = delegatorCumulativeStake(_startPool, _endPool, _stake);
cFees = delegatorCumulativeFees(_startPool, _endPool, _stake, _fees);
}

/**
* @notice Calculates a delegator's cumulative stake using the LIP-36 earnings claiming algorithm.
* @param _startPool The earning pool from the start round for the start cumulative factors. Normally this is the
* earning pool from the {Delegator-lastClaimRound} round, as the round where `_stake` was measured.
* @param _endPool The earning pool from the end round for the end cumulative factors.
* @param _stake The delegator stake at the start round, before earned rewards. Normally {Delegator-bondedAmount}.
* @return The delegator's cumulative stake including earned rewards.
*/
function delegatorCumulativeStake(
EarningsPool.Data memory _startPool,
EarningsPool.Data memory _endPool,
uint256 _stake
) internal pure returns (uint256) {
// If the start cumulativeRewardFactor is 0 set the default value to PreciseMathUtils.percPoints(1, 1)
if (_startPool.cumulativeRewardFactor == 0) {
_startPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1);
Expand All @@ -84,16 +102,39 @@ library EarningsPoolLIP36 {
_endPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1);
}

cFees = _fees.add(
PreciseMathUtils.percOf(
_stake,
_endPool.cumulativeFeeFactor.sub(_startPool.cumulativeFeeFactor),
_startPool.cumulativeRewardFactor
)
);
return PreciseMathUtils.percOf(_stake, _endPool.cumulativeRewardFactor, _startPool.cumulativeRewardFactor);
}

/**
* @notice Calculates a delegator's cumulative fees using the LIP-36 earnings claiming algorithm.
* @param _startPool The earning pool from the start round for the start cumulative factors. Normally this is the
* earning pool from the {Delegator-lastClaimRound} round, as the round where `_stake` was measured.
* @param _endPool The earning pool from the end round for the end cumulative factors.
* @param _stake The delegator stake at the start round, before earned rewards. Normally {Delegator-bondedAmount}.
* @param _fees The delegator's initial fees before including earned fees.
* @return The delegator's cumulative fees including earned fees.
*/
function delegatorCumulativeFees(
EarningsPool.Data memory _startPool,
EarningsPool.Data memory _endPool,
uint256 _stake,
uint256 _fees
) internal pure returns (uint256) {
// If the start cumulativeRewardFactor is 0 set the default value to PreciseMathUtils.percPoints(1, 1)
if (_startPool.cumulativeRewardFactor == 0) {
_startPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1);
}

cStake = PreciseMathUtils.percOf(_stake, _endPool.cumulativeRewardFactor, _startPool.cumulativeRewardFactor);
// If the end cumulativeRewardFactor is 0 set the default value to PreciseMathUtils.percPoints(1, 1)
if (_endPool.cumulativeRewardFactor == 0) {
_endPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1);
}

return (cStake, cFees);
uint256 earnedFees = PreciseMathUtils.percOf(
_stake,
_endPool.cumulativeFeeFactor.sub(_startPool.cumulativeFeeFactor),
_startPool.cumulativeRewardFactor
);
return _fees.add(earnedFees);
}
}

0 comments on commit f52b182

Please sign in to comment.