Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fetch rewards #1

Merged
merged 9 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions contracts/RewardPool/IRewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ interface IRewardPool {
function getValidatorReward(address validator) external view returns (uint256);

/**
* @notice Gets delegators's unclaimed rewards without custom rewards
* @notice Gets delegator's unclaimed rewards without custom rewards
* @param validator Address of validator
* @param delegator Address of delegator
* @return Delegator's unclaimed rewards with validator (in MATIC wei)
Expand Down Expand Up @@ -206,18 +206,25 @@ interface IRewardPool {
) external view returns (uint256 penalty, uint256 reward);

/**
* @notice Returns the penalty and reward that will be burned, if vested delegate position is active
* @notice Returns the penalty that will taken from the delegator, if the position is still active
* @param validator The address of the validator
* @param delegator The address of the delegator
* @param amount The amount that is going to be undelegated
* @return penalty for the delegator
* @return reward of the delegator
*/
function calculateDelegatePositionPenalty(
function calculatePositionPenalty(
address validator,
address delegator,
uint256 amount
) external view returns (uint256 penalty, uint256 reward);
) external view returns (uint256 penalty);

/**
* @notice Returns the total reward that is generated for a position
* @param validator The address of the validator
* @param delegator The address of the delegator
* @return reward for the delegator
*/
function calculateTotalPositionReward(address validator, address delegator) external view returns (uint256 reward);

/**
* @notice Claims reward for the vest manager (delegator).
Expand Down
6 changes: 6 additions & 0 deletions contracts/RewardPool/modules/APR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ contract APR is Initializable, AccessControl {
return 1e18;
}

/**
* @notice Function that calculates the end reward for a user (without vesting bonuses) based on the pool reward index.
* @dev Denominator is used because we should work with floating-point numbers
* @param reward index The reward to which we gonna apply the base APR
* @dev The reward with the applied APR
*/
function _applyCustomReward(uint256 reward) internal view returns (uint256) {
return _applyBaseAPR(reward) / EPOCHS_YEAR;
}
Expand Down
43 changes: 25 additions & 18 deletions contracts/RewardPool/modules/DelegationRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
return delegationPools[validator].supply;
}

/**
* @inheritdoc IRewardPool
*/
function getRawDelegatorReward(address validator, address delegator) external view returns (uint256) {
DelegationPool storage delegation = delegationPools[validator];
return delegation.claimableRewards(delegator);
}

/**
* @inheritdoc IRewardPool
*/
Expand All @@ -74,13 +66,12 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
address delegator,
uint256 epochNumber,
uint256 topUpIndex
) external view returns (uint256) {
) external view returns (uint256 sumReward) {
VestingPosition memory position = delegationPositions[validator][delegator];
if (_noRewardConditions(position)) {
return 0;
}

uint256 sumReward;
DelegationPool storage delegationPool = delegationPools[validator];
bool rsi = true;
if (_isTopUpMade(validator, delegator)) {
Expand All @@ -95,13 +86,15 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
sumReward += _applyCustomReward(position, rsiReward, true);
}

// distribute the proper vesting reward
(uint256 epochRPS, uint256 balance, int256 correction) = _rewardParams(
validator,
delegator,
epochNumber,
topUpIndex
);
uint256 reward = delegationPool.claimableRewards(delegator, epochRPS, balance, correction) - sumReward;

uint256 reward = delegationPool.claimableRewards(delegator, epochRPS, balance, correction);
reward = _applyCustomReward(position, reward, rsi);
sumReward += reward;

Expand All @@ -111,27 +104,30 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
additionalReward = _applyCustomReward(additionalReward);
sumReward += additionalReward;
}

return sumReward;
}

/**
* @inheritdoc IRewardPool
*/
function calculateDelegatePositionPenalty(
function calculatePositionPenalty(
address validator,
address delegator,
uint256 amount
) external view returns (uint256 penalty, uint256 reward) {
DelegationPool storage pool = delegationPools[validator];
) external view returns (uint256 penalty) {
VestingPosition memory position = delegationPositions[validator][delegator];
if (position.isActive()) {
penalty = _calcSlashing(position, amount);
// apply the max Vesting bonus, because the full reward must be burned
reward = applyMaxReward(pool.claimableRewards(delegator));
}
}

/**
* @inheritdoc IRewardPool
*/
function calculateTotalPositionReward(address validator, address delegator) external view returns (uint256 reward) {
VestingPosition memory position = delegationPositions[validator][delegator];
reward = _applyCustomReward(position, getRawDelegatorReward(validator, delegator), true);
}

/**
* @inheritdoc IRewardPool
*/
Expand All @@ -149,6 +145,9 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
delegationPools[validator].validator = validator;
}

