All pages
Powered by GitBook
1 of 5

Loading...

Loading...

Loading...

Loading...

Loading...

Base contracts

These contracts are base for all the other contracts and they have the common logic for functionalities like deposit, withdraw, createFarm, etc.

FarmDeployer

Git Source

Inherits: Ownable, ReentrancyGuard

Author: Sperax Foundation.

Exposes base functionalities which will be useful in every deployer.

State Variables

FARM_REGISTRY

farmImplementation

farmId

Functions

constructor

Constructor.

Parameters

Name
Type
Description

updateFarmImplementation

Update farm implementation's address.

Only callable by the owner.

Ensure that _newFarmId is correct for the new farm implementation.

Parameters

Name
Type
Description

_collectFee

Collect fee and transfer it to feeReceiver.

Function fetches all the fee params from farmRegistry.

_validateNonZeroAddr

Validate address.

Events

FarmCreated

FeeCollected

FarmImplementationUpdated

Errors

InvalidAddress

NewFarmImplementationSameAsOld

FarmRegistry

Inherits: IFarmRegistry, OwnableUpgradeable

Author: Sperax Foundation.

This contract tracks fee details, privileged users, deployed farms and farm deployers.

State Variables

_farmRegistry

address

Address of the Farm Registry.

_farmId

string

Id of the farm.

_newFarmImplementation

address

New farm implementation's address.

_newFarmId

string

ID of the new farm.

farms

deployerList

feeReceiver

feeToken

feeAmount

extensionFeePerDay

farmRegistered

deployerRegistered

isPrivilegedUser

Functions

constructor

initialize

constructor

Parameters

Name
Type
Description

_feeReceiver

address

Receiver of the fees.

_feeToken

address

The fee token for farm creation.

_feeAmount

uint256

The fee amount to be paid by the creator.

_extensionFeePerDay

uint256

Extension fee per day.

registerFarm

Register a farm created by registered Deployer.

Only registered deployer can register a farm.

Parameters

Name
Type
Description

_farm

address

Address of the created farm contract

_creator

address

Address of the farm creator.

registerFarmDeployer

Register a new farm deployer.

Only owner can call this function.

Parameters

Name
Type
Description

_deployer

address

Address of deployer to be registered.

removeDeployer

Remove an existing deployer from registry.

Only owner can call this function.

Parameters

Name
Type
Description

_id

uint16

ID of the deployer to be removed (0 index based).

updatePrivilege

Function to add/remove privileged User.

Only callable by the owner.

Parameters

Name
Type
Description

_user

address

User Address for which privilege is to be updated.

_privilege

bool

Privilege(bool) whether true or false.

getFarmDeployerList

Get list of registered deployer.

Returns

Name
Type
Description

<none>

address[]

Returns array of registered deployer addresses.

getFarmList

Get list of farms created via registered deployer.

Returns

Name
Type
Description

<none>

address[]

Returns array of farm addresses.

getFeeParams

Get all the fee parameters for creating farm.

It returns fee amount and extension fee as 0 if _user is privileged.

Parameters

Name
Type
Description

_user

address

The account creating the farm.

Returns

Name
Type
Description

<none>

address

Receiver of the fees.

<none>

address

Token in which fee is to be paid.

<none>

uint256

Amount of fees to be paid for creation of farm.

<none>

uint256

Extension fee per day in case of extending a farm.

updateFeeParams

Update the fee params for registry.

Parameters

Name
Type
Description

_receiver

address

FeeReceiver address.

_feeToken

address

Token address for fee.

_amount

uint256

Amount of token to be collected.

_extensionFeePerDay

uint256

Extension fee per day.

_validateNonZeroAddr

Validate address.

Git Source
address public immutable FARM_REGISTRY;
address public farmImplementation;
string public farmId;
constructor(address _farmRegistry, string memory _farmId) Ownable(msg.sender);
function updateFarmImplementation(address _newFarmImplementation, string calldata _newFarmId) external onlyOwner;
function _collectFee() internal virtual;
function _validateNonZeroAddr(address _addr) internal pure;
event FarmCreated(address indexed farm, address indexed creator, address indexed admin);
event FeeCollected(address indexed creator, address indexed token, uint256 amount);
event FarmImplementationUpdated(address indexed newFarmImplementation, string newFarmId);
error InvalidAddress();
error NewFarmImplementationSameAsOld();
address[] internal farms;
address[] internal deployerList;
address public feeReceiver;
address public feeToken;
uint256 public feeAmount;
uint256 public extensionFeePerDay;
mapping(address => bool) public farmRegistered;
mapping(address => bool) public deployerRegistered;
mapping(address => bool) public isPrivilegedUser;
constructor();
function initialize(address _feeReceiver, address _feeToken, uint256 _feeAmount, uint256 _extensionFeePerDay)
    external
    initializer;
