contracts-imtbl/contracts/staking/RewardRouter.sol
2024-09-10 20:42:29 +08:00

77 lines
2.8 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IRewardTracker} from "./interfaces/IRewardTracker.sol";
import {IVester} from "./interfaces/IVester.sol";
import {Governable} from "../core/Governable.sol";
contract RewardRouter is ReentrancyGuard, Governable {
using SafeERC20 for IERC20;
address public cec;
address public stakedCecTracker;
address public cecVester;
event StakeCec(address indexed account, address indexed token, uint256 amount);
event UnstakeCec(address indexed account, address indexed token, uint256 amount);
constructor(address _cec, address _stakedCecTracker, address _cecVester) {
cec = _cec;
stakedCecTracker = _stakedCecTracker;
cecVester = _cecVester;
}
// to help users who accidentally send their tokens to this contract
function withdrawToken(address _token, address _account, uint256 _amount) external onlyGov {
IERC20(_token).safeTransfer(_account, _amount);
}
function batchStakeCecForAccount(
address[] memory _accounts,
uint256[] memory _amounts
) external nonReentrant onlyGov {
for (uint256 i = 0; i < _accounts.length; i++) {
_stakeCec(msg.sender, _accounts[i], cec, _amounts[i]);
}
}
function stakeCec(uint256 _amount) external nonReentrant {
_stakeCec(msg.sender, msg.sender, cec, _amount);
}
function unstakeCec(uint256 _amount) external nonReentrant {
// check if the user has staked CEC in the vester
if (cecVester != address(0) && IVester(cecVester).needCheckStake()) {
IVester(cecVester).updateVesting(msg.sender);
require(IERC20(cecVester).balanceOf(msg.sender) + _amount <= IRewardTracker(stakedCecTracker).depositBalances(msg.sender, cec), "RewardRouter: insufficient CEC balance");
}
_unstakeCec(msg.sender, cec, _amount);
}
function claim() external nonReentrant {
address account = msg.sender;
IRewardTracker(stakedCecTracker).claimForAccount(account, account);
}
function _stakeCec(address _fundingAccount, address _account, address _token, uint256 _amount) private {
require(_amount > 0, "invalid _amount");
IRewardTracker(stakedCecTracker).stakeForAccount(_fundingAccount, _account, _token, _amount);
emit StakeCec(_account, _token, _amount);
}
function _unstakeCec(address _account, address _token, uint256 _amount) private {
require(_amount > 0, "invalid _amount");
// uint256 balance = IRewardTracker(stakedCecTracker).stakedAmounts(_account);
IRewardTracker(stakedCecTracker).unstakeForAccount(_account, _token, _amount, _account);
emit UnstakeCec(_account, _token, _amount);
}
}