Farm

Git Source

Inherits: FarmStorage, OwnableUpgradeable, ReentrancyGuardUpgradeable, MulticallUpgradeable

Author: Sperax Foundation.

This contract contains the core logic for the Sperax farms.

Functions

constructor

constructor();

withdraw

Function to be called to withdraw deposit.

function withdraw(uint256 _depositId) external virtual;

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

claimRewards

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

function claimRewards(uint256 _depositId) external;

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

initiateCooldown

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

_depositId is corresponding to the user's deposit.

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit to be locked.

addRewards

Add rewards to the farm.

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of reward tokens to add.

updateCooldownPeriod

Update the cooldown period.

Parameters

Name
Type
Description

_newCooldownPeriod

uint256

The new cooldown period (in days). E.g: 7 means 7 days.

farmPauseSwitch

Pause / UnPause the farm.

Parameters

Name
Type
Description

_isPaused

bool

Desired state of the farm (true to pause the farm).

closeFarm

A function to explicitly close the farm.

Recovers remaining non accrued rewards.

recoverERC20

Recover erc20 tokens other than the reward tokens.

Parameters

Name
Type
Description

_token

address

Address of token to be recovered.

recoverRewardFunds

Get the remaining reward balance out of the farm.

Function recovers minOf(_amount, rewardsLeft).

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of the reward tokens to be withdrawn.

setRewardRate

Function to update reward params for a fund.

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_newRewardRates

uint128[]

The new reward rate for the fund (includes the precision).

updateRewardData

Transfer the tokenManagerRole to other user.

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

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_newTknManager

address

Address of the new token manager.

computeRewards

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

Parameters

Name
Type
Description

_account

address

The user's address.

_depositId

uint256

The id of the deposit.

Returns

Name
Type
Description

rewards

uint256[][]

The total accrued rewards for the deposit for each subscription (uint256[][]).

getRewardFunds

Get the reward fund details.

Returns

Name
Type
Description

<none>

RewardFund[]

The available reward funds' details for all the reward funds.

getRewardData

Get the reward details for specified reward token.

Parameters

Name
Type
Description

_rwdToken

address

The address of the reward token.

Returns

Name
Type
Description

<none>

RewardData

The available reward details for the specified reward token.

getDepositInfo

Get deposit info for a deposit id.

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

Returns

Name
Type
Description

<none>

Deposit

The deposit info (Deposit).

getNumSubscriptions

Get number of subscriptions for an account.

Parameters

Name
Type
Description

_depositId

uint256

The deposit id.

Returns

Name
Type
Description

<none>

uint256

The number of subscriptions for the deposit.

getSubscriptionInfo

Get subscription stats for a deposit.

Parameters

Name
Type
Description

_depositId

uint256

The deposit id.

_subscriptionId

uint256

The subscription's id.

Returns

Name
Type
Description

<none>

Subscription

The subscription info (Subscription).

getRewardRates

Get reward rates for a rewardToken.

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

Returns

Name
Type
Description

<none>

uint256[]

The reward rates for the reward token (uint256[]).

getRewardFundInfo

Get farm reward fund info.

Parameters

Name
Type
Description

_fundId

uint8

The fund's id.

Returns

Name
Type
Description

<none>

RewardFund

The reward fund info (RewardFund).

getRewardTokens

Function to get the reward tokens added in the farm.

Returns

Name
Type
Description

<none>

address[]

The reward tokens added in the farm.

getTokenAmounts

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

This function should be overridden to add the respective logic.

Returns

Name
Type
Description

<none>

address[]

Tokens associated with the farm's pool.

<none>

uint256[]

Amounts associated with the farm's liquidity.

updateFarmRewardData

Function to update the FarmRewardData for all funds.

claimRewardsTo

Claim rewards and send it to another account.

Only the depositor can call this function.

Parameters

Name
Type
Description

_account

address

To receive the rewards.

_depositId

uint256

The id of the deposit.

updateFarmStartTime

Update the farm start time.

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

Parameters

Name
Type
Description

_newStartTime

uint256

The new farm start time.

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.

Returns

Name
Type
Description

