GHogRewardPool

Created Diff never expires
84 removals
Words removed158
Total words1010
Words removed (%)15.64
264 lines
287 additions
Words added649
Total words1501
Words added (%)43.24
455 lines
contract GScarabRewardPool {
contract GHogRewardPool is ReentrancyGuard {
using SafeMath for uint256;
using SafeMath for uint256;
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;


// governance
// governance
address public operator;
address public operator;


// Info of each user.
// Info of each user.
struct UserInfo {
struct UserInfo {
uint256 amount; // How many LP tokens the user has provided.
uint256 amount; // How many LP tokens the user has provided.
uint256 rewardDebt; // Reward debt. See explanation below.
uint256 rewardDebt; // Reward debt. See explanation below.
}
}


// Info of each pool.
// Info of each pool.
struct PoolInfo {
struct PoolInfo {
IERC20 token; // Address of LP token contract.
IERC20 token; // Address of LP token contract.
uint256 allocPoint; // How many allocation points assigned to this pool. tSHAREs to distribute per block.
uint256 withFee; // withdraw fee that is applied to created pool.
uint256 lastRewardTime; // Last time that tSHAREs distribution occurs.
uint256 allocPoint; // How many allocation points assigned to this pool. GHOGs to distribute per block.
uint256 accTSharePerShare; // Accumulated tSHAREs per share, times 1e18. See below.
uint256 lastRewardTime; // Last time that GHOGs distribution occurs.
uint256 accGhogPerShare; // Accumulated GHOGs per share, times 1e18. See below.
bool isStarted; // if lastRewardTime has passed
bool isStarted; // if lastRewardTime has passed
address gauge;
}
}


IERC20 public tshare;
IERC20 public ghog;

address public devFund;


// Info of each pool.
// Info of each pool.
PoolInfo[] public poolInfo;
PoolInfo[] public poolInfo;


// Info of each user that stakes LP tokens.
// Info of each user that stakes LP tokens.
mapping(uint256 => mapping(address => UserInfo)) public userInfo;
mapping(uint256 => mapping(address => UserInfo)) public userInfo;


// Total allocation points. Must be the sum of all allocation points in all pools.
// Total allocation points. Must be the sum of all allocation points in all pools.
uint256 public totalAllocPoint = 0;
uint256 public totalAllocPoint = 0;


// The time when tSHARE mining starts.
// The time when GHOG mining starts.
uint256 public poolStartTime;
uint256 public poolStartTime;


// The time when tSHARE mining ends.
// The time when GHOG mining ends.
uint256 public poolEndTime;
uint256 public poolEndTime;
uint256 public sharePerSecond = 0.00186122 ether;
uint256 public runningTime = 370 days;


uint256 public tSharePerSecond = 0.00186122 ether; // 59500 tshare / (370 days * 24h * 60min * 60s)
address public swapxToken = 0xA04BC7140c26fc9BB1F36B1A604C7A5a88fb0E70;
uint256 public runningTime = 370 days; // 370 days
uint256 public constant TOTAL_REWARDS = 59500 ether;


event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(
address indexed user,
uint256 indexed pid,
uint256 amount
);
event RewardPaid(address indexed user, uint256 amount);
event RewardPaid(address indexed user, uint256 amount);
event DevFundUpdated(address devFund);
event SharePerSecondUpdated(uint256 oldRate, uint256 newRate, uint256 timestamp);


constructor(
constructor(
address _tshare,
address _ghog,
address _hogS,
address _ghogS2,
address _devFund,
uint256 _poolStartTime
uint256 _poolStartTime
) public {
) {
require(block.timestamp < _poolStartTime, "late");
require(
if (_tshare != address(0)) tshare = IERC20(_tshare);
block.timestamp < _poolStartTime,
"pool cant be started in the past"
);
if (_ghog != address(0)) ghog = IERC20(_ghog);
if (_devFund != address(0)) devFund = _devFund;

poolStartTime = _poolStartTime;
poolStartTime = _poolStartTime;
poolEndTime = poolStartTime + runningTime;
poolEndTime = _poolStartTime + runningTime;
operator = msg.sender;
operator = msg.sender;
devFund = _devFund;

// create all the pools
add(0, 0, IERC20(_hogS), false, 0, address(0)); // Hog-S
add(0, 0, IERC20(_ghogS2), false, 0, address(0)); // GHog-S

}
}


modifier onlyOperator() {
modifier onlyOperator() {
require(operator == msg.sender, "TShareRewardPool: caller is not the operator");
require(
operator == msg.sender,
"GHogRewardPool: caller is not the operator"
);
_;
_;
}
}


function poolLength() external view returns (uint256) {
return poolInfo.length;
}

function checkPoolDuplicate(IERC20 _token) internal view {
function checkPoolDuplicate(IERC20 _token) internal view {
uint256 length = poolInfo.length;
uint256 length = poolInfo.length;
for (uint256 pid = 0; pid < length; ++pid) {
for (uint256 pid = 0; pid < length; ++pid) {
require(poolInfo[pid].token != _token, "TShareRewardPool: existing pool?");
require(
poolInfo[pid].token != _token,
"GHogRewardPool: existing pool?"
);
}
}
}
}


// Add a new lp to the pool. Can only be called by the owner.
// Add new lp to the pool. Can only be called by operator.
function add(
function add(
uint256 _allocPoint,
uint256 _allocPoint,
uint256 _withFee,
IERC20 _token,
IERC20 _token,
bool _withUpdate,
bool _withUpdate,
uint256 _lastRewardTime
uint256 _lastRewardTime,
address _gauge
) public onlyOperator {
) public onlyOperator {
checkPoolDuplicate(_token);
checkPoolDuplicate(_token);
if (_withUpdate) {
if (_withUpdate) {
massUpdatePools();
massUpdatePools();
}
}
if (block.timestamp < poolStartTime) {
if (block.timestamp < poolStartTime) {
// chef is sleeping
// chef is sleeping
if (_lastRewardTime == 0) {
if (_lastRewardTime == 0) {
_lastRewardTime = poolStartTime;
_lastRewardTime = poolStartTime;
} else {
} else {
if (_lastRewardTime < poolStartTime) {
if (_lastRewardTime < poolStartTime) {
_lastRewardTime = poolStartTime;
_lastRewardTime = poolStartTime;
}
}
}
}
} else {
} else {
// chef is cooking
// chef is cooking
if (_lastRewardTime == 0 || _lastRewardTime < block.timestamp) {
if (_lastRewardTime == 0 || _lastRewardTime < block.timestamp) {
_lastRewardTime = block.timestamp;
_lastRewardTime = block.timestamp;
}
}
}
}
bool _isStarted =
bool _isStarted = (_lastRewardTime <= poolStartTime) ||
(_lastRewardTime <= poolStartTime) ||
(_lastRewardTime <= block.timestamp);
(_lastRewardTime <= block.timestamp);
poolInfo.push(
poolInfo.push(PoolInfo({
PoolInfo({
token : _token,
token: _token,
allocPoint : _allocPoint,
withFee: _withFee,
lastRewardTime : _lastRewardTime,
allocPoint: _allocPoint,
accTSharePerShare : 0,
lastRewardTime: _lastRewardTime,
isStarted : _isStarted
accGhogPerShare: 0,
}));
isStarted: _isStarted,
gauge: _gauge
})
);

if (_isStarted) {
if (_isStarted) {
totalAllocPoint = totalAllocPoint.add(_allocPoint);
totalAllocPoint = totalAllocPoint.add(_allocPoint);
}
}
}
}


// Update the given pool's tSHARE allocation point. Can only be called by the owner.
// Update the given pool's GHOG allocation point. Can only be called by the operator.
function set(uint256 _pid, uint256 _allocPoint) public onlyOperator {
function set(
uint256 _pid,
uint256 _allocPoint,
uint256 _withFee,
address _gauge
) public onlyOperator {
massUpdatePools();
massUpdatePools();

PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
require(_withFee < 200); // withdraw fee cant be more than 2%;
pool.withFee = _withFee;

if (pool.isStarted) {
if (pool.isStarted) {
totalAllocPoint = totalAllocPoint.sub(pool.allocPoint).add(
totalAllocPoint = totalAllocPoint.sub(pool.allocPoint).add(
_allocPoint
_allocPoint
);
);
}
}
pool.allocPoint = _allocPoint;
pool.allocPoint = _allocPoint;
pool.gauge = _gauge;
updatePool(_pid);
}
}


// Return accumulate rewards over the given _from to _to block.
// AI-CONTROLLED: Updates the emission rate every 7 days based on protocol conditions
function getGeneratedReward(uint256 _fromTime, uint256 _toTime) public view returns (uint256) {
// This function allows the AI to adjust the reward distribution rate
function setSharePerSecond(uint256 _sharePerSecond) external onlyOperator {
uint256 oldSharePerSecond = sharePerSecond;
// Update to new rate
sharePerSecond = _sharePerSecond;
// Emit event for rate change
emit SharePerSecondUpdated(oldSharePerSecond, _sharePerSecond, block.timestamp);
massUpdatePools();
}

function getGeneratedReward(uint256 _fromTime, uint256 _toTime) public view returns (uint256) {

if (_fromTime >= _toTime) return 0;
if (_fromTime >= _toTime) return 0;

if (_toTime >= poolEndTime) {
if (_toTime >= poolEndTime) {

if (_fromTime >= poolEndTime) return 0;
if (_fromTime >= poolEndTime) return 0;
if (_fromTime <= poolStartTime) return poolEndTime.sub(poolStartTime).mul(tSharePerSecond);

return poolEndTime.sub(_fromTime).mul(tSharePerSecond);
if (_fromTime <= poolStartTime) return poolEndTime.sub(poolStartTime).mul(sharePerSecond);

return poolEndTime.sub(_fromTime).mul(sharePerSecond);

} else {
} else {

if (_toTime <= poolStartTime) return 0;
if (_toTime <= poolStartTime) return 0;
if (_fromTime <= poolStartTime) return _toTime.sub(poolStartTime).mul(tSharePerSecond);

return _toTime.sub(_fromTime).mul(tSharePerSecond);
if (_fromTime <= poolStartTime) return _toTime.sub(poolStartTime).mul(sharePerSecond);

return _toTime.sub(_fromTime).mul(sharePerSecond);
}
}
}
}


// View function to see pending tSHAREs on frontend.
// Modified pendingShare to use new getGeneratedReward
function pendingShare(uint256 _pid, address _user) external view returns (uint256) {
function pendingShare(uint256 _pid, address _user) public view returns (uint256) {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
UserInfo storage user = userInfo[_pid][_user];
uint256 accTSharePerShare = pool.accTSharePerShare;
uint256 accGhogPerShare = pool.accGhogPerShare;
uint256 tokenSupply = pool.token.balanceOf(address(this));
uint256 tokenSupply = pool.gauge != address(0) ? ISwapxGauge(pool.gauge).balanceOf(address(this)) : pool.token.balanceOf(address(this));
if (block.timestamp > pool.lastRewardTime && tokenSupply != 0) {
if (block.timestamp > pool.lastRewardTime && tokenSupply != 0) {
uint256 _generatedReward = getGeneratedReward(pool.lastRewardTime, block.timestamp);
uint256 _generatedReward = getGeneratedReward(
uint256 _tshareReward = _generatedReward.mul(pool.allocPoint).div(totalAllocPoint);
pool.lastRewardTime,
accTSharePerShare = accTSharePerShare.add(_tshareReward.mul(1e18).div(tokenSupply));
block.timestamp
);
uint256 _ghogReward = _generatedReward.mul(pool.allocPoint).div(totalAllocPoint);
accGhogPerShare = accGhogPerShare.add(_ghogReward.mul(1e18).div(tokenSupply));
}
}
return user.amount.mul(accTSharePerShare).div(1e18).sub(user.rewardDebt);
return user.amount.mul(accGhogPerShare).div(1e18).sub(user.rewardDebt);
}
}


// Update reward variables for all pools. Be careful of gas spending!
function massUpdatePools() public {
function massUpdatePools() public {
uint256 length = poolInfo.length;
uint256 length = poolInfo.length;
for (uint256 pid = 0; pid < length; ++pid) {
for (uint256 pid = 0; pid < length; ++pid) {
updatePool(pid);
updatePool(pid);
}
}
}
}


// Update reward variables of the given pool to be up-to-date.
// Update reward variables of the given pool to be up-to-date.
function updatePool(uint256 _pid) public {
function updatePool(uint256 _pid) public {
depositToGauge(_pid);
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
if (block.timestamp <= pool.lastRewardTime) {
if (block.timestamp <= pool.lastRewardTime) {
return;
return;
}
}
uint256 tokenSupply = pool.token.balanceOf(address(this));
uint256 tokenSupply = pool.gauge != address(0) ? ISwapxGauge(pool.gauge).balanceOf(address(this)) : pool.token.balanceOf(address(this));
if (tokenSupply == 0) {
if (tokenSupply == 0) {
pool.lastRewardTime = block.timestamp;
pool.lastRewardTime = block.timestamp;
return;
return;
}
}
if (!pool.isStarted) {
if (!pool.isStarted) {
pool.isStarted = true;
pool.isStarted = true;
totalAllocPoint = totalAllocPoint.add(pool.allocPoint);
totalAllocPoint = totalAllocPoint.add(pool.allocPoint);
}
}
if (totalAllocPoint > 0) {
if (totalAllocPoint > 0) {
uint256 _generatedReward = getGeneratedReward(pool.lastRewardTime, block.timestamp);
// This now correctly accounts for all emission rate changes
uint256 _tshareReward = _generatedReward.mul(pool.allocPoint).div(totalAllocPoint);
uint256 _generatedReward = getGeneratedReward(
pool.accTSharePerShare = pool.accTSharePerShare.add(_tshareReward.mul(1e18).div(tokenSupply));
pool.lastRewardTime,
block.timestamp
);
uint256 _ghogReward = _generatedReward.mul(pool.allocPoint).div(
totalAllocPoint
);
pool.accGhogPerShare = pool.accGhogPerShare.add(
_ghogReward.mul(1e18).div(tokenSupply)
);
}
}
pool.lastRewardTime = block.timestamp;
pool.lastRewardTime = block.timestamp;
}
}


// Deposit LP tokens.
// Deposit LP tokens.
function deposit(uint256 _pid, uint256 _amount) public {
function deposit(uint256 _pid, uint256 _amount) public nonReentrant {
address _sender = msg.sender;
address _sender = msg.sender;
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_sender];
UserInfo storage user = userInfo[_pid][_sender];
updatePool(_pid);
updatePool(_pid);
if (user.amount > 0) {
if (user.amount > 0) {
uint256 _pending = user.amount.mul(pool.accTSharePerShare).div(1e18).sub(user.rewardDebt);
uint256 _pending = user
.amount
.mul(pool.accGhogPerShare)
.div(1e18)
.sub(user.rewardDebt);
if (_pending > 0) {
if (_pending > 0) {
safeTShareTransfer(_sender, _pending);
safeGhogTransfer(_sender, _pending);
emit RewardPaid(_sender, _pending);
emit RewardPaid(_sender, _pending);
}
}
}
}
if (_amount > 0) {
if (_amount > 0) {
pool.token.safeTransferFrom(_sender, address(this), _amount);
pool.token.safeTransferFrom(_sender, address(this), _amount);
user.amount = user.amount.add(_amount);
user.amount = user.amount.add(_amount);
depositToGauge(_pid);
}
}
user.rewardDebt = user.amount.mul(pool.accTSharePerShare).div(1e18);
user.rewardDebt = user.amount.mul(pool.accGhogPerShare).div(1e18);
emit Deposit(_sender, _pid, _amount);
emit Deposit(_sender, _pid, _amount);
}
}


// Withdraw LP tokens.
// Withdraw LP tokens.
function withdraw(uint256 _pid, uint256 _amount) public {
function withdraw(uint256 _pid, uint256 _amount) public nonReentrant {
address _sender = msg.sender;
address _sender = msg.sender;
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_sender];
UserInfo storage user = userInfo[_pid][_sender];
require(user.amount >= _amount, "withdraw: not good");
require(user.amount >= _amount, "withdraw: not good");
updatePool(_pid);
updatePool(_pid);
uint256 _pending = user.amount.mul(pool.accTSharePerShare).div(1e18).sub(user.rewardDebt);
uint256 _pending = user.amount.mul(pool.accGhogPerShare).div(1e18).sub(
user.rewardDebt
);
if (_pending > 0) {
if (_pending > 0) {
safeTShareTransfer(_sender, _pending);
safeGhogTransfer(_sender, _pending);
emit RewardPaid(_sender, _pending);
emit RewardPaid(_sender, _pending);
}
}
if (_amount > 0) {
if (_amount > 0) {
user.amount = user.amount.sub(_amount);
user.amount = user.amount.sub(_amount);
pool.token.safeTransfer(_sender, _amount);
withdrawFromGauge(_pid, _amount);
// Calculate the fee and transfer it to the devFund
uint256 fee = _amount.mul(pool.withFee).div(10000); // Assuming withFee is in basis points (e.g., 100 = 1%)
uint256 amountAfterFee = _amount.sub(fee);

if (fee > 0) {
pool.token.safeTransfer(devFund, fee);
}

pool.token.safeTransfer(_sender, amountAfterFee);
}
}
user.rewardDebt = user.amount.mul(pool.accTSharePerShare).div(1e18);
user.rewardDebt = user.amount.mul(pool.accGhogPerShare).div(1e18);
emit Withdraw(_sender, _pid, _amount);
emit Withdraw(_sender, _pid, _amount);
}
}