function registerFarm(address _farm, address _creator) external;
function registerFarmDeployer(address _deployer) external onlyOwner;
function removeDeployer(uint16 _id) external onlyOwner;
function updatePrivilege(address _user, bool _privilege) external onlyOwner;
function getFarmDeployerList() external view returns (address[] memory);
function getFarmList() external view returns (address[] memory);
function getFeeParams(address _user) external view returns (address, address, uint256, uint256);
function updateFeeParams(address _receiver, address _feeToken, uint256 _amount, uint256 _extensionFeePerDay)
    public
    onlyOwner;
function _validateNonZeroAddr(address _addr) private pure;

FarmStorage

Git Source

Inherits: IFarm

Author: Sperax Foundation.

This contract contains the base storage variables for farms.

State Variables

COMMON_FUND_ID

LOCKUP_FUND_ID

PRECISION

MAX_COOLDOWN_PERIOD

MAX_NUM_REWARDS

farmId

isPaused

isClosed

cooldownPeriod

lastFundUpdateTime

farmStartTime

totalDeposits

rewardFunds

rewardTokens

rewardData

deposits

subscriptions

uint8 public constant COMMON_FUND_ID = 0;
uint8 public constant LOCKUP_FUND_ID = 1;
uint256 public constant PRECISION = 1e18;
uint256 public constant MAX_COOLDOWN_PERIOD = 30;
uint256 public constant MAX_NUM_REWARDS = 4;
string public farmId;
bool internal isPaused;
bool internal isClosed;
uint256 public cooldownPeriod;
uint256 public lastFundUpdateTime;
uint256 public farmStartTime;
uint256 public totalDeposits;
RewardFund[] internal rewardFunds;
address[] internal rewardTokens;
mapping(address => RewardData) internal rewardData;
mapping(uint256 => Deposit) internal deposits;
mapping(uint256 => Subscription[]) internal subscriptions;

Farm

Git Source

Inherits: FarmStorage, OwnableUpgradeable, ReentrancyGuardUpgradeable, MulticallUpgradeable

Author: Sperax Foundation.

This contract contains the core logic for the Sperax farms.

Functions

constructor

withdraw

Function to be called to withdraw deposit.

Parameters

Name
Type
Description

claimRewards

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

Parameters

Name
Type
Description

initiateCooldown

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

_depositId is corresponding to the user's deposit.

Parameters

Name
Type
Description

addRewards

Add rewards to the farm.

Parameters

Name
Type
Description

updateCooldownPeriod

Update the cooldown period.

Parameters

Name
Type
Description

farmPauseSwitch

Pause / UnPause the farm.

Parameters

Name
Type
Description

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

recoverRewardFunds

Get the remaining reward balance out of the farm.

Function recovers minOf(_amount, rewardsLeft).

Parameters

Name
Type
Description

setRewardRate

Function to update reward params for a fund.

Parameters

Name
Type
Description

updateRewardData

Transfer the tokenManagerRole to other user.

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

Parameters

Name
Type
Description

computeRewards

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

Parameters

Name
Type
Description

Returns

Name
Type
Description

getRewardFunds

Get the reward fund details.

Returns

Name
Type
Description

getRewardData

Get the reward details for specified reward token.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getDepositInfo

Get deposit info for a deposit id.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getNumSubscriptions

Get number of subscriptions for an account.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getSubscriptionInfo

Get subscription stats for a deposit.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getRewardRates

Get reward rates for a rewardToken.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getRewardFundInfo

Get farm reward fund info.

Parameters

Name
Type
Description

Returns

Name
Type
Description

getRewardTokens

Function to get the reward tokens added in the farm.

Returns

Name
Type
Description

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

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

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

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

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

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

Returns