/**
* @inheritdoc IRewardPool
*/
function onDelegate(address validator, address delegator, uint256 amount) external onlyValidatorSet {
DelegationPool storage delegation = delegationPools[validator];

Expand Down Expand Up @@ -371,6 +370,14 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa

// _______________ Public functions _______________

/**
* @inheritdoc IRewardPool
*/
function getRawDelegatorReward(address validator, address delegator) public view returns (uint256) {
DelegationPool storage delegation = delegationPools[validator];
return delegation.claimableRewards(delegator);
}

// TODO: Check if the commitEpoch is the last transaction in the epoch, otherwise bug may occur
/**
* @notice Checks if balance change was already made in the current epoch
Expand Down
38 changes: 27 additions & 11 deletions contracts/RewardPool/modules/Vesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,36 @@ error NotVestingManager();
abstract contract Vesting is APR {
using VestingPositionLib for VestingPosition;

/// @notice A constant for the calculation of the weeks left of a vesting period
/// @dev Representing a week in seconds - 1
/**
* @notice A constant for the calculation of the weeks left of a vesting period
* @dev Representing a week in seconds - 1
*/
uint256 private constant WEEK_MINUS_SECOND = 604799;

/// @notice The vesting positions for every validator
mapping(address => VestingPosition) public positions;
/// @notice The vesting positions for every delegator.
/// @dev Validator => Delegator => VestingPosition
/**
* @notice The vesting positions for every delegator
* @dev Validator => Delegator => VestingPosition
*/
mapping(address => mapping(address => VestingPosition)) public delegationPositions;
/// @notice Keeps the history of the RPS for the validators
/// @dev This is used to keep the history RPS in order to calculate properly the rewards
/**
* @notice Keeps the history of the RPS for the validators
* @dev This is used to keep the history RPS in order to calculate properly the rewards
*/
mapping(address => mapping(uint256 => RPS)) public historyRPS;
/// @notice Keeps the rewards history of the validators
mapping(address => ValRewardHistory[]) public valRewardHistory;
/// @notice Historical Validator Delegation Pool's Params per delegator
/// @dev Validator => Delegator => Top-up data
/**
* @notice Historical Validator Delegation Pool's Params per delegator
* @dev Validator => Delegator => Top-up data
*/
mapping(address => mapping(address => DelegationPoolParams[])) public delegationPoolParamsHistory;
/// @dev Keep the account parameters before the top-up, so we can separately calculate the rewards made before a top-up is made
/// @dev This is because we need to apply the RSI bonus to the rewards made before the top-up
/// @dev and not apply the RSI bonus to the rewards made after the top-up
/**
* @dev Keep the account parameters before the top-up, so we can separately calculate the rewards made before a top-up is made
* This is because we need to apply the RSI bonus to the rewards made before the top-up
* and not apply the RSI bonus to the rewards made after the top-up
*/
mapping(address => mapping(address => RewardParams)) public beforeTopUpParams;

// _______________ External functions _______________
Expand Down Expand Up @@ -140,6 +150,12 @@ abstract contract Vesting is APR {
historyRPS[validator][epochNumber] = RPS({value: uint192(rewardPerShare), timestamp: uint64(block.timestamp)});
}

/**
* @notice Function that applies the custom factors - base APR, vest bonus and rsi bonus
* @dev Denominator is used because we should work with floating-point numbers
* @param reward index The reward to which we gonna apply the custom APR
* @dev The reward with the applied APR
*/
function _applyCustomReward(
VestingPosition memory position,
uint256 reward,
Expand Down
6 changes: 4 additions & 2 deletions contracts/ValidatorSet/ValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ contract ValidatorSet is ValidatorSetBase, System, AccessControl, PowerExponent,
emit NewEpoch(id, epoch.startBlock, epoch.endBlock, epoch.epochRoot);
}

/// @notice Get the validator by its address
/// @param validatorAddress address
/**
* @notice Get the validator by its address
* @param validatorAddress address
*/
function getValidator(
address validatorAddress
)
Expand Down
32 changes: 27 additions & 5 deletions docs/RewardPool/IRewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

## Methods

### calculateDelegatePositionPenalty
### calculatePositionPenalty

```solidity
function calculateDelegatePositionPenalty(address validator, address delegator, uint256 amount) external view returns (uint256 penalty, uint256 reward)
function calculatePositionPenalty(address validator, address delegator, uint256 amount) external view returns (uint256 penalty)
```

Returns the penalty and reward that will be burned, if vested delegate position is active
Returns the penalty that will taken from the delegator, if the position is still active



Expand All @@ -33,7 +33,6 @@ Returns the penalty and reward that will be burned, if vested delegate position
| Name | Type | Description |
|---|---|---|
| penalty | uint256 | for the delegator |
| reward | uint256 | of the delegator |

### calculateStakePositionPenalty

Expand All @@ -59,6 +58,29 @@ Returns the penalty and reward that will be burned, if vested stake position is
| penalty | uint256 | for the staker |
| reward | uint256 | of the staker |

### calculateTotalPositionReward

```solidity
function calculateTotalPositionReward(address validator, address delegator) external view returns (uint256 reward)
```

Returns the total reward that is generated for a position



#### Parameters

| Name | Type | Description |
|---|---|---|
| validator | address | The address of the validator |
| delegator | address | The address of the delegator |

#### Returns

| Name | Type | Description |
|---|---|---|
| reward | uint256 | for the delegator |

### claimDelegatorReward

```solidity
Expand Down Expand Up @@ -212,7 +234,7 @@ Gets delegators's unclaimed rewards including custom rewards
function getRawDelegatorReward(address validator, address delegator) external view returns (uint256)
```

Gets delegators's unclaimed rewards without custom rewards
Gets delegator's unclaimed rewards without custom rewards



Expand Down
36 changes: 29 additions & 7 deletions docs/RewardPool/RewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ function beforeTopUpParams(address, address) external view returns (uint256 rewa
| balance | uint256 | undefined |
| correction | int256 | undefined |

### calculateDelegatePositionPenalty
### calculatePositionPenalty

```solidity
function calculateDelegatePositionPenalty(address validator, address delegator, uint256 amount) external view returns (uint256 penalty, uint256 reward)
function calculatePositionPenalty(address validator, address delegator, uint256 amount) external view returns (uint256 penalty)
```

Returns the penalty and reward that will be burned, if vested delegate position is active
Returns the penalty that will taken from the delegator, if the position is still active



Expand All @@ -318,7 +318,6 @@ Returns the penalty and reward that will be burned, if vested delegate position
| Name | Type | Description |
|---|---|---|
| penalty | uint256 | for the delegator |
| reward | uint256 | of the delegator |

### calculateStakePositionPenalty

Expand All @@ -344,6 +343,29 @@ Returns the penalty and reward that will be burned, if vested stake position is
| penalty | uint256 | for the staker |
| reward | uint256 | of the staker |

### calculateTotalPositionReward

```solidity
function calculateTotalPositionReward(address validator, address delegator) external view returns (uint256 reward)
```

Returns the total reward that is generated for a position



#### Parameters

| Name | Type | Description |
|---|---|---|
| validator | address | The address of the validator |
| delegator | address | The address of the delegator |

#### Returns

| Name | Type | Description |
|---|---|---|
| reward | uint256 | for the delegator |

### claimDelegatorReward

```solidity
Expand Down Expand Up @@ -569,7 +591,7 @@ Gets delegators's history of the delegated position
### getDelegatorPositionReward

```solidity
function getDelegatorPositionReward(address validator, address delegator, uint256 epochNumber, uint256 topUpIndex) external view returns (uint256)
function getDelegatorPositionReward(address validator, address delegator, uint256 epochNumber, uint256 topUpIndex) external view returns (uint256 sumReward)
```

Gets delegators's unclaimed rewards including custom rewards for a position
Expand All @@ -589,7 +611,7 @@ Gets delegators's unclaimed rewards including custom rewards for a position

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | Delegator's unclaimed rewards with validator (in MATIC wei) |
| sumReward | uint256 | Delegator's unclaimed rewards with validator (in MATIC wei) |

### getDelegatorReward

Expand Down Expand Up @@ -701,7 +723,7 @@ function getRPSValues(address validator, uint256 startEpoch, uint256 endEpoch) e
function getRawDelegatorReward(address validator, address delegator) external view returns (uint256)
```

Gets delegators's unclaimed rewards without custom rewards
Gets delegator's unclaimed rewards without custom rewards



Expand Down
Loading
Loading