These contracts are base for all the other contracts and they have the common logic for functionalities like deposit, withdraw, createFarm, etc.
Inherits: Ownable, ReentrancyGuard
Author: Sperax Foundation.
Exposes base functionalities which will be useful in every deployer.
Constructor.
Parameters
Update farm implementation's address.
Only callable by the owner.
Ensure that _newFarmId is correct for the new farm implementation.
Parameters
Collect fee and transfer it to feeReceiver.
Function fetches all the fee params from farmRegistry.
Validate address.
_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.
constructor
Parameters
_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.
Register a farm created by registered Deployer.
Only registered deployer can register a farm.
Parameters
_farm
address
Address of the created farm contract
_creator
address
Address of the farm creator.
Register a new farm deployer.
Only owner can call this function.
Parameters
_deployer
address
Address of deployer to be registered.
Remove an existing deployer from registry.
Only owner can call this function.
Parameters
_id
uint16
ID of the deployer to be removed (0 index based).
Function to add/remove privileged User.
Only callable by the owner.
Parameters
_user
address
User Address for which privilege is to be updated.
_privilege
bool
Privilege(bool) whether true or false.
Get list of registered deployer.
Returns
<none>
address[]
Returns array of registered deployer addresses.
Get list of farms created via registered deployer.
Returns
<none>
address[]
Returns array of farm addresses.
Get all the fee parameters for creating farm.
It returns fee amount and extension fee as 0 if _user is privileged.
Parameters
_user
address
The account creating the farm.
Returns
<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.
Update the fee params for registry.
Parameters
_receiver
address
FeeReceiver address.
_feeToken
address
Token address for fee.
_amount
uint256
Amount of token to be collected.
_extensionFeePerDay
uint256
Extension fee per day.
Validate address.
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;Inherits: IFarm
Author: Sperax Foundation.
This contract contains the base storage variables for farms.
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;Inherits: FarmStorage, OwnableUpgradeable, ReentrancyGuardUpgradeable, MulticallUpgradeable
Author: Sperax Foundation.
This contract contains the core logic for the Sperax farms.
Function to be called to withdraw deposit.
Parameters
A function to be called by the depositor to claim rewards.
Parameters
Function to be called to initiate cooldown for a staked deposit.
_depositId is corresponding to the user's deposit.
Parameters
Add rewards to the farm.
Parameters
Update the cooldown period.
Parameters
Pause / UnPause the farm.
Parameters
A function to explicitly close the farm.
Recovers remaining non accrued rewards.
Recover erc20 tokens other than the reward tokens.
Parameters
Get the remaining reward balance out of the farm.
Function recovers minOf(_amount, rewardsLeft).
Parameters
Function to update reward params for a fund.
Parameters
Transfer the tokenManagerRole to other user.
Only the existing tokenManager for a reward can call this function.
Parameters
Function to compute the total accrued rewards for a deposit for each subscription.
Parameters
Returns
Get the reward fund details.
Returns
Get the reward details for specified reward token.
Parameters
Returns
Get deposit info for a deposit id.
Parameters
Returns
Get number of subscriptions for an account.
Parameters
Returns
Get subscription stats for a deposit.
Parameters
Returns
Get reward rates for a rewardToken.
Parameters
Returns
Get farm reward fund info.
Parameters
Returns
Function to get the reward tokens added in the farm.
Returns
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
Function to update the FarmRewardData for all funds.
Claim rewards and send it to another account.
Only the depositor can call this function.
Parameters
Update the farm start time.
Can be updated only before the farm start. New start time should be in future.
Parameters
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
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
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
Returns
Common logic for deposit in the farm.
Parameters
Returns
Common logic for initiating cooldown.
Parameters
Common logic for withdraw.
Parameters
Claim rewards for the user.
NOTE: any function calling this private function should be marked as non-reentrant.
Parameters
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
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
Function to update reward params for a fund.
Parameters
Function to setup the reward funds and initialize the farm global params during construction.
Parameters
Adds new reward token to the farm.
Parameters
Update the last reward accrual time.
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
Returns
Validate the deposit for account.
Parameters
A function to validate deposit ts to prevent flash loan vulnerabilities
Reverts when deposit made in the same transaction.
Parameters
Validate if farm is open. Revert otherwise.
This function can be overridden to add any new/additional logic.
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.
Validate the caller is the token Manager. Revert otherwise.
Parameters
Validate the reward token is valid.
Parameters
Get the time elapsed since the last reward accrual.
Returns
An internal function to validate cooldown period.
Parameters
Validate address.
Parameters
Add subscription to the reward fund for a deposit.
Parameters
Unsubscribe a reward fund from a deposit.
The rewards claimed from the reward fund is persisted in the event.
Parameters
_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;