Farm

Git Source

Inherits: FarmStorage, OwnableUpgradeable, ReentrancyGuardUpgradeable, MulticallUpgradeable

Author: Sperax Foundation.

This contract contains the core logic for the Demeter farms.

Functions

constructor

constructor();

withdraw

Function to be called to withdraw deposit.

function withdraw(uint256 _depositId) external virtual;

Parameters

claimRewards

A function to be called by the depositor to claim rewards.

function claimRewards(uint256 _depositId) external;

Parameters

initiateCooldown

Function to be called to initiate cooldown for a staked deposit.

_depositId is corresponding to the user's deposit.

function initiateCooldown(uint256 _depositId) external nonReentrant;

Parameters

addRewards

Add rewards to the farm.

function addRewards(address _rwdToken, uint256 _amount) external nonReentrant;

Parameters

updateCooldownPeriod

Update the cooldown period.

function updateCooldownPeriod(uint256 _newCooldownPeriod) external onlyOwner;

Parameters

farmPauseSwitch

Pause / UnPause the farm.

function farmPauseSwitch(bool _isPaused) external onlyOwner;

Parameters

closeFarm

A function to explicitly close the farm.

Recovers remaining non accrued rewards.

function closeFarm() external onlyOwner nonReentrant;

recoverERC20

Recover erc20 tokens other than the reward tokens.

function recoverERC20(address _token) external onlyOwner nonReentrant;

Parameters

recoverRewardFunds

Get the remaining reward balance out of the farm.

Function recovers minOf(_amount, rewardsLeft).

function recoverRewardFunds(address _rwdToken, uint256 _amount) external nonReentrant;

Parameters

setRewardRate

Function to update reward params for a fund.

function setRewardRate(address _rwdToken, uint128[] memory _newRewardRates) external;

Parameters

updateRewardData

Transfer the tokenManagerRole to other user.

Only the existing tokenManager for a reward can call this function.

function updateRewardData(address _rwdToken, address _newTknManager) external;

Parameters

computeRewards

Function to compute the total accrued rewards for a deposit for each subscription.

function computeRewards(address _account, uint256 _depositId)
    external
    view
    virtual
    returns (uint256[][] memory rewards);

Parameters

Returns

getRewardFunds

Get the reward fund details.

function getRewardFunds() external view returns (RewardFund[] memory);

Returns

getRewardData

Get the reward details for specified reward token.

function getRewardData(address _rwdToken) external view returns (RewardData memory);

Parameters

Returns

getDepositInfo

Get deposit info for a deposit id.

function getDepositInfo(uint256 _depositId) external view returns (Deposit memory);

Parameters

Returns

getNumSubscriptions

Get number of subscriptions for an account.

function getNumSubscriptions(uint256 _depositId) external view returns (uint256);

Parameters

Returns

getSubscriptionInfo

Get subscription stats for a deposit.

function getSubscriptionInfo(uint256 _depositId, uint256 _subscriptionId) external view returns (Subscription memory);

Parameters

Returns

getRewardRates

Get reward rates for a rewardToken.

function getRewardRates(address _rwdToken) external view returns (uint256[] memory);

Parameters

Returns

getRewardFundInfo

Get farm reward fund info.

function getRewardFundInfo(uint8 _fundId) external view returns (RewardFund memory);

Parameters

Returns

getRewardTokens

Function to get the reward tokens added in the farm.

function getRewardTokens() external view returns (address[] memory);

Returns

getTokenAmounts

Function to be called by Demeter Rewarder to get tokens and amounts associated with the farm's liquidity.

This function should be overridden to add the respective logic.

function getTokenAmounts() external view virtual returns (address[] memory, uint256[] memory);

Returns

updateFarmRewardData

Function to update the FarmRewardData for all funds.

function updateFarmRewardData() public virtual;

claimRewardsTo

Claim rewards and send it to another account.

Only the depositor can call this function.

function claimRewardsTo(address _account, uint256 _depositId) public nonReentrant;

Parameters

updateFarmStartTime

Update the farm start time.

Can be updated only before the farm start. New start time should be in future.

function updateFarmStartTime(uint256 _newStartTime) public virtual onlyOwner;

Parameters

isFarmOpen

Returns if farm is open. Farm is open if it is not closed.

This function can be overridden to add any new/additional logic.

function isFarmOpen() public view virtual returns (bool);

Returns

isFarmActive

Returns if farm is active. Farm is active if it is not paused and not closed.

This function can be overridden to add any new/additional logic.