// Withdraw without caring about rewards. EMERGENCY ONLY.
// Withdraw without caring about rewards. EMERGENCY ONLY.
function emergencyWithdraw(uint256 _pid) public {
function emergencyWithdraw(uint256 _pid) public nonReentrant {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
UserInfo storage user = userInfo[_pid][msg.sender];
uint256 _amount = user.amount;
uint256 _amount = user.amount;
user.amount = 0;
user.amount = 0;
user.rewardDebt = 0;
user.rewardDebt = 0;
pool.token.safeTransfer(msg.sender, _amount);
withdrawFromGauge(_pid, _amount);

uint256 fee = _amount.mul(pool.withFee).div(10000); // Assuming withFee is in basis points (e.g., 100 = 1%)
uint256 amountAfterFee = _amount.sub(fee);

if (fee > 0) {
pool.token.safeTransfer(devFund, fee);
}

pool.token.safeTransfer(msg.sender, amountAfterFee);

emit EmergencyWithdraw(msg.sender, _pid, _amount);
emit EmergencyWithdraw(msg.sender, _pid, _amount);
}
}


// Safe tshare transfer function, just in case if rounding error causes pool to not have enough tSHAREs.
// Safe ghog transfer function, just in case if rounding error causes pool to not have enough Ghog.
function safeTShareTransfer(address _to, uint256 _amount) internal {
function safeGhogTransfer(address _to, uint256 _amount) internal {
uint256 _tshareBal = tshare.balanceOf(address(this));
uint256 _ghogBal = ghog.balanceOf(address(this));
if (_tshareBal > 0) {

if (_amount > _tshareBal) {
if (_ghogBal > 0) {
tshare.safeTransfer(_to, _tshareBal);
if (_amount > _ghogBal) {
ghog.safeTransfer(_to, _ghogBal);
} else {
} else {
tshare.safeTransfer(_to, _amount);
ghog.safeTransfer(_to, _amount);
}
}
}
}
}
}


