> For the complete documentation index, see [llms.txt](https://docs.sperax.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sperax.io/sperax-farms-protocol/technical-documents/smart-contracts/rewarder/rewarder.md).

# Rewarder

[Git Source](https://github.com/Sperax/Demeter-Protocol/blob/cc93b9106874316d6dd016cdace652e2ca4ef8e1/contracts/rewarder/Rewarder.sol)

**Inherits:** IRewarder, OwnableUpgradeable, ReentrancyGuardUpgradeable

**Author:** Sperax Foundation.

This contract tracks farms, their APR and other data for a specific reward token.

*Farms for UniV3 pools using Rewarder contract must have a minimum observationCardinality of 20. It can be updated by calling increaseObservationCardinalityNext function on the pool.*

## State Variables

### MAX\_PERCENTAGE

```solidity
uint256 public constant MAX_PERCENTAGE = 1e4;
```

### APR\_PRECISION

```solidity
uint256 public constant APR_PRECISION = 1e8;
```

### REWARD\_PERIOD

```solidity
uint256 public constant REWARD_PERIOD = 1 weeks;
```

### DENOMINATOR

```solidity
uint256 public constant DENOMINATOR = 100;
```

### ONE\_YEAR

```solidity
uint256 public constant ONE_YEAR = 365 days;
```

### REWARD\_TOKEN

```solidity
address public REWARD_TOKEN;
```

### REWARD\_TOKEN\_DECIMALS

```solidity
uint8 public REWARD_TOKEN_DECIMALS;
```

### totalRewardRate

```solidity
uint256 public totalRewardRate;
```

### rewarderFactory

```solidity
address public rewarderFactory;
```

### calibrationRestricted

```solidity
mapping(address => bool) public calibrationRestricted;
```

### farmRewardConfigs

```solidity
mapping(address => FarmRewardConfig) internal farmRewardConfigs;
```

### \_decimals

```solidity
mapping(address => uint8) private _decimals;
```

## Functions

### constructor

```solidity
constructor();
```

### initialize

Initializer function of this contract.

```solidity
function initialize(address _rwdToken, address _oracle, address _admin) external initializer;
```

**Parameters**

| Name        | Type      | Description                              |
| ----------- | --------- | ---------------------------------------- |
| `_rwdToken` | `address` | Address of the reward token.             |
| `_oracle`   | `address` | Address of the USDs Master Price Oracle. |
| `_admin`    | `address` | Admin/ deployer of this contract.        |

### calibrateReward

Function to calibrate rewards for this rewarder's reward token for a farm.

*Calculates based on APR, caps based on maxRewardPerSec or balance rewards, sends and sets the rewardRate in the farm.*

```solidity
function calibrateReward(address _farm) external nonReentrant returns (uint256 rewardsToSend);
```

**Parameters**

| Name    | Type      | Description                                                     |
| ------- | --------- | --------------------------------------------------------------- |
| `_farm` | `address` | Address of the farm for which the rewards are to be calibrated. |

**Returns**

| Name            | Type      | Description                         |
| --------------- | --------- | ----------------------------------- |
| `rewardsToSend` | `uint256` | Rewards which are sent to the farm. |

### updateTokenManagerOfFarm

Function to update the token manager's address in the farm.

```solidity
function updateTokenManagerOfFarm(address _farm, address _newManager) external onlyOwner;
```

**Parameters**

| Name          | Type      | Description                                                 |
| ------------- | --------- | ----------------------------------------------------------- |
| `_farm`       | `address` | Farm's address in which the token manager is to be updated. |
| `_newManager` | `address` | Address of the new token manager.                           |

### recoverRewardFundsOfFarm

Function to recover reward funds from the farm.

```solidity
function recoverRewardFundsOfFarm(address _farm, uint256 _amount) external onlyOwner;
```

**Parameters**

| Name      | Type      | Description                                                |
| --------- | --------- | ---------------------------------------------------------- |
| `_farm`   | `address` | Farm's address from which reward funds is to be recovered. |
| `_amount` | `uint256` | Amount which is to be recovered.                           |

### updateAPR

Function to update APR.

```solidity
function updateAPR(address _farm, uint256 _apr) external onlyOwner nonReentrant;
```

**Parameters**

| Name    | Type      | Description           |
| ------- | --------- | --------------------- |
| `_farm` | `address` | Address of the farm.  |
| `_apr`  | `uint256` | APR in 1e8 precision. |

### toggleCalibrationRestriction

Function to toggle calibration restriction.

```solidity
function toggleCalibrationRestriction(address _farm) external onlyOwner;
```

**Parameters**

| Name    | Type      | Description                                                         |
| ------- | --------- | ------------------------------------------------------------------- |
| `_farm` | `address` | Address of farm for which calibration restriction is to be toggled. |

### recoverERC20

Function to recover ERC20 tokens from this contract.

```solidity
function recoverERC20(address _token, uint256 _amount) external onlyOwner;
```

**Parameters**

| Name      | Type      | Description           |
| --------- | --------- | --------------------- |
| `_token`  | `address` | Address of the token. |
| `_amount` | `uint256` | Amount of the tokens. |

### getTokenAmounts

Function to get token amounts value of underlying pool of the farm.

```solidity
function getTokenAmounts(address _farm) external view returns (address[] memory, uint256[] memory);
```

**Parameters**

| Name    | Type      | Description          |
| ------- | --------- | -------------------- |
| `_farm` | `address` | Address of the farm. |

**Returns**

| Name     | Type        | Description               |
| -------- | ----------- | ------------------------- |
| `<none>` | `address[]` | Array of token addresses. |
| `<none>` | `uint256[]` |                           |

### getFarmRewardConfig

Function to get reward config for a farm.

```solidity
function getFarmRewardConfig(address _farm) external view returns (FarmRewardConfig memory);
```

**Parameters**

| Name    | Type      | Description          |
| ------- | --------- | -------------------- |
| `_farm` | `address` | Address of the farm. |

**Returns**

| Name     | Type               | Description                          |
| -------- | ------------------ | ------------------------------------ |
| `<none>` | `FarmRewardConfig` | FarmRewardConfig Farm reward config. |

### rewardsEndTime

Function to calculate the time till which rewards are there for an LP.

```solidity
function rewardsEndTime(address _farm) external view returns (uint256 rewardsEndingOn);
```

**Parameters**

| Name    | Type      | Description                                                     |
| ------- | --------- | --------------------------------------------------------------- |
| `_farm` | `address` | Address of the farm for which the end time is to be calculated. |

**Returns**

| Name              | Type      | Description                                                                    |
| ----------------- | --------- | ------------------------------------------------------------------------------ |
| `rewardsEndingOn` | `uint256` | Timestamp in seconds till which the rewards are there in farm and in rewarder. |

### updateRewardConfig

Function to update the REWARD\_TOKEN configuration. This function calibrates reward so token manager must be updated to address of this contract in the farm.

```solidity
function updateRewardConfig(address _farm, FarmRewardConfigInput memory _rewardConfig) public onlyOwner nonReentrant;
```

**Parameters**

| Name            | Type                    | Description                                                |
| --------------- | ----------------------- | ---------------------------------------------------------- |
| `_farm`         | `address`               | Address of the farm for which the config is to be updated. |
| `_rewardConfig` | `FarmRewardConfigInput` | The config which is to be set.                             |

### \_initialize

Internal initialize function.

```solidity
function _initialize(address _rwdToken, address _oracle, address _admin, address _rewarderFactory) internal;
```

**Parameters**

| Name               | Type      | Description                              |
| ------------------ | --------- | ---------------------------------------- |
| `_rwdToken`        | `address` | Address of the reward token.             |
| `_oracle`          | `address` | Address of the USDs Master Price Oracle. |
| `_admin`           | `address` | Admin/ deployer of this contract.        |
| `_rewarderFactory` | `address` | Address of Rewarder factory contract.    |

### \_isConfigured

Function to check if the farm's reward is configured.

```solidity
function _isConfigured(address _farm) internal view;
```

**Parameters**

| Name    | Type      | Description          |
| ------- | --------- | -------------------- |
| `_farm` | `address` | Address of the farm. |

### \_getTokenAmounts

An internal function to get token amounts for the farm.

```solidity
function _getTokenAmounts(address _farm) internal view virtual returns (address[] memory, uint256[] memory);
```

**Parameters**

| Name    | Type      | Description          |
| ------- | --------- | -------------------- |
| `_farm` | `address` | Address of the farm. |

**Returns**

| Name     | Type        | Description               |
| -------- | ----------- | ------------------------- |
| `<none>` | `address[]` | Array of token addresses. |
| `<none>` | `uint256[]` | Array of token amounts.   |

### \_hasRewardToken

Function to check if the reward token of this contract is one of farm's reward token.

```solidity
function _hasRewardToken(address _farm) internal view virtual returns (bool);
```

**Parameters**

| Name    | Type      | Description          |
| ------- | --------- | -------------------- |
| `_farm` | `address` | Address of the farm. |

**Returns**

| Name     | Type   | Description                                                           |
| -------- | ------ | --------------------------------------------------------------------- |
| `<none>` | `bool` | If farm has one of the reward token as reward token of this contract. |

### \_validateNonZeroAddr

Validate address.

```solidity
function _validateNonZeroAddr(address _addr) internal pure;
```

**Parameters**

| Name    | Type      | Description              |
| ------- | --------- | ------------------------ |
| `_addr` | `address` | Address to be validated. |

### \_calibrateReward

```solidity
function _calibrateReward(address _farm) private returns (uint256 rewardsToSend);
```

### \_setRewardRate

Function to set reward rate in the farm.

```solidity
function _setRewardRate(address _farm, uint128 _rwdRate, uint256 _nonLockupRewardPer) private;
```

**Parameters**

| Name                  | Type      | Description                                          |
| --------------------- | --------- | ---------------------------------------------------- |
| `_farm`               | `address` | Address of the farm.                                 |
| `_rwdRate`            | `uint128` | Reward per second to be emitted.                     |
| `_nonLockupRewardPer` | `uint256` | Reward percentage to be allocated to no lockup fund. |

### \_adjustGlobalRewardRate

Function to adjust global rewards per second emitted for a reward token.

```solidity
function _adjustGlobalRewardRate(uint256 _oldRewardRate, uint256 _newRewardRate) private;
```

**Parameters**

| Name             | Type      | Description        |
| ---------------- | --------- | ------------------ |
| `_oldRewardRate` | `uint256` | Old emission rate. |
| `_newRewardRate` | `uint256` | New emission rate. |

### \_isValidFarm

Function to validate farm.

*It checks that the farm should implement getTokenAmounts and have REWARD\_TOKEN. as one of the reward tokens.*

```solidity
function _isValidFarm(address _farm, address[] memory _baseTokens) private returns (bool);
```

**Parameters**

| Name          | Type        | Description                          |
| ------------- | ----------- | ------------------------------------ |
| `_farm`       | `address`   | Address of the farm to be validated. |
| `_baseTokens` | `address[]` | Array of base tokens.                |

**Returns**

| Name     | Type   | Description                 |
| -------- | ------ | --------------------------- |
| `<none>` | `bool` | bool True if farm is valid. |

### \_hasBaseTokens

Function to check whether the base tokens are a subset of farm's assets.

*It handles repeated base tokens as well and pushes indexed in farmRewardConfigs.*

```solidity
function _hasBaseTokens(address _farm, address[] memory _baseTokens) private returns (bool);
```

**Parameters**

| Name          | Type        | Description                                                           |
| ------------- | ----------- | --------------------------------------------------------------------- |
| `_farm`       | `address`   | Address of the farm.                                                  |
| `_baseTokens` | `address[]` | Array of base token addresses to be considered for value calculation. |

**Returns**

| Name     | Type   | Description                                                                    |
| -------- | ------ | ------------------------------------------------------------------------------ |
| `<none>` | `bool` | hasBaseTokens True if baseTokens are non redundant and are a subset of assets. |

### \_normalizeAmount

Function to normalize asset amounts to be of precision REWARD\_TOKEN\_DECIMALS.

```solidity
function _normalizeAmount(address _token, uint256 _amount) private view returns (uint256);
```

**Parameters**

| Name      | Type      | Description                 |
| --------- | --------- | --------------------------- |
| `_token`  | `address` | Address of the asset token. |
| `_amount` | `uint256` | Amount of the token.        |

**Returns**

| Name     | Type      | Description                                           |
| -------- | --------- | ----------------------------------------------------- |
| `<none>` | `uint256` | Normalized amount of the token in \_desiredPrecision. |

### \_getPrice

Function to fetch and get the price of a token.

```solidity
function _getPrice(address _token, address _oracle) private view returns (IOracle.PriceData memory priceData);
```

**Parameters**

| Name      | Type      | Description                                     |
| --------- | --------- | ----------------------------------------------- |
| `_token`  | `address` | Token for which the the price is to be fetched. |
| `_oracle` | `address` | Address of the oracle contract.                 |

**Returns**

| Name        | Type                | Description              |
| ----------- | ------------------- | ------------------------ |
| `priceData` | `IOracle.PriceData` | Price data of the token. |

### \_validatePriceFeed

Function to validate price feed.

```solidity
function _validatePriceFeed(address _token, address _oracle) private view;
```

**Parameters**

| Name      | Type      | Description            |
| --------- | --------- | ---------------------- |
| `_token`  | `address` | Token to be validated. |
| `_oracle` | `address` | Address of the oracle. |

### \_validateRewardPer

Function to validate the no lockup fund's reward percentage.

```solidity
function _validateRewardPer(uint256 _percentage) private pure;
```

**Parameters**

| Name          | Type      | Description                                         |
| ------------- | --------- | --------------------------------------------------- |
| `_percentage` | `uint256` | No lockup fund's reward percentage to be validated. |
