Skip to content

Commit

Permalink
124 add functions in the contracts that allow minstake and mindelegat…
Browse files Browse the repository at this point in the history
…ion to be changeable by the governor (#11)

* add changing on minStake/Delegate + tests

* fix fixture on RewardPool and re-organize

* Apply PR requested changes

* move errors

* update errors internal styling

* update md file

* fix md
  • Loading branch information
SamBorisov authored May 10, 2024
1 parent 5c1c312 commit 5420121
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 26 deletions.
7 changes: 7 additions & 0 deletions contracts/RewardPool/IRewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,11 @@ interface IRewardPool {
* @return Amount delegated (in MATIC wei)
*/
function totalDelegationOf(address validator) external view returns (uint256);

/**
* @dev Should be called only by the Governance.
* @notice Changes the minDelegationAmount
* @param newMinDelegation New minimum delegation amount
*/
function changeMinDelegation(uint256 newMinDelegation) external;
}
25 changes: 20 additions & 5 deletions contracts/RewardPool/modules/DelegationRewards.sol
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "./../RewardPoolBase.sol";
import "./Vesting.sol";
import "./../../common/Errors.sol";
import "./RewardsWithdrawal.sol";

import "./../RewardPoolBase.sol";
import "./../libs/DelegationPoolLib.sol";
import "./../libs/VestingPositionLib.sol";

import "./../../common/Errors.sol";

abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawal {
using DelegationPoolLib for DelegationPool;
using VestingPositionLib for VestingPosition;

/// @notice A constant for the minimum delegation limit
uint256 public constant MIN_DELEGATION_LIMIT = 1 ether;
/// @notice Keeps the delegation pools
mapping(address => DelegationPool) public delegationPools;
// @note maybe this must be part of the ValidatorSet
/// @notice The minimum delegation amount to be delegated
uint256 public minDelegation;

error InvalidMinDelegation();

// _______________ Initializer _______________

function __DelegationRewards_init(uint256 newMinDelegation) internal onlyInitializing {
__DelegationRewards_init_unchained(newMinDelegation);
}

function __DelegationRewards_init_unchained(uint256 newMinDelegation) internal onlyInitializing {
// TODO: all requre statements should be replaced with Error
require(newMinDelegation >= 1 ether, "INVALID_MIN_DELEGATION");
minDelegation = newMinDelegation;
_changeMinDelegation(newMinDelegation);
}

// _______________ External functions _______________
Expand Down Expand Up @@ -368,6 +371,13 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
emit PositionRewardClaimed(msg.sender, validator, sumReward);
}

/**
* @inheritdoc IRewardPool
*/
function changeMinDelegation(uint256 newMinDelegation) external onlyRole(DEFAULT_ADMIN_ROLE) {
_changeMinDelegation(newMinDelegation);
}

// _______________ Public functions _______________

/**
Expand Down Expand Up @@ -403,6 +413,11 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa

// _______________ Private functions _______________

function _changeMinDelegation(uint256 newMinDelegation) private {
if (newMinDelegation < MIN_DELEGATION_LIMIT) revert InvalidMinDelegation();
minDelegation = newMinDelegation;
}

function _noRewardConditions(VestingPosition memory position) private view returns (bool) {
// If still unused position, there is no reward
if (position.start == 0) {
Expand Down
10 changes: 10 additions & 0 deletions contracts/ValidatorSet/modules/Staking/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ interface IStaking {
event Staked(address indexed validator, uint256 amount);
event Unstaked(address indexed validator, uint256 amount);

error InvalidCommission(uint256 commission);
error InvalidMinStake();

/**
* @notice Sets commission for validator.
* @param newCommission New commission (100 = 100%)
Expand Down Expand Up @@ -37,4 +40,11 @@ interface IStaking {
* @param amount Amount to unstake
*/
function unstake(uint256 amount) external;

/**
* @dev Should be called by the Governance.
* @notice Changes minimum stake required for validators.
* @param newMinStake New minimum stake
*/
function changeMinStake(uint256 newMinStake) external;
}
18 changes: 16 additions & 2 deletions contracts/ValidatorSet/modules/Staking/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ abstract contract Staking is
LiquidStaking,
StateSyncer
{
/// @notice A constant for the minimum stake limit
uint256 public constant MIN_STAKE_LIMIT = 1 ether;
/// @notice A constant for the maximum comission a validator can receive from the delegator's rewards
uint256 public constant MAX_COMMISSION = 100;
/// @notice A state variable to keep the minimum amount of stake
Expand All @@ -38,8 +40,8 @@ abstract contract Staking is
}

function __Staking_init_unchained(uint256 newMinStake) internal onlyInitializing {
if (newMinStake < 1 ether) revert InvalidMinStake(newMinStake);
minStake = newMinStake;
_changeMinStake(newMinStake);

}

