Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SC-929] Added docs #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions contracts/Distributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import { IDistributor } from "./interfaces/IDistributor.sol";
abstract contract Distributor is IDistributor, Ownable {
address internal _distributor;

event DistributorChanged(address newDistributor);

error NotDistributor();
error ZeroDistributorAddress();

modifier onlyDistributor {
if (msg.sender != _distributor) revert NotDistributor();
_;
Expand Down
44 changes: 22 additions & 22 deletions contracts/FarmingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ library FarmingLib {
}

/**
* @notice Creates a new Info struct.
* @notice Creates a new FarmingLib.Info struct.
* @param getTotalSupply The function to get the total supply.
* @param data The data struct for storage.
* @return info The created Info struct.
Expand All @@ -40,8 +40,8 @@ library FarmingLib {
}

/**
* @notice Retrieves the Data struct from an Info struct.
* @param self The Info struct.
* @notice Retrieves the FarmingLib.Data struct from an FarmingLib.Info struct.
* @param self The Info struct to retrieve data from storage.
* @return data The retrieved Data struct.
*/
function getData(Info memory self) internal pure returns(Data storage data) {
Expand All @@ -52,11 +52,11 @@ library FarmingLib {
}

/**
* @notice Begins farming for a specified period.
* @param self The Info struct.
* @param amount The amount to farm.
* @param period The farming period.
* @return reward The farming reward.
* @notice Updates farming info with new amount and specified period.
* @param self The FarmingLib.Info struct to retrieve data from storage.
* @param amount A new amount to farm.
* @param period A new farming period.
* @return reward Updated farming reward.
*/
function startFarming(Info memory self, uint256 amount, uint256 period) internal returns(uint256 reward) {
Data storage data = self.getData();
Expand All @@ -76,22 +76,22 @@ library FarmingLib {
}

/**
* @notice Gets the farmed amount for an account.
* @param self The Info struct.
* @param account The account to check.
* @param balance The account balance.
* @return result The farmed amount.
* @notice Gets the amount of farmed reward tokens for an account.
* @param self The FarmingLib.Info struct to retrieve data from storage.
* @param account The address of the account to check.
* @param balance The farmable token balance of the account.
* @return result The number of tokens farmed.
*/
function farmed(Info memory self, address account, uint256 balance) internal view returns(uint256) {
return self.getData().userInfo.farmed(account, balance, _farmedPerToken(self));
}

/**
* @notice Claims the farmed amount for an account.
* @param self The Info struct.
* @param account The account to claim for.
* @param balance The account balance.
* @return amount The claimed amount.
* @notice Claims the farmed reward tokens for an account.
* @param self The FarmingLib.Info struct to retrieve data from storage.
* @param account The address of the account to claim for.
* @param balance The account balance of farmable tokens.
* @return amount The claimed amount of reward tokens.
*/
function claim(Info memory self, address account, uint256 balance) internal returns(uint256 amount) {
Data storage data = self.getData();
Expand All @@ -104,10 +104,10 @@ library FarmingLib {
}

/**
* @notice Updates the balances of two accounts.
* @param self The Info struct.
* @param from The account to transfer from.
* @param to The account to transfer to.
* @notice Updates the farmable token balances of two accounts.
* @param self The FarmingLib.Info struct to retrieve data from storage.
* @param from The address of the account to transfer from.
* @param to The address of the account to transfer to.
* @param amount The amount to transfer.
*/
function updateBalances(Info memory self, address from, address to, uint256 amount) internal {
Expand Down
30 changes: 26 additions & 4 deletions contracts/FarmingPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import { IFarmingPlugin } from "./interfaces/IFarmingPlugin.sol";
import { Distributor } from "./Distributor.sol";
import { FarmingLib, FarmAccounting } from "./FarmingLib.sol";

/**
* @title Implementation of the {IFarmingPlugin} interface.
* @notice This contract only accounts for the balances of users
* who added it as a plugin to the farmable token.
*/
contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor {
using SafeERC20 for IERC20;
using FarmingLib for FarmingLib.Info;
using FarmAccounting for FarmAccounting.Info;
using Address for address payable;

error ZeroFarmableTokenAddress();
error ZeroRewardsTokenAddress();
error InsufficientFunds();

IERC20 public immutable rewardsToken;

uint256 private _totalSupply;
Expand All @@ -37,20 +38,32 @@ contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor {
emit FarmCreated(address(farmableToken_), address(rewardsToken_));
}

/**
* @notice See {IFarmingPlugin-farmInfo}
*/
function farmInfo() public view returns(FarmAccounting.Info memory) {
return _farm.farmInfo;
}

/**
* @notice See {IFarmingPlugin-totalSupply}
*/
function totalSupply() public view returns(uint256) {
return _totalSupply;
}

/**
* @notice See {IFarmingPlugin-startFarming}
*/
function startFarming(uint256 amount, uint256 period) public virtual onlyDistributor {
uint256 reward = _makeInfo().startFarming(amount, period);
emit RewardUpdated(reward, period);
rewardsToken.safeTransferFrom(msg.sender, address(this), amount);
}

/**
* @notice See {IFarmingPlugin-stopFarming}
*/
function stopFarming() public virtual onlyDistributor {
uint256 leftover = _makeInfo().stopFarming();
emit RewardUpdated(0, 0);
Expand All @@ -59,11 +72,17 @@ contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor {
}
}

/**
* @notice See {IFarmingPlugin-farmed}
*/
function farmed(address account) public view virtual returns(uint256) {
uint256 balance = IERC20Plugins(token).pluginBalanceOf(address(this), account);
return _makeInfo().farmed(account, balance);
}

/**
* @notice See {IFarmingPlugin-claim}
*/
function claim() public virtual {
uint256 pluginBalance = IERC20Plugins(token).pluginBalanceOf(address(this), msg.sender);
uint256 amount = _makeInfo().claim(msg.sender, pluginBalance);
Expand All @@ -86,6 +105,9 @@ contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor {
}
}

/**
* @notice See {IFarmingPlugin-rescueFunds}
*/
function rescueFunds(IERC20 token_, uint256 amount) public virtual onlyDistributor {
if(token_ == IERC20(address(0))) {
payable(_distributor).sendValue(amount);
Expand Down
42 changes: 35 additions & 7 deletions contracts/FarmingPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ import { IFarmingPool } from "./interfaces/IFarmingPool.sol";
import { Distributor } from "./Distributor.sol";
import { FarmAccounting, FarmingLib } from "./FarmingLib.sol";

/**
* @title Implementation of the {IFarmingPool} interface.
* @notice This contract accounts for the balance of the farmable token's deposits through
* its own balance as it is inherited from ERC20.
*/
contract FarmingPool is IFarmingPool, Distributor, ERC20 {
using SafeERC20 for IERC20;
using Address for address payable;
using FarmingLib for FarmingLib.Info;

error SameStakingAndRewardsTokens();
error ZeroStakingTokenAddress();
error ZeroRewardsTokenAddress();
error AccessDenied();
error InsufficientFunds();
error MaxBalanceExceeded();

uint256 internal constant _MAX_BALANCE = 1e32;

IERC20 public immutable stakingToken;
Expand All @@ -44,20 +42,32 @@ contract FarmingPool is IFarmingPool, Distributor, ERC20 {
rewardsToken = rewardsToken_;
}

/**
* @notice See {IERC20Metadata-decimals}
*/
function decimals() public view virtual override returns (uint8) {
return IERC20Metadata(address(stakingToken)).decimals();
}

/**
* @notice See {IFarmingPool-farmInfo}
*/
function farmInfo() public view returns(FarmAccounting.Info memory) {
return _farm.farmInfo;
}

/**
* @notice See {IFarmingPool-startFarming}
*/
function startFarming(uint256 amount, uint256 period) public virtual onlyDistributor {
uint256 reward = _makeInfo().startFarming(amount, period);
emit RewardUpdated(reward, period);
rewardsToken.safeTransferFrom(msg.sender, address(this), amount);
}

/**
* @notice See {IFarmingPool-stopFarming}
*/
function stopFarming() public virtual onlyDistributor {
uint256 leftover = _makeInfo().stopFarming();
emit RewardUpdated(0, 0);
Expand All @@ -66,21 +76,33 @@ contract FarmingPool is IFarmingPool, Distributor, ERC20 {
}
}

/**
* @notice See {IFarmingPool-farmed}
*/
function farmed(address account) public view virtual returns (uint256) {
return _makeInfo().farmed(account, balanceOf(account));
}

/**
* @notice See {IFarmingPool-deposit}
*/
function deposit(uint256 amount) public virtual {
_mint(msg.sender, amount);
if (balanceOf(msg.sender) > _MAX_BALANCE) revert MaxBalanceExceeded();
stakingToken.safeTransferFrom(msg.sender, address(this), amount);
}

/**
* @notice See {IFarmingPool-withdraw}
*/
function withdraw(uint256 amount) public virtual {
_burn(msg.sender, amount);
stakingToken.safeTransfer(msg.sender, amount);
}

/**
* @notice See {IFarmingPool-claim}
*/
function claim() public virtual {
uint256 amount = _makeInfo().claim(msg.sender, balanceOf(msg.sender));
if (amount > 0) {
Expand All @@ -92,11 +114,17 @@ contract FarmingPool is IFarmingPool, Distributor, ERC20 {
reward.safeTransfer(to, amount);
}

/**
* @notice See {IFarmingPool-exit}
*/
function exit() public virtual {
withdraw(balanceOf(msg.sender));
claim();
}

/**
* @notice See {IFarmingPool-rescueFunds}
*/
function rescueFunds(IERC20 token, uint256 amount) public virtual onlyDistributor {
if (token == IERC20(address(0))) {
payable(_distributor).sendValue(amount);
Expand Down
Loading