Skip to content

Commit

Permalink
optimize rsi
Browse files Browse the repository at this point in the history
create new custom errors for the rsi;
ensure that the new RSI is between the range - 0% to 50%;
set initial rsi bonus on stake position;
adapt tests and create new ones for the APR;
  • Loading branch information
Vitomir2 committed Apr 23, 2024
1 parent 56f51e8 commit 39dbe5c
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 24 deletions.
11 changes: 8 additions & 3 deletions contracts/RewardPool/modules/APR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.8.17;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";

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

contract APR is Initializable, AccessControl {
uint256 public constant INITIAL_BASE_APR = 500;
uint256 public constant INITIAL_MACRO_FACTOR = 7500;
Expand Down Expand Up @@ -41,7 +43,10 @@ contract APR is Initializable, AccessControl {
}

function setRSI(uint256 newRSI) public onlyRole(MANAGER_ROLE) {
require(newRSI <= getMaxRSI(), "TOO_HIGH_RSI");
if (newRSI < INITIAL_RSI_BONUS) revert TooLowRSI(newRSI, INITIAL_RSI_BONUS);

uint256 maxRSI = getMaxRSI();
if (newRSI > getMaxRSI()) revert TooHighRSI(newRSI, maxRSI);

rsi = newRSI;
}
Expand All @@ -57,10 +62,10 @@ contract APR is Initializable, AccessControl {

function getMaxAPR() public view returns (uint256 nominator, uint256 denominator) {
// TODO: Base + vesting and RSI must return the max possible value here (implement max base)
uint256 vesting = getVestingBonus(52);
uint256 vestBonus = getVestingBonus(52);
uint256 rsiBonusFactor = getMaxRSI();

nominator = (base + vesting) * macroFactor * rsiBonusFactor;
nominator = (base + vestBonus) * macroFactor * rsiBonusFactor;
denominator = 10000 * 10000 * 10000;
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/RewardPool/modules/DelegationRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ abstract contract DelegationRewards is RewardPoolBase, Vesting, RewardsWithdrawa
uint256 newBalance = balance + amount;
if (newBalance < minDelegation) revert DelegateRequirement({src: "vesting", msg: "DELEGATION_TOO_LOW"});

// @note Potentially use memory variable to avoid get from storage twice
if (delegationPositions[validator][delegator].isMaturing()) {
VestingPosition memory position = delegationPositions[validator][delegator];
if (position.isMaturing()) {
revert DelegateRequirement({src: "vesting", msg: "POSITION_MATURING"});
}

if (delegationPositions[validator][delegator].isActive()) {
if (position.isActive()) {
revert DelegateRequirement({src: "vesting", msg: "POSITION_ACTIVE"});
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/RewardPool/modules/Vesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ abstract contract Vesting is APR {
uint256 durationIncrease = _calculateDurationIncrease(amount, oldBalance, duration);
positions[staker].duration = duration + durationIncrease;
positions[staker].end = positions[staker].end + durationIncrease;
positions[staker].rsiBonus = 0;
positions[staker].rsiBonus = rsi;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions contracts/common/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ error SendFailed();
error AlreadyRegistered(address validator);
error InvalidCommission(uint256 commission);
error InvalidMinStake(uint256 minStake);
error TooLowRSI(uint256 newRSI, uint256 minRSI);
error TooHighRSI(uint256 newRSI, uint256 maxRSI);
34 changes: 34 additions & 0 deletions docs/RewardPool/RewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,40 @@ error StakeRequirement(string src, string msg)
| src | string | undefined |
| msg | string | undefined |

### TooHighRSI

```solidity
error TooHighRSI(uint256 newRSI, uint256 maxRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| maxRSI | uint256 | undefined |

### TooLowRSI

```solidity
error TooLowRSI(uint256 newRSI, uint256 minRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| minRSI | uint256 | undefined |

### Unauthorized

```solidity
Expand Down
37 changes: 37 additions & 0 deletions docs/RewardPool/modules/APR.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,40 @@ event RoleRevoked(bytes32 indexed role, address indexed account, address indexed



## Errors

### TooHighRSI

```solidity
error TooHighRSI(uint256 newRSI, uint256 maxRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| maxRSI | uint256 | undefined |

### TooLowRSI

```solidity
error TooLowRSI(uint256 newRSI, uint256 minRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| minRSI | uint256 | undefined |


34 changes: 34 additions & 0 deletions docs/RewardPool/modules/DelegationRewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,40 @@ error StakeRequirement(string src, string msg)
| src | string | undefined |
| msg | string | undefined |

### TooHighRSI

```solidity
error TooHighRSI(uint256 newRSI, uint256 maxRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| maxRSI | uint256 | undefined |

### TooLowRSI

```solidity
error TooLowRSI(uint256 newRSI, uint256 minRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| minRSI | uint256 | undefined |

### Unauthorized

```solidity
Expand Down
34 changes: 34 additions & 0 deletions docs/RewardPool/modules/StakingRewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -1509,4 +1509,38 @@ error StakeRequirement(string src, string msg)
| src | string | undefined |
| msg | string | undefined |

### TooHighRSI

```solidity
error TooHighRSI(uint256 newRSI, uint256 maxRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| maxRSI | uint256 | undefined |

### TooLowRSI

```solidity
error TooLowRSI(uint256 newRSI, uint256 minRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| minRSI | uint256 | undefined |


37 changes: 37 additions & 0 deletions docs/RewardPool/modules/Vesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,40 @@ event RoleRevoked(bytes32 indexed role, address indexed account, address indexed



## Errors

### TooHighRSI

```solidity
error TooHighRSI(uint256 newRSI, uint256 maxRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| maxRSI | uint256 | undefined |

### TooLowRSI

```solidity
error TooLowRSI(uint256 newRSI, uint256 minRSI)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| newRSI | uint256 | undefined |
| minRSI | uint256 | undefined |


Loading

0 comments on commit 39dbe5c

Please sign in to comment.