// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 2 of 9: ERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/ERC20.sol)pragmasolidity ^0.8.0;import"./IERC20.sol";
import"./extensions/IERC20Metadata.sol";
import"../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/contractERC20isContext, IERC20, IERC20Metadata{
mapping(address=>uint256) private _balances;
mapping(address=>mapping(address=>uint256)) private _allowances;
uint256private _totalSupply;
stringprivate _name;
stringprivate _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* The default value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/constructor(stringmemory name_, stringmemory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/functionname() publicviewvirtualoverridereturns (stringmemory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/functionsymbol() publicviewvirtualoverridereturns (stringmemory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/functiondecimals() publicviewvirtualoverridereturns (uint8) {
return18;
}
/**
* @dev See {IERC20-totalSupply}.
*/functiontotalSupply() publicviewvirtualoverridereturns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/functionbalanceOf(address account) publicviewvirtualoverridereturns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/functiontransfer(address to, uint256 amount) publicvirtualoverridereturns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
returntrue;
}
/**
* @dev See {IERC20-allowance}.
*/functionallowance(address owner, address spender) publicviewvirtualoverridereturns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/functionapprove(address spender, uint256 amount) publicvirtualoverridereturns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
returntrue;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* NOTE: Does not update the allowance if the current allowance
* is the maximum `uint256`.
*
* Requirements:
*
* - `from` and `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
* - the caller must have allowance for ``from``'s tokens of at least
* `amount`.
*/functiontransferFrom(addressfrom,
address to,
uint256 amount
) publicvirtualoverridereturns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
returntrue;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/functionincreaseAllowance(address spender, uint256 addedValue) publicvirtualreturns (bool) {
address owner = _msgSender();
_approve(owner, spender, _allowances[owner][spender] + addedValue);
returntrue;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/functiondecreaseAllowance(address spender, uint256 subtractedValue) publicvirtualreturns (bool) {
address owner = _msgSender();
uint256 currentAllowance = _allowances[owner][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
returntrue;
}
/**
* @dev Moves `amount` of tokens from `sender` to `recipient`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
*/function_transfer(addressfrom,
address to,
uint256 amount
) internalvirtual{
require(from!=address(0), "ERC20: transfer from the zero address");
require(to !=address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
}
_balances[to] += amount;
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/function_mint(address account, uint256 amount) internalvirtual{
require(account !=address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/function_burn(address account, uint256 amount) internalvirtual{
require(account !=address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/function_approve(address owner,
address spender,
uint256 amount
) internalvirtual{
require(owner !=address(0), "ERC20: approve from the zero address");
require(spender !=address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Spend `amount` form the allowance of `owner` toward `spender`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/function_spendAllowance(address owner,
address spender,
uint256 amount
) internalvirtual{
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance !=type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/function_beforeTokenTransfer(addressfrom,
address to,
uint256 amount
) internalvirtual{}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/function_afterTokenTransfer(addressfrom,
address to,
uint256 amount
) internalvirtual{}
}
Contract Source Code
File 3 of 9: IERC165.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/interfaceIERC165{
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/functionsupportsInterface(bytes4 interfaceId) externalviewreturns (bool);
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)pragmasolidity ^0.8.0;import"../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/interfaceIERC20MetadataisIERC20{
/**
* @dev Returns the name of the token.
*/functionname() externalviewreturns (stringmemory);
/**
* @dev Returns the symbol of the token.
*/functionsymbol() externalviewreturns (stringmemory);
/**
* @dev Returns the decimals places of the token.
*/functiondecimals() externalviewreturns (uint8);
}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.4;import'@openzeppelin/contracts/token/ERC20/ERC20.sol';
import'@openzeppelin/contracts/interfaces/IERC20.sol';
import'@openzeppelin/contracts/interfaces/IERC721.sol';
import'@openzeppelin/contracts/utils/math/SafeMath.sol';
import'./interfaces/IOKLGFaaSTimePricing.sol';
/**
* @title OKLGFaaSToken (sOKLG)
* @notice Represents a contract where a token owner has put her tokens up for others to stake and earn said tokens.
*/contractOKLGFaaSTokenisERC20{
usingSafeMathforuint256;
boolpublic contractIsRemoved =false;
IERC20 private _rewardsToken;
IERC20 private _stakedERC20;
IERC721 private _stakedERC721;
IOKLGFaaSTimePricing private _faasPricing;
PoolInfo public pool;
structPoolInfo {
address creator; // address of contract creatoraddress tokenOwner; // address of original rewards token owneruint256 poolTotalSupply; // supply of rewards tokens put up to be rewarded by original owneruint256 poolRemainingSupply; // current supply of rewardsuint256 totalTokensStaked; // current amount of tokens stakeduint256 creationBlock; // block this contract was createduint256 perBlockNum; // amount of rewards tokens rewarded per blockuint256 lockedUntilDate; // unix timestamp of how long this contract is locked and can't be changed// uint256 allocPoint; // How many allocation points assigned to this pool. ERC20s to distribute per block.uint256 lastRewardBlock; // Last block number that ERC20s distribution occurs.uint256 accERC20PerShare; // Accumulated ERC20s per share, times 1e36.uint256 stakeTimeLockSec; // number of seconds after depositing the user is required to stake before unstakingbool isStakedNft;
}
structStakerInfo {
uint256 amountStaked;
uint256 blockOriginallyStaked; // block the user originally stakeduint256 timeOriginallyStaked; // unix timestamp in seconds that the user originally stakeduint256 blockLastHarvested; // the block the user last claimed/harvested rewardsuint256 rewardDebt; // Reward debt. See explanation below.uint256[] nftTokenIds; // if this is an NFT staking pool, make sure we store the token IDs here
}
structBlockTokenTotal {
uint256 blockNumber;
uint256 totalTokens;
}
// mapping of userAddresses => tokenAddresses that can// can be evaluated to determine for a particular user which tokens// they are staking.mapping(address=> StakerInfo) public stakers;
// If we need to keep track of owed rewards for a user but not// send them yet (i.e. when adding tokens for a stake during lockup)// this keeps track of that, and will add said rewards back to// the rewards pool on emergency unstakemapping(address=>uint256) public rewardVault;
eventDeposit(addressindexed user, uint256 amount);
eventWithdraw(addressindexed user, uint256 amount);
/**
* @notice The constructor for the Staking Token.
* @param _name Name of the staking token
* @param _symbol Name of the staking token symbol
* @param _rewardSupply The amount of tokens to mint on construction, this should be the same as the tokens provided by the creating user.
* @param _rewardsTokenAddy Contract address of token to be rewarded to users
* @param _stakedTokenAddy Contract address of token to be staked by users
* @param _originalTokenOwner Address of user putting up staking tokens to be staked
* @param _perBlockAmount Amount of tokens to be rewarded per block
* @param _lockedUntilDate Unix timestamp that the staked tokens will be locked. 0 means locked forever until all tokens are staked
* @param _stakeTimeLockSec number of seconds a user is required to stake, or 0 if none
* @param _isStakedNft is this an NFT staking pool
* @param _pricingContract is contract we use to pay to create and update supply for FaaS pools
*/constructor(stringmemory _name,
stringmemory _symbol,
uint256 _rewardSupply,
address _rewardsTokenAddy,
address _stakedTokenAddy,
address _originalTokenOwner,
uint256 _perBlockAmount,
uint256 _lockedUntilDate,
uint256 _stakeTimeLockSec,
bool _isStakedNft,
address _pricingContract
) ERC20(_name, _symbol) {
require(
_perBlockAmount >uint256(0) && _perBlockAmount <=uint256(_rewardSupply),
'per block amount must be more than 0 and less than supply'
);
// A locked date of '0' corresponds to being locked forever until the supply has expired and been rewards to all stakersrequire(
_lockedUntilDate >block.timestamp|| _lockedUntilDate ==0,
'locked time must be after now or 0'
);
_rewardsToken = IERC20(_rewardsTokenAddy);
if (_isStakedNft) {
_stakedERC721 = IERC721(_stakedTokenAddy);
} else {
_stakedERC20 = IERC20(_stakedTokenAddy);
}
pool = PoolInfo({
creator: msg.sender,
tokenOwner: _originalTokenOwner,
poolTotalSupply: _rewardSupply,
poolRemainingSupply: _rewardSupply,
totalTokensStaked: 0,
creationBlock: 0,
perBlockNum: _perBlockAmount,
lockedUntilDate: _lockedUntilDate,
lastRewardBlock: block.number,
accERC20PerShare: 0,
stakeTimeLockSec: _stakeTimeLockSec,
isStakedNft: _isStakedNft
});
_faasPricing = IOKLGFaaSTimePricing(_pricingContract);
}
// SHOULD ONLY BE CALLED AT CONTRACT CREATION and allows changing// the initial supply if tokenomics of token transfer causes// the original staking contract supply to be less than the originalfunctionupdateSupply(uint256 _newSupply) external{
require(
msg.sender== pool.creator,
'only contract creator can update the supply'
);
pool.poolTotalSupply = _newSupply;
pool.poolRemainingSupply = _newSupply;
}
functionaddToSupply(uint256 _additionalSupply) externalpayable{
require(_additionalSupply >= pool.perBlockNum, 'must add 1 block at least');
_faasPricing.payForPool{ value: msg.value }(
_additionalSupply,
pool.perBlockNum
);
uint256 _balBefore = _rewardsToken.balanceOf(address(this));
_rewardsToken.transferFrom(msg.sender, address(this), _additionalSupply);
_additionalSupply = _rewardsToken.balanceOf(address(this)) - _balBefore;
pool.poolTotalSupply += _additionalSupply;
pool.poolRemainingSupply += _additionalSupply;
}
functionstakedTokenAddress() externalviewreturns (address) {
return pool.isStakedNft ? address(_stakedERC721) : address(_stakedERC20);
}
functionrewardsTokenAddress() externalviewreturns (address) {
returnaddress(_rewardsToken);
}
functiontokenOwner() externalviewreturns (address) {
return pool.tokenOwner;
}
functiongetLockedUntilDate() externalviewreturns (uint256) {
return pool.lockedUntilDate;
}
functionremoveRewards() external{
require(
msg.sender== pool.tokenOwner ||msg.sender== pool.creator,
'must be owner or master contract to remove rewards'
);
_rewardsToken.transfer(pool.tokenOwner, pool.poolRemainingSupply);
pool.poolRemainingSupply =0;
contractIsRemoved =true;
}
functionstakeTokens(uint256 _amount, uint256[] memory _tokenIds) public{
require(
getLastStakableBlock() >block.number,
'this farm is expired and no more stakers can be added'
);
StakerInfo storage _staker = stakers[msg.sender];
_updatePool();
if (balanceOf(msg.sender) >0) {
_harvestTokens(
msg.sender,
block.timestamp>=
_staker.timeOriginallyStaked.add(pool.stakeTimeLockSec)
);
}
uint256 _finalAmountTransferred;
if (pool.isStakedNft) {
require(
_tokenIds.length>0,
"you need to provide NFT token IDs you're staking"
);
for (uint256 _i =0; _i < _tokenIds.length; _i++) {
_stakedERC721.transferFrom(msg.sender, address(this), _tokenIds[_i]);
}
_finalAmountTransferred = _tokenIds.length;
} else {
uint256 _contractBalanceBefore = _stakedERC20.balanceOf(address(this));
_stakedERC20.transferFrom(msg.sender, address(this), _amount);
// in the event a token contract on transfer taxes, burns, etc. tokens// the contract might not get the entire amount that the user originally// transferred. Need to calculate from the previous contract balance// so we know how many were actually transferred.
_finalAmountTransferred = _stakedERC20.balanceOf(address(this)).sub(
_contractBalanceBefore
);
}
if (totalSupply() ==0) {
pool.creationBlock =block.number;
pool.lastRewardBlock =block.number;
}
_mint(msg.sender, _finalAmountTransferred);
_staker.amountStaked = _staker.amountStaked.add(_finalAmountTransferred);
_staker.blockOriginallyStaked =block.number;
_staker.timeOriginallyStaked =block.timestamp;
_staker.blockLastHarvested =block.number;
_staker.rewardDebt = _staker.amountStaked.mul(pool.accERC20PerShare).div(
1e36
);
for (uint256 _i =0; _i < _tokenIds.length; _i++) {
_staker.nftTokenIds.push(_tokenIds[_i]);
}
_updNumStaked(_finalAmountTransferred, 'add');
emit Deposit(msg.sender, _finalAmountTransferred);
}
// pass 'false' for _shouldHarvest for emergency unstaking without claiming rewardsfunctionunstakeTokens(uint256 _amount, bool _shouldHarvest) external{
StakerInfo storage _staker = stakers[msg.sender];
uint256 _userBalance = _staker.amountStaked;
require(
pool.isStakedNft ? true : _amount <= _userBalance,
'user can only unstake amount they have currently staked or less'
);
// allow unstaking if the user is emergency unstaking and not getting rewards or// if theres a time lock that it's past the time lock or// the contract rewards were removed by the original contract creator or// the contract is expiredrequire(
!_shouldHarvest ||block.timestamp>=
_staker.timeOriginallyStaked.add(pool.stakeTimeLockSec) ||
contractIsRemoved ||block.number> getLastStakableBlock(),
'you have not staked for minimum time lock yet and the pool is not expired'
);
_updatePool();
if (_shouldHarvest) {
_harvestTokens(msg.sender, true);
} else {
_removeFromVaultBackToPool(msg.sender);
}
uint256 _amountToRemoveFromStaked = pool.isStakedNft
? _userBalance
: _amount;
_burn(
msg.sender,
_amountToRemoveFromStaked > balanceOf(msg.sender)
? balanceOf(msg.sender)
: _amountToRemoveFromStaked
);
if (pool.isStakedNft) {
for (uint256 _i =0; _i < _staker.nftTokenIds.length; _i++) {
_stakedERC721.transferFrom(
address(this),
msg.sender,
_staker.nftTokenIds[_i]
);
}
} else {
require(
_stakedERC20.transfer(msg.sender, _amountToRemoveFromStaked),
'unable to send user original tokens'
);
}
if (balanceOf(msg.sender) <=0) {
delete stakers[msg.sender];
} else {
_staker.amountStaked = _staker.amountStaked.sub(
_amountToRemoveFromStaked
);
}
_updNumStaked(_amountToRemoveFromStaked, 'remove');
emit Withdraw(msg.sender, _amountToRemoveFromStaked);
}
functionemergencyUnstake() external{
StakerInfo memory _staker = stakers[msg.sender];
_removeFromVaultBackToPool(msg.sender);
uint256 _amountToRemoveFromStaked = _staker.amountStaked;
require(
_amountToRemoveFromStaked >0,
'user can only unstake if they have tokens in the pool'
);
_burn(
msg.sender,
_amountToRemoveFromStaked > balanceOf(msg.sender)
? balanceOf(msg.sender)
: _amountToRemoveFromStaked
);
if (pool.isStakedNft) {
for (uint256 _i =0; _i < _staker.nftTokenIds.length; _i++) {
_stakedERC721.transferFrom(
address(this),
msg.sender,
_staker.nftTokenIds[_i]
);
}
} else {
require(
_stakedERC20.transfer(msg.sender, _amountToRemoveFromStaked),
'unable to send user original tokens'
);
}
delete stakers[msg.sender];
_updNumStaked(_amountToRemoveFromStaked, 'remove');
emit Withdraw(msg.sender, _amountToRemoveFromStaked);
}
functionharvestForUser(address _userAddy, bool _autoCompound)
externalreturns (uint256)
{
require(
msg.sender== pool.creator ||msg.sender== _userAddy,
'can only harvest tokens for someone else if this was the contract creator'
);
_updatePool();
StakerInfo memory _staker = stakers[_userAddy];
uint256 _tokensToUser = _harvestTokens(
_userAddy,
block.timestamp>= _staker.timeOriginallyStaked.add(pool.stakeTimeLockSec)
);
if (
_autoCompound &&!pool.isStakedNft &&address(_rewardsToken) ==address(_stakedERC20)
) {
uint256[] memory _placeholder;
stakeTokens(_tokensToUser, _placeholder);
}
return _tokensToUser;
}
functiongetLastStakableBlock() publicviewreturns (uint256) {
uint256 _blockToAdd = pool.creationBlock ==0
? block.number
: pool.creationBlock;
return pool.poolTotalSupply.div(pool.perBlockNum).add(_blockToAdd);
}
functioncalcHarvestTot(address _userAddy) publicviewreturns (uint256) {
StakerInfo memory _staker = stakers[_userAddy];
if (
_staker.blockLastHarvested >=block.number||
_staker.blockOriginallyStaked ==0||
pool.totalTokensStaked ==0
) {
return0;
}
uint256 _accERC20PerShare = pool.accERC20PerShare;
if (block.number> pool.lastRewardBlock && pool.totalTokensStaked !=0) {
uint256 _endBlock = getLastStakableBlock();
uint256 _lastBlock =block.number< _endBlock ? block.number : _endBlock;
uint256 _nrOfBlocks = _lastBlock.sub(pool.lastRewardBlock);
uint256 _erc20Reward = _nrOfBlocks.mul(pool.perBlockNum);
_accERC20PerShare = _accERC20PerShare.add(
_erc20Reward.mul(1e36).div(pool.totalTokensStaked)
);
}
return
_staker.amountStaked.mul(_accERC20PerShare).div(1e36).sub(
_staker.rewardDebt
);
}
// Update reward variables of the given pool to be up-to-date.function_updatePool() private{
uint256 _endBlock = getLastStakableBlock();
uint256 _lastBlock =block.number< _endBlock ? block.number : _endBlock;
if (_lastBlock <= pool.lastRewardBlock) {
return;
}
uint256 _stakedSupply = pool.totalTokensStaked;
if (_stakedSupply ==0) {
pool.lastRewardBlock = _lastBlock;
return;
}
uint256 _nrOfBlocks = _lastBlock.sub(pool.lastRewardBlock);
uint256 _erc20Reward = _nrOfBlocks.mul(pool.perBlockNum);
pool.accERC20PerShare = pool.accERC20PerShare.add(
_erc20Reward.mul(1e36).div(_stakedSupply)
);
pool.lastRewardBlock = _lastBlock;
}
function_harvestTokens(address _userAddy, bool _sendRewards)
privatereturns (uint256)
{
StakerInfo storage _staker = stakers[_userAddy];
require(_staker.blockOriginallyStaked >0, 'user must have tokens staked');
uint256 _num2Trans = calcHarvestTot(_userAddy);
if (_num2Trans >0) {
if (_sendRewards) {
_sendRewardsToUser(_userAddy, _num2Trans);
} else {
rewardVault[_userAddy] += _num2Trans;
}
}
_staker.rewardDebt = _staker.amountStaked.mul(pool.accERC20PerShare).div(
1e36
);
_staker.blockLastHarvested =block.number;
return _num2Trans;
}
function_sendRewardsToUser(address _user, uint256 _amount) internal{
uint256 _totalToSend = _amount + rewardVault[_user];
rewardVault[_user] =0;
require(
_rewardsToken.transfer(_user, _totalToSend),
'unable to send user their harvested tokens'
);
pool.poolRemainingSupply = pool.poolRemainingSupply.sub(_totalToSend);
}
function_removeFromVaultBackToPool(address _user) internal{
uint256 _amountInVault = rewardVault[_user];
rewardVault[_user] =0;
pool.poolRemainingSupply = pool.poolRemainingSupply.add(_amountInVault);
}
// update the amount currently staked after a user harvestsfunction_updNumStaked(uint256 _amount, stringmemory _operation) private{
if (_compareStr(_operation, 'remove')) {
pool.totalTokensStaked = pool.totalTokensStaked.sub(_amount);
} else {
pool.totalTokensStaked = pool.totalTokensStaked.add(_amount);
}
}
function_compareStr(stringmemory a, stringmemory b)
privatepurereturns (bool)
{
return (keccak256(abi.encodePacked((a))) ==keccak256(abi.encodePacked((b))));
}
}
Contract Source Code
File 9 of 9: SafeMath.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/math/SafeMath.sol)pragmasolidity ^0.8.0;// CAUTION// This version of SafeMath should only be used with Solidity 0.8 or later,// because it relies on the compiler's built in overflow checks./**
* @dev Wrappers over Solidity's arithmetic operations.
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
*/librarySafeMath{
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryAdd(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontrySub(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryMul(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryDiv(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryMod(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/functionadd(uint256 a, uint256 b) internalpurereturns (uint256) {
return a + b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b) internalpurereturns (uint256) {
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/functionmul(uint256 a, uint256 b) internalpurereturns (uint256) {
return a * b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator.
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b) internalpurereturns (uint256) {
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b >0, errorMessage);
return a / b;
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b >0, errorMessage);
return a % b;
}
}
}