Name
Type
Description

_recoverERC20

_deposit

Common logic for deposit in the farm.

Parameters

Name
Type
Description

Returns

Name
Type
Description

_initiateCooldown

Common logic for initiating cooldown.

Parameters

Name
Type
Description

_withdraw

Common logic for withdraw.

Parameters

Name
Type
Description

_updateAndClaimFarmRewards

Claim rewards for the user.

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

Parameters

Name
Type
Description

_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

_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

_setRewardRate

Function to update reward params for a fund.

Parameters

Name
Type
Description

_setupFarm

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

Parameters

Name
Type
Description

_addRewardData

Adds new reward token to the farm.

Parameters

Name
Type
Description

_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

Returns

Name
Type
Description

_validateDeposit

Validate the deposit for account.

Parameters

Name
Type
Description

_validateNotRecentDeposit

A function to validate deposit ts to prevent flash loan vulnerabilities

Reverts when deposit made in the same transaction.

Parameters

Name
Type
Description

_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

_validateRewardToken

Validate the reward token is valid.

Parameters

Name
Type
Description

_getRewardAccrualTimeElapsed

Get the time elapsed since the last reward accrual.

Returns

Name
Type
Description

_validateCooldownPeriod

An internal function to validate cooldown period.

Parameters

Name
Type
Description

_validateNonZeroAddr

Validate address.

Parameters

Name
Type
Description

_subscribeRewardFund

Add subscription to the reward fund for a deposit.

Parameters

Name
Type
Description

_unsubscribeRewardFund

Unsubscribe a reward fund from a deposit.

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

Parameters

Name
Type
Description

_depositId

uint256

The id of the deposit.

_depositId

uint256

The id of the deposit.

_depositId

uint256

The id of the deposit to be locked.

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of reward tokens to add.

_newCooldownPeriod

uint256

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

_isPaused

bool

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

_token

address

Address of token to be recovered.

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of the reward tokens to be withdrawn.

_rwdToken

address

The reward token's address.

_newRewardRates

uint128[]

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

_rwdToken

address

The reward token's address.

_newTknManager

address

Address of the new token manager.

_account

address

The user's address.

_depositId

uint256

The id of the deposit.

rewards

uint256[][]

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

<none>

RewardFund[]

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

_rwdToken

address

The address of the reward token.

<none>

RewardData

The available reward details for the specified reward token.

_depositId

uint256

The id of the deposit.

<none>

Deposit

The deposit info (Deposit).

_depositId

uint256

The deposit id.

<none>

uint256

The number of subscriptions for the deposit.

_depositId

uint256

The deposit id.

_subscriptionId

uint256

The subscription's id.

<none>

Subscription

The subscription info (Subscription).

_rwdToken

address

The reward token's address.

<none>

uint256[]

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

_fundId

uint8

The fund's id.

<none>

RewardFund

The reward fund info (RewardFund).

<none>

address[]

The reward tokens added in the farm.

<none>

address[]

Tokens associated with the farm's pool.

<none>

uint256[]

Amounts associated with the farm's liquidity.

_account

address

To receive the rewards.

_depositId

uint256

The id of the deposit.

_newStartTime

uint256

The new farm start time.

<none>

bool

bool True if farm is open.

<none>

bool

bool True if farm is active.

_rwdToken

address

The address of the reward token.

<none>

uint256

The available reward balance for the specified reward token.

_account

address

Address of the depositor.

_lockup

bool

Lockup option for the deposit.

_liquidity

uint256

Liquidity amount to be added to the pool.

<none>

uint256

The deposit id.

_depositId

uint256

User's deposit Id.

_depositId

uint256

User's deposit id.

_depositId

uint256

The id of the deposit.

_depositId

uint256

The id of the deposit.

_receiver

address

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

_rwdToken

address

The reward token's address.

_amount

uint256

The amount of the reward token to be withdrawn.

_rwdToken

address

The reward token's address.

_newRewardRates

uint128[]

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

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

_token

address

Address of the reward token to be added.

_tknManager

address

Address of the reward token Manager.

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

<none>

uint256

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

_account

address

Address of the caller to be checked against depositor.

_depositId

uint256

Id of the deposit.

_depositTs

uint256

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

_rwdToken

address

Address of reward token.

_rwdToken