function isFarmActive() public view virtual returns (bool);

Returns

getRewardBalance

Get the reward balance for specified reward token.

This function calculates the available reward balance by considering the accrued rewards and the token supply.

function getRewardBalance(address _rwdToken) public view returns (uint256);

Parameters

Returns

_recoverERC20

function _recoverERC20(address _token) internal virtual;

_deposit

Common logic for deposit in the demeter farm.

function _deposit(address _account, bool _lockup, uint256 _liquidity) internal returns (uint256);

Parameters

Returns

_initiateCooldown

Common logic for initiating cooldown.

function _initiateCooldown(uint256 _depositId) internal;

Parameters

_withdraw

Common logic for withdraw.

function _withdraw(uint256 _depositId) internal;

Parameters

_updateAndClaimFarmRewards

Claim rewards for the user.

NOTE: any function calling this private function should be marked as non-reentrant.

function _updateAndClaimFarmRewards(uint256 _depositId) internal;

Parameters

_updateAndClaimFarmRewardsTo

Claim rewards for the user and send it to another account.

NOTE: any function calling this private function should be marked as non-reentrant.

function _updateAndClaimFarmRewardsTo(uint256 _depositId, address _receiver) internal;

Parameters

_recoverRewardFunds

Get the remaining reward balance out of the farm.

Function recovers minOf(_amount, rewardsLeft).

In case of partial withdraw of funds, the reward rate has to be set manually again.

function _recoverRewardFunds(address _rwdToken, uint256 _amount) internal;

Parameters

_setRewardRate

Function to update reward params for a fund.

function _setRewardRate(address _rwdToken, uint128[] memory _newRewardRates) internal;

Parameters

_setupFarm

Function to setup the reward funds and initialize the farm global params during construction.

function _setupFarm(
    string calldata _farmId,
    uint256 _farmStartTime,
    uint256 _cooldownPeriod,
    RewardTokenData[] memory _rwdTokenData
) internal initializer;

Parameters

_addRewardData

Adds new reward token to the farm.

function _addRewardData(address _token, address _tknManager) internal;

Parameters

_updateLastRewardAccrualTime

Update the last reward accrual time.

function _updateLastRewardAccrualTime() internal virtual;

_getAccRewards

Computes the accrued reward for a given fund id and time interval.

_alreadyAccRewardBal is useful when this function called from computeRewards function. As computeReward is a view function and it doesn't update the accRewardBal in the rewardData.

function _getAccRewards(uint8 _rwdId, uint8 _fundId, uint256 _time, uint256 _alreadyAccRewardBal)
    internal
    view
    returns (uint256);

Parameters

Returns

_validateDeposit

Validate the deposit for account.

function _validateDeposit(address _account, uint256 _depositId) internal view;

Parameters

_validateNotRecentDeposit

A function to validate deposit ts to prevent flash loan vulnerabilities

Reverts when deposit made in the same transaction.

function _validateNotRecentDeposit(uint256 _depositTs) internal view;

Parameters

_validateFarmOpen

Validate if farm is open. Revert otherwise.

This function can be overridden to add any new/additional logic.

function _validateFarmOpen() internal view;

_validateFarmActive

Validate if farm is active. Revert otherwise. Farm is active if it is not paused and not closed.

This function can be overridden to add any new/additional logic.

function _validateFarmActive() internal view;

_validateTokenManager

Validate the caller is the token Manager. Revert otherwise.

function _validateTokenManager(address _rwdToken) internal view;

Parameters

_validateRewardToken

Validate the reward token is valid.

function _validateRewardToken(address _rwdToken) internal view;

Parameters

_getRewardAccrualTimeElapsed

Get the time elapsed since the last reward accrual.

function _getRewardAccrualTimeElapsed() internal view virtual returns (uint256);

Returns

_validateCooldownPeriod

An internal function to validate cooldown period.

function _validateCooldownPeriod(uint256 _cooldownPeriod) internal pure;

Parameters

_validateNonZeroAddr

Validate address.

function _validateNonZeroAddr(address _addr) internal pure;

Parameters

_subscribeRewardFund

Add subscription to the reward fund for a deposit.

function _subscribeRewardFund(uint8 _fundId, uint256 _depositId, uint256 _liquidity) private;

Parameters

_unsubscribeRewardFund

Unsubscribe a reward fund from a deposit.

The rewards claimed from the reward fund is persisted in the event.

function _unsubscribeRewardFund(uint8 _fundId, uint256 _depositId) private;

Parameters