function setOperator(address _operator) external onlyOperator {
function setOperator(address _operator) external onlyOperator {
operator = _operator;
operator = _operator;
}
}


function governanceRecoverUnsupported(IERC20 _token, uint256 amount, address to) external onlyOperator {
function setDevFund(address _devFund) external onlyOperator {
if (block.timestamp < poolEndTime + 90 days) {
require(_devFund != address(0), "Cannot set devFund to zero address");
devFund = _devFund;
emit DevFundUpdated(_devFund);
}

function governanceRecoverUnsupported(
IERC20 _token,
uint256 amount,
address to
) external onlyOperator {
if (block.timestamp < poolEndTime + 10 days) {
// do not allow to drain core token (tSHARE or lps) if less than 90 days after pool ends
// do not allow to drain core token (tSHARE or lps) if less than 90 days after pool ends
require(_token != tshare, "tshare");

require(_token != ghog, "ghog");

uint256 length = poolInfo.length;
uint256 length = poolInfo.length;

for (uint256 pid = 0; pid < length; ++pid) {
for (uint256 pid = 0; pid < length; ++pid) {
PoolInfo storage pool = poolInfo[pid];
PoolInfo storage pool = poolInfo[pid];

require(_token != pool.token, "pool.token");
require(_token != pool.token, "pool.token");
}
}
}
}

_token.safeTransfer(to, amount);
_token.safeTransfer(to, amount);
}
}