address

Address of reward token.

<none>

uint256

time The time elapsed since the last reward accrual.

_cooldownPeriod

uint256

Period to be validated.

_addr

address

Address to be validated.

_fundId

uint8

The reward fund id.

_depositId

uint256

The unique ID of the deposit.

_liquidity

uint256

The liquidity of the deposit.

_fundId

uint8

The reward fund id.

_depositId

uint256

The deposit id corresponding to the user.

constructor();
function withdraw(uint256 _depositId) external virtual;
function claimRewards(uint256 _depositId) external;
function initiateCooldown(uint256 _depositId) external nonReentrant;
function addRewards(address _rwdToken, uint256 _amount) external nonReentrant;
function updateCooldownPeriod(uint256 _newCooldownPeriod) external onlyOwner;
function farmPauseSwitch(bool _isPaused) external onlyOwner;
function closeFarm() external onlyOwner nonReentrant;
function recoverERC20(address _token) external onlyOwner nonReentrant;
function recoverRewardFunds(address _rwdToken, uint256 _amount) external nonReentrant;
function setRewardRate(address _rwdToken, uint128[] memory _newRewardRates) external;
function updateRewardData(address _rwdToken, address _newTknManager) external;
function computeRewards(address _account, uint256 _depositId)
    external
    view
    virtual
    returns (uint256[][] memory rewards);
function getRewardFunds() external view returns (RewardFund[] memory);
function getRewardData(address _rwdToken) external view returns (RewardData memory);
function getDepositInfo(uint256 _depositId) external view returns (Deposit memory);
function getNumSubscriptions(uint256 _depositId) external view returns (uint256);
function getSubscriptionInfo(uint256 _depositId, uint256 _subscriptionId) external view returns (Subscription memory);
function getRewardRates(address _rwdToken) external view returns (uint256[] memory);
function getRewardFundInfo(uint8 _fundId) external view returns (RewardFund memory);
function getRewardTokens() external view returns (address[] memory);
function getTokenAmounts() external view virtual returns (address[] memory, uint256[] memory);
function updateFarmRewardData() public virtual;
function claimRewardsTo(address _account, uint256 _depositId) public nonReentrant;
function updateFarmStartTime(uint256 _newStartTime) public virtual onlyOwner;
function isFarmOpen() public view virtual returns (bool);
function isFarmActive() public view virtual returns (bool);
function getRewardBalance(address _rwdToken) public view returns (uint256);
function _recoverERC20(address _token) internal virtual;
function _deposit(address _account, bool _lockup, uint256 _liquidity) internal returns (uint256);
function _initiateCooldown(uint256 _depositId) internal;
function _withdraw(uint256 _depositId) internal;
function _updateAndClaimFarmRewards(uint256 _depositId) internal;
function _updateAndClaimFarmRewardsTo(uint256 _depositId, address _receiver) internal;
function _recoverRewardFunds(address _rwdToken, uint256 _amount) internal;
function _setRewardRate(address _rwdToken, uint128[] memory _newRewardRates) internal;
function _setupFarm(
    string calldata _farmId,
    uint256 _farmStartTime,
    uint256 _cooldownPeriod,
    RewardTokenData[] memory _rwdTokenData
) internal initializer;
function _addRewardData(address _token, address _tknManager) internal;
function _updateLastRewardAccrualTime() internal virtual;
function _getAccRewards(uint8 _rwdId, uint8 _fundId, uint256 _time, uint256 _alreadyAccRewardBal)
    internal
    view
    returns (uint256);
function _validateDeposit(address _account, uint256 _depositId) internal view;
function _validateNotRecentDeposit(uint256 _depositTs) internal view;
function _validateFarmOpen() internal view;
function _validateFarmActive() internal view;
function _validateTokenManager(address _rwdToken) internal view;
function _validateRewardToken(address _rwdToken) internal view;
function _getRewardAccrualTimeElapsed() internal view virtual returns (uint256);
function _validateCooldownPeriod(uint256 _cooldownPeriod) internal pure;
function _validateNonZeroAddr(address _addr) internal pure;
function _subscribeRewardFund(uint8 _fundId, uint256 _depositId, uint256 _liquidity) private;
function _unsubscribeRewardFund(uint8 _fundId, uint256 _depositId) private;