<none>

bool

bool True if farm is open.

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.

Returns

Name
Type
Description

<none>

bool

bool True if farm is active.

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.

Parameters

Name
Type
Description

_rwdToken

address

The address of the reward token.

Returns

Name
Type
Description

<none>

uint256

The available reward balance for the specified reward token.

_recoverERC20

_deposit

Common logic for deposit in the farm.

Parameters

Name
Type
Description

_account

address

Address of the depositor.

_lockup

bool

Lockup option for the deposit.

_liquidity

uint256

Liquidity amount to be added to the pool.

Returns

Name
Type
Description

<none>

uint256

The deposit id.

_initiateCooldown

Common logic for initiating cooldown.

Parameters

Name
Type
Description

_depositId

uint256

User's deposit Id.

_withdraw

Common logic for withdraw.

Parameters

Name
Type
Description

_depositId

uint256

User's deposit id.

_updateAndClaimFarmRewards

Claim rewards for the user.

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

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

_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.

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

_receiver

address

The receiver of the rewards (Could be different from depositor)

_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.

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of the reward token to be withdrawn.

_setRewardRate

Function to update reward params for a fund.

Parameters

Name
Type
Description

_rwdToken

address

The reward token's address.

_newRewardRates

uint128[]

The new reward rate for the fund (includes the precision).

_setupFarm

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

Parameters

Name
Type
Description

_farmId

string

ID of the farm. E.g: Demeter_Camelot_V2.

_farmStartTime

uint256

- Farm start time.

_cooldownPeriod

uint256

- Cooldown period in days for locked deposits. E.g: 7 means 7 days.

_rwdTokenData

RewardTokenData[]

- Reward data for each reward token.

_addRewardData

Adds new reward token to the farm.

Parameters

Name
Type
Description

_token

address

Address of the reward token to be added.

_tknManager

address

Address of the reward token Manager.

_updateLastRewardAccrualTime

Update the last reward accrual time.

_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.

Parameters

Name
Type
Description

_rwdId

uint8

Id of the reward token.

_fundId

uint8

Id of the reward fund.

_time

uint256

Time interval for the reward computation.

_alreadyAccRewardBal

uint256

Already accrued reward balance.

Returns

Name
Type
Description

<none>

uint256

accRewards Accrued rewards for the given _rwdId, _fundId and _time.

_validateDeposit

Validate the deposit for account.

Parameters

Name
Type
Description

_account

address

Address of the caller to be checked against depositor.

_depositId

uint256

Id of the deposit.

_validateNotRecentDeposit

A function to validate deposit ts to prevent flash loan vulnerabilities

Reverts when deposit made in the same transaction.

Parameters

Name
Type
Description

_depositTs

uint256

depositTs of user's deposit. (It represents deposit ts or increaseDeposit ts)

_validateFarmOpen

Validate if farm is open. Revert otherwise.

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

_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.

_validateTokenManager

Validate the caller is the token Manager. Revert otherwise.

Parameters

Name
Type
Description

_rwdToken

address

Address of reward token.

_validateRewardToken

Validate the reward token is valid.

Parameters

Name
Type
Description

_rwdToken

address

Address of reward token.

_getRewardAccrualTimeElapsed

Get the time elapsed since the last reward accrual.

Returns

Name
Type
Description

<none>

uint256

time The time elapsed since the last reward accrual.

_validateCooldownPeriod

An internal function to validate cooldown period.

Parameters

Name
Type
Description

_cooldownPeriod

uint256

Period to be validated.

_validateNonZeroAddr

Validate address.

Parameters

Name
Type
Description

_addr

address

Address to be validated.

_subscribeRewardFund

Add subscription to the reward fund for a deposit.

Parameters

Name
Type
Description

_fundId

uint8

The reward fund id.

_depositId

uint256

The unique ID of the deposit.

_liquidity

uint256

The liquidity of the deposit.

_unsubscribeRewardFund

Unsubscribe a reward fund from a deposit.

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

Parameters

Name
Type
Description

_fundId

uint8

The reward fund id.

_depositId

uint256

The deposit id corresponding to the user.

Last updated