// _______________ External functions _______________
Expand Down Expand Up @@ -91,6 +93,13 @@ abstract contract Staking is
emit Unstaked(msg.sender, amount);
}

/**
* @inheritdoc IStaking
*/
function changeMinStake(uint256 newMinStake) external onlyOwner {
_changeMinStake(newMinStake);
}

// _______________ Internal functions _______________

function _register(
Expand Down Expand Up @@ -145,6 +154,11 @@ abstract contract Staking is
emit CommissionUpdated(validator, newCommission);
}

function _changeMinStake(uint256 newMinStake) private {
if (newMinStake < MIN_STAKE_LIMIT) revert InvalidMinStake();
minStake = newMinStake;
}

// slither-disable-next-line unused-state,naming-convention
uint256[50] private __gap;
}
2 changes: 0 additions & 2 deletions contracts/common/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ error DelegateRequirement(string src, string msg);
error InvalidSignature(address signer);
error ZeroAddress();
error SendFailed();
error InvalidCommission(uint256 commission);
error InvalidMinStake(uint256 minStake);
16 changes: 16 additions & 0 deletions docs/RewardPool/IRewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ Returns the total reward that is generated for a position
|---|---|---|
| reward | uint256 | for the delegator |

### changeMinDelegation

```solidity
function changeMinDelegation(uint256 newMinDelegation) external nonpayable
```

Changes the minDelegationAmount

*Should be called only by the Governance.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newMinDelegation | uint256 | New minimum delegation amount |

### claimDelegatorReward

```solidity
Expand Down
44 changes: 44 additions & 0 deletions docs/RewardPool/RewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ function MAX_RSI_BONUS() external view returns (uint256)



#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

### MIN_DELEGATION_LIMIT

```solidity
function MIN_DELEGATION_LIMIT() external view returns (uint256)
```

A constant for the minimum delegation limit




#### Returns

| Name | Type | Description |
Expand Down Expand Up @@ -383,6 +400,22 @@ Returns the total reward that is generated for a position
|---|---|---|
| reward | uint256 | for the delegator |

### changeMinDelegation

```solidity
function changeMinDelegation(uint256 newMinDelegation) external nonpayable
```

Changes the minDelegationAmount

*Should be called only by the Governance.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newMinDelegation | uint256 | New minimum delegation amount |

### claimDelegatorReward

```solidity
Expand Down Expand Up @@ -1735,6 +1768,17 @@ error DelegateRequirement(string src, string msg)
| src | string | undefined |
| msg | string | undefined |

### InvalidMinDelegation

```solidity
error InvalidMinDelegation()
```






### InvalidRSI

```solidity
Expand Down
16 changes: 16 additions & 0 deletions docs/RewardPool/RewardPoolBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ Returns the total reward that is generated for a position
|---|---|---|
| reward | uint256 | for the delegator |

### changeMinDelegation

```solidity
function changeMinDelegation(uint256 newMinDelegation) external nonpayable
```

Changes the minDelegationAmount

*Should be called only by the Governance.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newMinDelegation | uint256 | New minimum delegation amount |

### claimDelegatorReward

```solidity
Expand Down
44 changes: 44 additions & 0 deletions docs/RewardPool/modules/DelegationRewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ function MAX_RSI_BONUS() external view returns (uint256)



#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

### MIN_DELEGATION_LIMIT

```solidity
function MIN_DELEGATION_LIMIT() external view returns (uint256)
```

A constant for the minimum delegation limit




#### Returns

| Name | Type | Description |
Expand Down Expand Up @@ -281,6 +298,22 @@ Returns the total reward that is generated for a position
|---|---|---|
| reward | uint256 | for the delegator |

### changeMinDelegation

```solidity
function changeMinDelegation(uint256 newMinDelegation) external nonpayable
```

Changes the minDelegationAmount

*Should be called only by the Governance.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newMinDelegation | uint256 | New minimum delegation amount |

### claimDelegatorReward

```solidity
Expand Down Expand Up @@ -1525,6 +1558,17 @@ error DelegateRequirement(string src, string msg)
| src | string | undefined |
| msg | string | undefined |

### InvalidMinDelegation

```solidity
error InvalidMinDelegation()
```






### InvalidRSI

```solidity
Expand Down
16 changes: 16 additions & 0 deletions docs/RewardPool/modules/StakingRewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ Returns the total reward that is generated for a position
|---|---|---|
| reward | uint256 | for the delegator |

### changeMinDelegation

```solidity
function changeMinDelegation(uint256 newMinDelegation) external nonpayable
```

Changes the minDelegationAmount

*Should be called only by the Governance.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newMinDelegation | uint256 | New minimum delegation amount |

### claimDelegatorReward

```solidity
Expand Down
Loading

0 comments on commit 5420121

Please sign in to comment.