Verified contractPartialGPL-3.0
MasterChef
Address0xc0464dcbe84ea67550e948d2c78193d85f2bde44
- Balance
0 ETH
- Transactions
4
- Compiler
solc 0.8.18
- Same code
Only this contract
Details
- Compiler
- solc 0.8.18+commit.87f61d96
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- GPL-3.0
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 16,992,718
- Creation transaction
- 0x53f7901445…aba3913811
MasterChef.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @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.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// 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/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts 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.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts 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.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts 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.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message 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.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract MasterChef is Ownable {
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event Claim(address user, uint256 pid, uint256 amount);
event ClaimAll(address user, uint256 amount);
struct UserInfo {
uint256 amount; // How many LP tokens the user has provided.
uint256 rewardDebt; // Reward debt. See explanation below.
}
struct PoolInfo {
IERC20 lpToken; // Address of LP token contract.
uint256 allocPoint; // How many allocation points assigned to this pool. Rewards to distribute per block.
uint256 lastRewardBlock; // Last block number that Rewards distribution occurs.
uint256 accRewardPerShare; // Accumulated Rewards per share, times 1e36. See below.
}
struct EpochInfo {
uint256 startBlock;
uint256 endBlock;
uint256 rewardPerBlock;
}
IERC20 public loong;
EpochInfo public epoch;
PoolInfo[] public poolInfo;
mapping(uint256 => mapping(address => UserInfo)) public userInfo;
uint256 public totalAllocPoint;
mapping(address => uint256) private userDebit; // user's debits
mapping(address => uint256) private userRewards; // user's rewards
constructor() {}
function setLoongToken(address _loong) external onlyOwner {
loong = IERC20(_loong);
}
function newEpoch(uint256 _startBlock, uint256 _endBlock, uint256 _rewardPerBlock) external onlyOwner {
require(block.number != _blockNumber(), "epoch ongoing");
require(_startBlock > epoch.endBlock, "Invalid start block");
require(_endBlock > _startBlock, "Invalid end block");
require(_rewardPerBlock > 0, "Invalid reward per block");
_massUpdatePools();
epoch.startBlock = _startBlock;
epoch.endBlock = _endBlock;
epoch.rewardPerBlock = _rewardPerBlock;
}
receive() external payable {}
function saveDebit(address sender, uint256 amount) external {
require(msg.sender == address(loong));
userDebit[sender] += amount;
}
function saveReward(address sender, uint256 amount) external {
userRewards[sender] += amount;
}
function add(
uint256 _allocPoint,
IERC20 _lpToken,
bool _withUpdate
) external onlyOwner {
if (_withUpdate) {
_massUpdatePools();
}
uint256 lastRewardBlock = _blockNumber();
totalAllocPoint = totalAllocPoint + _allocPoint;
poolInfo.push(
PoolInfo({
lpToken: _lpToken,
allocPoint: _allocPoint,
lastRewardBlock: lastRewardBlock,
accRewardPerShare: 0
})
);
}
function set(
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate
) external onlyOwner {
if (_withUpdate) {
_massUpdatePools();
}
uint256 prevAllocPoint = poolInfo[_pid].allocPoint;
poolInfo[_pid].allocPoint = _allocPoint;
if (prevAllocPoint != _allocPoint) {
totalAllocPoint = totalAllocPoint - prevAllocPoint + _allocPoint;
}
}
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
function pendingReward(uint256 _pid, address _user)
external
view
returns (uint256)
{
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accRewardPerShare = pool.accRewardPerShare;
uint256 blockNumber = _blockNumber();
uint256 lastRewardBlock = _normalizeBlockNumber(pool.lastRewardBlock);
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (blockNumber > lastRewardBlock && lpSupply != 0) {
uint256 reward = ((blockNumber - lastRewardBlock) *
epoch.rewardPerBlock *
pool.allocPoint) / totalAllocPoint;
accRewardPerShare =
accRewardPerShare +
(reward * (1e36)) /
lpSupply;
}
return
userRewards[_user] +
(user.amount * accRewardPerShare) /
(1e36) -
user.rewardDebt;
}
function deposit(uint256 _pid, uint256 _amount) external {
require(_amount > 0, "invalid_amount");
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
_updatePool(_pid);
_withdrawReward(_pid);
pool.lpToken.transferFrom(msg.sender, address(this), _amount);
user.amount = user.amount + _amount;
user.rewardDebt = (user.amount * pool.accRewardPerShare) / (1e36);
emit Deposit(msg.sender, _pid, _amount);
}
function withdraw(uint256 _pid, uint256 _amount) external {
require(_amount > 0, "invalid_amount");
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
require(user.amount >= _amount, "insufficient_amount");
_updatePool(_pid);
_withdrawReward(_pid);
user.amount -= _amount;
pool.lpToken.transfer(address(msg.sender), _amount);
user.rewardDebt = (user.amount * pool.accRewardPerShare) / (1e36);
emit Withdraw(msg.sender, _pid, _amount);
}
function _blockNumber() internal view returns (uint256) {
return _normalizeBlockNumber(block.number);
}
function _normalizeBlockNumber(uint256 blockNumber) internal view returns (uint256) {
if (blockNumber < epoch.startBlock)
return epoch.startBlock;
if (blockNumber > epoch.endBlock)
return epoch.endBlock;
return blockNumber;
}
// Update reward variables for all pools. Be careful of gas spending!
function _massUpdatePools() internal {
uint256 length = poolInfo.length;
for (uint256 pid = 0; pid < length; ++pid) {
_updatePool(pid);
}
}
// Update reward variables of the given pool to be up-to-date.
function _updatePool(uint256 _pid) internal {
PoolInfo storage pool = poolInfo[_pid];
uint256 blockNumber = _blockNumber();
uint256 lastRewardBlock = _normalizeBlockNumber(pool.lastRewardBlock);
if (blockNumber <= lastRewardBlock) {
return;
}
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (lpSupply == 0) {
pool.lastRewardBlock = blockNumber;
return;
}
uint256 reward = ((blockNumber - lastRewardBlock) *
epoch.rewardPerBlock *
pool.allocPoint) / totalAllocPoint;
pool.accRewardPerShare =
pool.accRewardPerShare +
(reward * (1e36)) /
lpSupply;
pool.lastRewardBlock = blockNumber;
}
function _withdrawReward(uint256 _pid) internal returns (uint256) {
UserInfo memory user = userInfo[_pid][msg.sender];
uint256 pending = (user.amount * poolInfo[_pid].accRewardPerShare) /
(1e36) -
user.rewardDebt;
if (pending > 0) {
userRewards[msg.sender] += pending;
}
return pending;
}
function claim(uint256 _pid) external {
_updatePool(_pid);
_withdrawReward(_pid);
uint256 rewards = userRewards[msg.sender];
require(rewards > 0, "no_reward");
loong.transfer(msg.sender, rewards);
userRewards[msg.sender] = 0;
userInfo[_pid][msg.sender].rewardDebt =
(userInfo[_pid][msg.sender].amount *
poolInfo[_pid].accRewardPerShare) /
(1e36);
emit Claim(msg.sender, _pid, rewards);
}
function claimAll() external {
for (uint256 i = 0; i < poolInfo.length; i++) {
_updatePool(i);
_withdrawReward(i);
userInfo[i][msg.sender].rewardDebt =
(userInfo[i][msg.sender].amount *
poolInfo[i].accRewardPerShare) /
(1e36);
}
uint256 rewards = userRewards[msg.sender];
require(rewards > 0, "no_reward");
loong.transfer(msg.sender, rewards);
userRewards[msg.sender] = 0;
emit ClaimAll(msg.sender, rewards);
}
function withdrawDust() external onlyOwner {
payable(msg.sender).transfer(address(this).balance);
}
}Compiler settingsShow
{
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
}
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.