Contract Overview

This contract is called LP Farming. It allows users to stake LP Token and receive reward tokens (which are a separate ERC-20 token) in exchange. The contract tracks each user's staked LP token balance and reward token balance separately, and enables them to withdraw their staked LP token and earned reward tokens at any time.

Values

  1. lpToken: IERC20 public lpToken: This variable represents the ERC20 token contract for the LP tokens used in the farming contract. It allows interaction with the LP token contract, such as transferring tokens.

  2. rewardToken: IERC20 public rewardToken: This variable represents the ERC20 token contract for the reward tokens distributed in the farming contract. It allows interaction with the reward token contract, such as transferring tokens.

  3. owner: address public owner: This variable stores the address of the contract owner, who has certain privileges like starting the farming period and withdrawing LP tokens.

  4. totalRewards: uint256 public totalRewards: This variable represents the total amount of rewards available for distribution among the stakers in the farming contract.

  5. duration: uint256 public duration: This variable stores the duration of the farming period in seconds. It is calculated as the difference between the start and end times.

  6. startTime: uint256 public startTime: This variable stores the starting time of the farming period. Stakers can start depositing LP tokens and earning rewards after this time.

  7. endTime: uint256 public endTime: This variable stores the ending time of the farming period. After this time, stakers can no longer deposit LP tokens or earn rewards.

  8. rewardsPerTokenStored: uint256 public rewardsPerTokenStored: This variable represents the current rewards earned per LP token. It is updated whenever there is a change in the rewards distribution.

  9. userRewardPerTokenPaid: mapping(address => uint256) public userRewardPerTokenPaid: This mapping keeps track of the rewards per token already paid to each staker. It is used to calculate the earned rewards for individual stakers.

  10. rewards: mapping(address => uint256) public rewards: This mapping stores the total rewards earned by each staker. It keeps track of the accumulated rewards for each staker's deposited LP tokens.

  11. lastUpdateTime: uint256 public lastUpdateTime: This variable stores the timestamp of the last update to the rewards distribution. It is used to calculate the rewards earned since the last update.

  12. totalDepositAmount: uint256 public totalDepositAmount: This variable represents the total amount of LP tokens deposited in the farming contract by all stakers.

  13. depositedAmount: mapping(address => uint256) public depositedAmount: This mapping keeps track of the deposited LP token amount for each staker. It stores the amount of LP tokens deposited by individual stakers.

  14. claimedRewards: mapping(address => uint256) public claimedRewards: This mapping stores the total rewards claimed by each staker. It keeps track of the accumulated rewards claimed by individual stakers.

  15. totalClaimed: uint256 public totalClaimed: This variable represents the total amount of rewards claimed by all stakers in the farming contract.

  16. PRECISION: uint256 public constant PRECISION: This variable represents the precision factor used in calculations. It is set to 10^18 to standardize the precision of calculations.

  17. lpTokenDecimal: uint256 public lpTokenDecimal: This variable stores the number of decimal places for the LP token. It is used to convert LP token amounts to the standard precision.

  18. rewardTokenDecimal: uint256 public rewardTokenDecimal: This variable stores the number of decimal places for the reward token. It is used to convert reward amounts to the standard precision.

These variables store important contract state information, including timing parameters, reward calculations, staker-specific data, and contract-related metadata.

Functions

  1. timing(uint256 _startTime, uint256 _endTime) external nonReentrant: This function allows the contract owner to update the start and end times of the farming period. It sets the new timing parameters and recalculates the rewards based on the updated period.

  2. deposit(uint256 _amount) external nonReentrant: Users can call this function to deposit their LP tokens into the farming contract. The specified amount is transferred from the user's account to the contract, and their deposited amount is recorded for reward calculations.

  3. claimRewards() external nonReentrant: Stakers can claim their earned rewards using this function. The rewards are transferred from the contract to the staker's account, and the claimed rewards are recorded for tracking purposes.

  4. withdraw(uint256 _amount) external nonReentrant: Stakers can withdraw their deposited LP tokens from the contract using this function. The specified amount is transferred from the contract back to the staker's account.

  5. withdrawLPSentInError() external nonReentrant: This function is available only for the contract owner. It allows the owner to withdraw any LP tokens that were mistakenly sent to the contract. The LP tokens are transferred back to the owner's account.

  6. calculateRewards(uint256 _amount) external view returns (uint256): This view function calculates the rewards that would be earned for a specified amount of deposited LP tokens. It considers the total deposit amount, total rewards, and the proportionate share of the specified amount.

  7. rewardsPerToken() public view returns (uint256): This view function calculates the rewards per token. It takes into account the total deposit amount, total rewards, and the time elapsed since the last update to determine the rewards earned per LP token.

  8. earned(address _account) public view returns (uint256): This view function calculates the total earned rewards for a specific account. It considers the rewards per token, the user's proportionate share, and the rewards already claimed.

  9. lastTimeRewardApplicable() internal view returns (uint256): This internal view function returns the last applicable time for rewards. It checks if the current block timestamp is before the end time of the farming period and returns the smaller of the two values.

  10. getLPValueUniswap() external view returns (uint256): This view function calculates the value of LP tokens in the Uniswap liquidity pool. It considers the token balance and the total supply of LP tokens to determine the value.

  11. totalLPDepositValue() external view returns (uint256): This view function calculates the total value of LP deposits in the farming contract. It multiplies the value of an LP token in Uniswap by the total deposit amount.

  12. getAPR() external view returns (uint256): This view function calculates the Annual Percentage Rate (APR) for the farming contract. It considers the total LP deposit value and the total rewards to determine the APR.

Events

  1. event RewardClaimed(address indexed _account, uint256 _reward): This event is emitted when a staker claims their earned rewards. It includes the staker's account address and the amount of rewards claimed.

  2. event LPTokenWithdrawn(address indexed _owner, uint256 _amount): This event is emitted when the contract owner withdraws LP tokens from the contract. It includes the owner's address and the amount of LP tokens withdrawn.

  3. event Deposit(address indexed _account, uint256 _amount): This event is emitted when a staker deposits LP tokens into the farming contract. It includes the staker's account address and the amount of LP tokens deposited.

  4. event Withdraw(address indexed _account, uint256 _amount): This event is emitted when a staker withdraws their deposited LP tokens from the farming contract. It includes the staker's account address and the amount of LP tokens withdrawn.

These events provide important information about the state changes and actions performed within the farming contract. They can be used by external systems or UIs to track and monitor the contract's activities and provide relevant notifications to stakeholders.

Last updated