# Rewards

The Rewards contract is a key piece in the liquid staking application, providing a way to distribute rewards to protocol participants, handling protocol fees, and managing checkpoints for efficient reward calculation. This contract is also the withdrawal address of the protocol's validators: where all the rewards and unstakes from the protocol's validators get sent to.&#x20;

The *Rewards* contract interacts with various other contracts including:

1. [*StakedLyxToken*](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/merkle-distributor): Tracks the staked tokens.
2. [*Oracles*](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/oracles): Responsible for updating the total rewards in the Rewards contract.
3. [*MerkleDistributor*](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/merkle-distributor)*:* Distributes the rewards to the protocol's participants.
4. [*FeesEscrow*](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/feesescrow): Handles the protocol's execution layer fees.
5. [*Pool*](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/pool)*:* Handles incoming LYX.

### Key features

The *Rewards* contract tracks the total rewards earned by the staking protocol and the total amount that has been cashed out. It also calculates the reward per token for user reward calculation.

#### **Checkpointing**

To optimize gas usage and enhance scalability, the contract uses a checkpoint system that keeps track of an account's rewards at a particular point in time. The reward per token is stored in each checkpoint, along with the reward value, in a single memory slot. This allows the Rewards contract to calculate the user's reward.

```solidity
struct Checkpoint {
    uint128 reward;
    uint128 rewardPerToken;
}
```

#### **Fee Handling**

The contract calculates protocol fees and handles the distribution of these fees. It allows the protocol's admin to set the fee recipient address and fee percentage.

#### **Oracles**

An oracle system is in place that updates the total rewards in the Rewards contract.

#### **Reward disabling**

The contract provides an option to disable rewards for an account. The state is toggled by calling the `setRewardsDisabled` function. Rewards of addresses with rewards disabled accrue in the distributor principal, the balance of the [Merkle Distributor contract](https://docs.leequid.io/leequid/leequid-in-depth/smart-contracts/merkle-distributor), responsible for the token distribution of rewards calculated off-chain.

#### **Auto-compounding**

The contract stores a list of addresses who explicitly granted permission for rewards to be staked on their behalf and provides an interface to effectively re-stake these rewards. This operation can be done in batch and it will call the Pool contract's, transferring LYX to it.
