> For the complete documentation index, see [llms.txt](https://rentible.gitbook.io/rentible.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rentible.gitbook.io/rentible.io/real-world-assets-tokenization/technical-documentation/documentation-yield-distributor-smart-contract.md).

# \[Documentation] Yield Distributor Smart Contract

This contract facilitates the distribution of yields to fractional owners of tokenized assets in a transparent and straightforward manner, akin to the functionality of staking contracts.

<figure><img src="/files/fIjeFneol3ZQmURBSxbZ" alt=""><figcaption></figcaption></figure>

**Contract Structure**

The contract consists of the following key components:

1. **State Variables:**
   * `stakingToken`: The ERC20 token that users will deposit to signal their fractional share.
   * `Yield_Distributor_Role`: Role assigned to the yield distributor, such as the property manager.
   * `_stakedBalances`: Mapping to track the staked balances of users.
   * `_rewardBalances`: Nested mapping to track reward balances by token.
   * `stakers`: Array containing addresses of all stakers.
   * `_totalStaked`: Total amount of tokens staked by all users.
2. **Events:**
   * `Staked`: Event emitted when a user stakes tokens.
   * `YieldDistributed`: Event emitted when yields are distributed.
   * `ClaimedYield`: Event emitted when a user claims their earned yields.
   * `Withdrawn`: Event emitted when a user withdraws their staked tokens.
3. **Constructor:**
   * Initializes the contract with the ERC20 token address and grants roles to the contract deployer.
4. **Public Functions:**
   * `stake`: Allows users to stake project tokens.
   * `distributeYield`: Allows yield distributors to distribute yields to stakers.
   * `claimYield`: Allows users to claim their earned yields.
   * `withdrawStake`: Allows users to withdraw their staked project tokens.
   * `stakedBalanceOf`: Retrieves the staked balance of a specific user.
   * `rewardBalanceOf`: Retrieves the reward balance of a specific user for a specific token.
   * `totalStaked`: Retrieves the total amount of tokens staked.

### Usage

#### Staking Project Tokens

Users can stake tokens by calling the `stake` function and providing the desired amount of tokens to stake.

#### Distributing Yields

Yield distributors can distribute yields to stakers by calling the `distributeYield` function and providing the amount of yields to distribute.

#### Claiming Yields

Users can claim their earned yields by calling the `claimYield` function.

#### Withdrawing Staked Tokens

Users can withdraw their staked tokens by calling the `withdrawStake` function.
