Skip to content

Commit

Permalink
optimize rsi (#4)
Browse files Browse the repository at this point in the history
* optimize rsi

create new custom error for InvalidRSI;
ensure that the new RSI is between the range - 0% to 50%;
create the max rsi as a constant instead of a function;
set initial rsi to 0 and also if one tries to set rsi lower than 10000, then set it to 0;
set initial rsi bonus on stake position;
adapt tests and create new ones for the APR;
  • Loading branch information
Vitomir2 authored Apr 26, 2024
1 parent c3f016e commit 5e7e64b
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 243 deletions.
29 changes: 12 additions & 17 deletions contracts/RewardPool/modules/APR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ 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 {
error InvalidRSI();

uint256 public constant INITIAL_BASE_APR = 500;
uint256 public constant INITIAL_MACRO_FACTOR = 7500;
uint256 public constant INITIAL_RSI_BONUS = 10000;
uint256 public constant MIN_RSI_BONUS = 10000;
uint256 public constant MAX_RSI_BONUS = 17000;
uint256 public constant DENOMINATOR = 10000;
uint256 public constant EPOCHS_YEAR = 31500;
bytes32 public constant MANAGER_ROLE = keccak256("manager_role");
Expand All @@ -20,7 +25,6 @@ contract APR is Initializable, AccessControl {
function __APR_init(address manager) internal onlyInitializing {
base = INITIAL_BASE_APR;
macroFactor = INITIAL_MACRO_FACTOR;
rsi = INITIAL_RSI_BONUS;

initializeVestingBonus();

Expand All @@ -41,36 +45,27 @@ contract APR is Initializable, AccessControl {
}

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

rsi = newRSI;
}
if (newRSI > MAX_RSI_BONUS) revert InvalidRSI();

function getDefaultRSI() public pure returns (uint256 nominator) {
return DENOMINATOR;
}
if (newRSI < MIN_RSI_BONUS) newRSI = 0;

// TODO: Ensure the fetched from oracles value is smaller or equal to this
function getMaxRSI() public pure returns (uint256 nominator) {
return 15000;
rsi = newRSI;
}

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 rsiBonusFactor = getMaxRSI();
uint256 vestBonus = getVestingBonus(52);

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

function applyMaxReward(uint256 reward) public view returns (uint256) {
// TODO: Consider setting max base
// max vesting bonus is 52 weeks
uint256 maxRSI = getMaxRSI();
uint256 vestBonus = getVestingBonus(52);

uint256 bonus = (base + vestBonus) * maxRSI;
uint256 bonus = (base + vestBonus) * MAX_RSI_BONUS;

return ((reward * bonus) / (10000 * 10000)) / EPOCHS_YEAR;
}
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -163,7 +163,7 @@ abstract contract Vesting is APR {
) internal pure returns (uint256) {
uint256 bonus = (position.base + position.vestBonus);
uint256 divider = DENOMINATOR;
if (rsi) {
if (rsi && position.rsiBonus != 0) {
bonus = bonus * position.rsiBonus;
divider *= divider;
}
Expand Down
72 changes: 33 additions & 39 deletions docs/RewardPool/RewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,27 @@ function INITIAL_MACRO_FACTOR() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined |

### INITIAL_RSI_BONUS
### MANAGER_ROLE

```solidity
function MANAGER_ROLE() external view returns (bytes32)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | bytes32 | undefined |

### MAX_RSI_BONUS

```solidity
function INITIAL_RSI_BONUS() external view returns (uint256)
function MAX_RSI_BONUS() external view returns (uint256)
```


Expand All @@ -112,10 +129,10 @@ function INITIAL_RSI_BONUS() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined |

### MANAGER_ROLE
### MIN_RSI_BONUS

```solidity
function MANAGER_ROLE() external view returns (bytes32)
function MIN_RSI_BONUS() external view returns (uint256)
```


Expand All @@ -127,7 +144,7 @@ function MANAGER_ROLE() external view returns (bytes32)

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

### NATIVE_TOKEN_CONTRACT

Expand Down Expand Up @@ -548,23 +565,6 @@ function distributeRewardsFor(uint256 epochId, Uptime[] uptime, uint256 epochSiz
| uptime | Uptime[] | undefined |
| epochSize | uint256 | undefined |

### getDefaultRSI

```solidity
function getDefaultRSI() external pure returns (uint256 nominator)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| nominator | uint256 | undefined |

### getDelegationPoolParamsHistory

```solidity
Expand Down Expand Up @@ -676,23 +676,6 @@ function getMaxAPR() external view returns (uint256 nominator, uint256 denominat
| nominator | uint256 | undefined |
| denominator | uint256 | undefined |

### getMaxRSI

```solidity
function getMaxRSI() external pure returns (uint256 nominator)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| nominator | uint256 | undefined |

### getRPSValues

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

### InvalidRSI

```solidity
error InvalidRSI()
```






### NoTokensDelegated

```solidity
Expand Down
67 changes: 32 additions & 35 deletions docs/RewardPool/modules/APR.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ function INITIAL_MACRO_FACTOR() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined |

### INITIAL_RSI_BONUS
### MANAGER_ROLE

```solidity
function INITIAL_RSI_BONUS() external view returns (uint256)
function MANAGER_ROLE() external view returns (bytes32)
```


Expand All @@ -110,12 +110,12 @@ function INITIAL_RSI_BONUS() external view returns (uint256)

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

### MANAGER_ROLE
### MAX_RSI_BONUS

```solidity
function MANAGER_ROLE() external view returns (bytes32)
function MAX_RSI_BONUS() external view returns (uint256)
```


Expand All @@ -127,51 +127,51 @@ function MANAGER_ROLE() external view returns (bytes32)

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

### applyMaxReward
### MIN_RSI_BONUS

```solidity
function applyMaxReward(uint256 reward) external view returns (uint256)
function MIN_RSI_BONUS() external view returns (uint256)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| reward | uint256 | undefined |

#### Returns

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

### base
### applyMaxReward

```solidity
function base() external view returns (uint256)
function applyMaxReward(uint256 reward) external view returns (uint256)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| reward | uint256 | undefined |

#### Returns

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

### getDefaultRSI
### base

```solidity
function getDefaultRSI() external pure returns (uint256 nominator)
function base() external view returns (uint256)
```


Expand All @@ -183,7 +183,7 @@ function getDefaultRSI() external pure returns (uint256 nominator)

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

### getEpochMaxReward

Expand Down Expand Up @@ -225,23 +225,6 @@ function getMaxAPR() external view returns (uint256 nominator, uint256 denominat
| nominator | uint256 | undefined |
| denominator | uint256 | undefined |

### getMaxRSI

```solidity
function getMaxRSI() external pure returns (uint256 nominator)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| nominator | uint256 | undefined |

### getRoleAdmin

```solidity
Expand Down Expand Up @@ -562,3 +545,17 @@ event RoleRevoked(bytes32 indexed role, address indexed account, address indexed



## Errors

### InvalidRSI

```solidity
error InvalidRSI()
```







Loading

0 comments on commit 5e7e64b

Please sign in to comment.