// Calculate total GHOG emitted from pool start until now
function getTotalEmittedShares() public view returns (uint256) {
if (block.timestamp <= poolStartTime) return 0;
uint256 endTime = block.timestamp;
if (endTime > poolEndTime) {
endTime = poolEndTime;
}
return getGeneratedReward(poolStartTime, endTime);
}

// Calculate total GHOG emitted between any two timestamps
function getTotalEmittedSharesBetween(uint256 _fromTime, uint256 _toTime) public view returns (uint256) {
require(_fromTime <= _toTime, "Invalid time range");
// Bound times within pool's active period
if (_fromTime < poolStartTime) {
_fromTime = poolStartTime;
}
if (_toTime > poolEndTime) {
_toTime = poolEndTime;
}
return getGeneratedReward(_fromTime, _toTime);
}

function depositToGauge(uint256 _pid) internal {
PoolInfo storage pool = poolInfo[_pid];
address gauge = pool.gauge;
uint256 balance = pool.token.balanceOf(address(this));
// Do nothing if this pool doesn't have a gauge
if (pool.gauge != address(0)) {
// Do nothing if the LP token in the MC is empty
if (balance > 0) {
// Approve to the gauge
if (pool.token.allowance(address(this), gauge) < balance ){
pool.token.approve(gauge, type(uint256).max);
}
ISwapxGauge(pool.gauge).deposit(balance);
}
}
}

function claimSwapxRewards(uint256 _pid) public onlyOperator {
PoolInfo storage pool = poolInfo[_pid];
ISwapxGauge(pool.gauge).getReward(); // claim the swapx rewards
IERC20 rewardToken = IERC20(swapxToken);
uint256 rewardAmount = rewardToken.balanceOf(address(this));
if (rewardAmount > 0) {
rewardToken.safeTransfer(devFund, rewardAmount);
}
}

function withdrawFromGauge(uint256 _pid, uint256 _amount) internal {
PoolInfo storage pool = poolInfo[_pid];
// Do nothing if this pool doesn't have a gauge
if (pool.gauge != address(0)) {
// Withdraw from the gauge
ISwapxGauge(pool.gauge).withdraw(_amount);
}
}
}
}