# \[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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rentible.gitbook.io/rentible.io/real-world-assets-tokenization/technical-documentation/documentation-yield-distributor-smart-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
