文件 1 的 15:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) private pure returns (bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 15:IERC20.sol
pragma solidity ^0.8.0;
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);
}
文件 3 的 15:IERC20Burnable.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20Burnable is IERC20 {
function burn(uint256 amount) external returns (bool);
}
文件 4 的 15:IERC20Extended.sol
pragma solidity ^0.8.0;
import "./IERC20Metadata.sol";
import "./IERC20Mintable.sol";
import "./IERC20Burnable.sol";
import "./IERC20Permit.sol";
import "./IERC20TransferWithAuth.sol";
import "./IERC20SafeAllowance.sol";
interface IERC20Extended is
IERC20Metadata,
IERC20Mintable,
IERC20Burnable,
IERC20Permit,
IERC20TransferWithAuth,
IERC20SafeAllowance
{}
文件 5 的 15:IERC20Metadata.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 6 的 15:IERC20Mintable.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20Mintable is IERC20 {
function mint(address dst, uint256 amount) external returns (bool);
}
文件 7 的 15:IERC20Permit.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20Permit is IERC20 {
function getDomainSeparator() external view returns (bytes32);
function DOMAIN_TYPEHASH() external view returns (bytes32);
function VERSION_HASH() external view returns (bytes32);
function PERMIT_TYPEHASH() external view returns (bytes32);
function nonces(address) external view returns (uint);
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
}
文件 8 的 15:IERC20SafeAllowance.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20SafeAllowance is IERC20 {
function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
}
文件 9 的 15:IERC20TransferWithAuth.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IERC20TransferWithAuth is IERC20 {
function transferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) external;
function receiveWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) external;
function TRANSFER_WITH_AUTHORIZATION_TYPEHASH() external view returns (bytes32);
function RECEIVE_WITH_AUTHORIZATION_TYPEHASH() external view returns (bytes32);
event AuthorizationUsed(address indexed authorizer, bytes32 indexed nonce);
}
文件 10 的 15:ILockManager.sol
pragma solidity ^0.8.0;
interface ILockManager {
struct LockedStake {
uint256 amount;
uint256 votingPower;
}
function getAmountStaked(address staker, address stakedToken) external view returns (uint256);
function getStake(address staker, address stakedToken) external view returns (LockedStake memory);
function calculateVotingPower(address token, uint256 amount) external view returns (uint256);
function grantVotingPower(address receiver, address token, uint256 tokenAmount) external returns (uint256 votingPowerGranted);
function removeVotingPower(address receiver, address token, uint256 tokenAmount) external returns (uint256 votingPowerRemoved);
}
文件 11 的 15:IMasterChef.sol
pragma solidity ^0.8.0;
import "./IERC20.sol";
interface IMasterChef {
struct PoolInfo {
IERC20 lpToken;
uint256 allocPoint;
uint256 lastRewardBlock;
uint256 accSushiPerShare;
}
function deposit(uint256 _pid, uint256 _amount) external;
function withdraw(uint256 _pid, uint256 _amount) external;
function poolInfo(uint256 _pid) external view returns (PoolInfo memory);
function pendingSushi(uint256 _pid, address _user) external view returns (uint256);
function updatePool(uint256 _pid) external;
function sushiPerBlock() external view returns (uint256);
function totalAllocPoint() external view returns (uint256);
function getMultiplier(uint256 _from, uint256 _to) external view returns (uint256);
}
文件 12 的 15:IVault.sol
pragma solidity ^0.8.0;
interface IVault {
struct Lock {
address token;
address receiver;
uint48 startTime;
uint16 vestingDurationInDays;
uint16 cliffDurationInDays;
uint256 amount;
uint256 amountClaimed;
uint256 votingPower;
}
struct LockBalance {
uint256 id;
uint256 claimableAmount;
Lock lock;
}
struct TokenBalance {
uint256 totalAmount;
uint256 claimableAmount;
uint256 claimedAmount;
uint256 votingPower;
}
function lockTokens(address token, address locker, address receiver, uint48 startTime, uint256 amount, uint16 lockDurationInDays, uint16 cliffDurationInDays, bool grantVotingPower) external;
function lockTokensWithPermit(address token, address locker, address receiver, uint48 startTime, uint256 amount, uint16 lockDurationInDays, uint16 cliffDurationInDays, bool grantVotingPower, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
function claimUnlockedTokenAmounts(uint256[] memory lockIds, uint256[] memory amounts) external;
function claimAllUnlockedTokens(uint256[] memory lockIds) external;
function tokenLocks(uint256 lockId) external view returns(Lock memory);
function allActiveLockIds() external view returns(uint256[] memory);
function allActiveLocks() external view returns(Lock[] memory);
function allActiveLockBalances() external view returns(LockBalance[] memory);
function activeLockIds(address receiver) external view returns(uint256[] memory);
function allLocks(address receiver) external view returns(Lock[] memory);
function activeLocks(address receiver) external view returns(Lock[] memory);
function activeLockBalances(address receiver) external view returns(LockBalance[] memory);
function totalTokenBalance(address token) external view returns(TokenBalance memory balance);
function tokenBalance(address token, address receiver) external view returns(TokenBalance memory balance);
function lockBalance(uint256 lockId) external view returns (LockBalance memory);
function claimableBalance(uint256 lockId) external view returns (uint256);
function extendLock(uint256 lockId, uint16 vestingDaysToAdd, uint16 cliffDaysToAdd) external;
}
文件 13 的 15:ReentrancyGuard.sol
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 14 的 15:RewardsManager.sol
pragma solidity ^0.8.0;
import "./interfaces/IMasterChef.sol";
import "./interfaces/IVault.sol";
import "./interfaces/ILockManager.sol";
import "./interfaces/IERC20Extended.sol";
import "./lib/SafeERC20.sol";
import "./lib/ReentrancyGuard.sol";
contract RewardsManager is ReentrancyGuard {
using SafeERC20 for IERC20Extended;
address public owner;
struct UserInfo {
uint256 amount;
uint256 rewardTokenDebt;
uint256 sushiRewardDebt;
}
struct PoolInfo {
IERC20Extended token;
uint256 allocPoint;
uint256 lastRewardBlock;
uint256 accRewardsPerShare;
uint32 vestingPercent;
uint16 vestingPeriod;
uint16 vestingCliff;
uint256 totalStaked;
bool vpForDeposit;
bool vpForVesting;
}
IERC20Extended public rewardToken;
IERC20Extended public sushiToken;
IMasterChef public masterChef;
IVault public vault;
ILockManager public lockManager;
uint256 public rewardTokensPerBlock;
PoolInfo[] public poolInfo;
mapping (address => uint256) public sushiPools;
mapping (uint256 => mapping (address => UserInfo)) public userInfo;
uint256 public totalAllocPoint;
uint256 public startBlock;
modifier onlyOwner {
require(msg.sender == owner, "not owner");
_;
}
event Deposit(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 PoolAdded(uint256 indexed pid, address indexed token, uint256 allocPoints, uint256 totalAllocPoints, uint256 rewardStartBlock, uint256 sushiPid, bool vpForDeposit, bool vpForVesting);
event PoolUpdated(uint256 indexed pid, uint256 oldAllocPoints, uint256 newAllocPoints, uint256 newTotalAllocPoints);
event ChangedOwner(address indexed oldOwner, address indexed newOwner);
event ChangedRewardTokensPerBlock(uint256 indexed oldRewardTokensPerBlock, uint256 indexed newRewardTokensPerBlock);
event SetRewardsStartBlock(uint256 indexed startBlock);
event ChangedAddress(string indexed addressType, address indexed oldAddress, address indexed newAddress);
constructor(
address _owner,
address _lockManager,
address _vault,
address _rewardToken,
address _sushiToken,
address _masterChef,
uint256 _startBlock,
uint256 _rewardTokensPerBlock
) {
owner = _owner;
emit ChangedOwner(address(0), _owner);
lockManager = ILockManager(_lockManager);
emit ChangedAddress("LOCK_MANAGER", address(0), _lockManager);
vault = IVault(_vault);
emit ChangedAddress("VAULT", address(0), _vault);
rewardToken = IERC20Extended(_rewardToken);
emit ChangedAddress("REWARD_TOKEN", address(0), _rewardToken);
sushiToken = IERC20Extended(_sushiToken);
emit ChangedAddress("SUSHI_TOKEN", address(0), _sushiToken);
masterChef = IMasterChef(_masterChef);
emit ChangedAddress("MASTER_CHEF", address(0), _masterChef);
startBlock = _startBlock == 0 ? block.number : _startBlock;
emit SetRewardsStartBlock(startBlock);
rewardTokensPerBlock = _rewardTokensPerBlock;
emit ChangedRewardTokensPerBlock(0, _rewardTokensPerBlock);
rewardToken.safeIncreaseAllowance(address(vault), type(uint256).max);
}
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
function add(
uint256 allocPoint,
address token,
uint32 vestingPercent,
uint16 vestingPeriod,
uint16 vestingCliff,
bool withUpdate,
uint256 sushiPid,
bool vpForDeposit,
bool vpForVesting
) external onlyOwner {
if (withUpdate) {
massUpdatePools();
}
uint256 rewardStartBlock = block.number > startBlock ? block.number : startBlock;
totalAllocPoint = totalAllocPoint + allocPoint;
poolInfo.push(PoolInfo({
token: IERC20Extended(token),
allocPoint: allocPoint,
lastRewardBlock: rewardStartBlock,
accRewardsPerShare: 0,
vestingPercent: vestingPercent,
vestingPeriod: vestingPeriod,
vestingCliff: vestingCliff,
totalStaked: 0,
vpForDeposit: vpForDeposit,
vpForVesting: vpForVesting
}));
if (sushiPid != uint256(0)) {
sushiPools[token] = sushiPid;
IERC20Extended(token).safeIncreaseAllowance(address(masterChef), type(uint256).max);
}
IERC20Extended(token).safeIncreaseAllowance(address(vault), type(uint256).max);
emit PoolAdded(poolInfo.length - 1, token, allocPoint, totalAllocPoint, rewardStartBlock, sushiPid, vpForDeposit, vpForVesting);
}
function set(
uint256 pid,
uint256 allocPoint,
bool withUpdate
) external onlyOwner {
if (withUpdate) {
massUpdatePools();
}
totalAllocPoint = totalAllocPoint - poolInfo[pid].allocPoint + allocPoint;
emit PoolUpdated(pid, poolInfo[pid].allocPoint, allocPoint, totalAllocPoint);
poolInfo[pid].allocPoint = allocPoint;
}
function rewardsActive() public view returns (bool) {
return block.number >= startBlock && totalAllocPoint > 0 ? true : false;
}
function getMultiplier(uint256 from, uint256 to) public pure returns (uint256) {
return to > from ? to - from : 0;
}
function pendingRewardTokens(uint256 pid, address account) external view returns (uint256) {
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][account];
uint256 accRewardsPerShare = pool.accRewardsPerShare;
uint256 tokenSupply = pool.totalStaked;
if (block.number > pool.lastRewardBlock && tokenSupply != 0) {
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
uint256 totalReward = multiplier * rewardTokensPerBlock * pool.allocPoint / totalAllocPoint;
accRewardsPerShare = accRewardsPerShare + totalReward * 1e12 / tokenSupply;
}
uint256 accumulatedRewards = user.amount * accRewardsPerShare / 1e12;
if (accumulatedRewards < user.rewardTokenDebt) {
return 0;
}
return accumulatedRewards - user.rewardTokenDebt;
}
function pendingSushi(uint256 pid, address account) external view returns (uint256) {
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][account];
uint256 sushiPid = sushiPools[address(pool.token)];
if (sushiPid == uint256(0)) {
return 0;
}
IMasterChef.PoolInfo memory sushiPool = masterChef.poolInfo(sushiPid);
uint256 sushiPerBlock = masterChef.sushiPerBlock();
uint256 totalSushiAllocPoint = masterChef.totalAllocPoint();
uint256 accSushiPerShare = sushiPool.accSushiPerShare;
uint256 lpSupply = sushiPool.lpToken.balanceOf(address(masterChef));
if (block.number > sushiPool.lastRewardBlock && lpSupply != 0) {
uint256 multiplier = masterChef.getMultiplier(sushiPool.lastRewardBlock, block.number);
uint256 sushiReward = multiplier * sushiPerBlock * sushiPool.allocPoint / totalSushiAllocPoint;
accSushiPerShare = accSushiPerShare + sushiReward * 1e12 / lpSupply;
}
uint256 accumulatedSushi = user.amount * accSushiPerShare / 1e12;
if (accumulatedSushi < user.sushiRewardDebt) {
return 0;
}
return accumulatedSushi - user.sushiRewardDebt;
}
function massUpdatePools() public {
for (uint256 pid = 0; pid < poolInfo.length; ++pid) {
updatePool(pid);
}
}
function updatePool(uint256 pid) public {
PoolInfo storage pool = poolInfo[pid];
if (block.number <= pool.lastRewardBlock) {
return;
}
uint256 tokenSupply = pool.totalStaked;
if (tokenSupply == 0) {
pool.lastRewardBlock = block.number;
return;
}
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
uint256 totalReward = multiplier * rewardTokensPerBlock * pool.allocPoint / totalAllocPoint;
pool.accRewardsPerShare = pool.accRewardsPerShare + totalReward * 1e12 / tokenSupply;
pool.lastRewardBlock = block.number;
}
function deposit(uint256 pid, uint256 amount) external nonReentrant {
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][msg.sender];
_deposit(pid, amount, pool, user);
}
function depositWithPermit(
uint256 pid,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external nonReentrant {
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][msg.sender];
pool.token.permit(msg.sender, address(this), amount, deadline, v, r, s);
_deposit(pid, amount, pool, user);
}
function withdraw(uint256 pid, uint256 amount) external nonReentrant {
require(amount > 0, "RM::withdraw: amount must be > 0");
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][msg.sender];
_withdraw(pid, amount, pool, user);
}
function emergencyWithdraw(uint256 pid) external nonReentrant {
PoolInfo storage pool = poolInfo[pid];
UserInfo storage user = userInfo[pid][msg.sender];
if (user.amount > 0) {
uint256 sushiPid = sushiPools[address(pool.token)];
if (sushiPid != uint256(0)) {
masterChef.withdraw(sushiPid, user.amount);
}
if (pool.vpForDeposit) {
lockManager.removeVotingPower(msg.sender, address(pool.token), user.amount);
}
pool.totalStaked = pool.totalStaked - user.amount;
pool.token.safeTransfer(msg.sender, user.amount);
emit EmergencyWithdraw(msg.sender, pid, user.amount);
user.amount = 0;
user.rewardTokenDebt = 0;
user.sushiRewardDebt = 0;
}
}
function tokenAllow(
address[] memory tokensToApprove,
uint256[] memory approvalAmounts,
address spender
) external onlyOwner {
require(tokensToApprove.length == approvalAmounts.length, "RM::tokenAllow: not same length");
for(uint i = 0; i < tokensToApprove.length; i++) {
IERC20Extended token = IERC20Extended(tokensToApprove[i]);
if (token.allowance(address(this), spender) != type(uint256).max) {
token.safeApprove(spender, approvalAmounts[i]);
}
}
}
function rescueTokens(
address[] calldata tokens,
uint256[] calldata amounts,
address receiver
) external onlyOwner {
require(tokens.length == amounts.length, "RM::rescueTokens: not same length");
for (uint i = 0; i < tokens.length; i++) {
IERC20Extended token = IERC20Extended(tokens[i]);
uint256 withdrawalAmount;
uint256 tokenBalance = token.balanceOf(address(this));
uint256 tokenAllowance = token.allowance(address(this), receiver);
if (amounts[i] == 0) {
if (tokenBalance > tokenAllowance) {
withdrawalAmount = tokenAllowance;
} else {
withdrawalAmount = tokenBalance;
}
} else {
require(tokenBalance >= amounts[i], "RM::rescueTokens: contract balance too low");
require(tokenAllowance >= amounts[i], "RM::rescueTokens: increase token allowance");
withdrawalAmount = amounts[i];
}
token.safeTransferFrom(address(this), receiver, withdrawalAmount);
}
}
function setRewardsPerBlock(uint256 newRewardTokensPerBlock) external onlyOwner {
emit ChangedRewardTokensPerBlock(rewardTokensPerBlock, newRewardTokensPerBlock);
rewardTokensPerBlock = newRewardTokensPerBlock;
}
function setSushiToken(address newToken) external onlyOwner {
emit ChangedAddress("SUSHI_TOKEN", address(sushiToken), newToken);
sushiToken = IERC20Extended(newToken);
}
function setMasterChef(address newAddress) external onlyOwner {
emit ChangedAddress("MASTER_CHEF", address(masterChef), newAddress);
masterChef = IMasterChef(newAddress);
}
function setVault(address newAddress) external onlyOwner {
emit ChangedAddress("VAULT", address(vault), newAddress);
vault = IVault(newAddress);
}
function setLockManager(address newAddress) external onlyOwner {
emit ChangedAddress("LOCK_MANAGER", address(lockManager), newAddress);
lockManager = ILockManager(newAddress);
}
function changeOwner(address newOwner) external onlyOwner {
require(newOwner != address(0) && newOwner != address(this), "RM::changeOwner: not valid address");
emit ChangedOwner(owner, newOwner);
owner = newOwner;
}
function _deposit(
uint256 pid,
uint256 amount,
PoolInfo storage pool,
UserInfo storage user
) internal {
updatePool(pid);
uint256 sushiPid = sushiPools[address(pool.token)];
uint256 pendingSushiTokens = 0;
if (user.amount > 0) {
uint256 pendingRewards = user.amount * pool.accRewardsPerShare / 1e12 - user.rewardTokenDebt;
if (pendingRewards > 0) {
_distributeRewards(msg.sender, pendingRewards, pool.vestingPercent, pool.vestingPeriod, pool.vestingCliff, pool.vpForVesting);
}
if (sushiPid != uint256(0)) {
masterChef.updatePool(sushiPid);
pendingSushiTokens = user.amount * masterChef.poolInfo(sushiPid).accSushiPerShare / 1e12 - user.sushiRewardDebt;
}
}
pool.token.safeTransferFrom(msg.sender, address(this), amount);
pool.totalStaked = pool.totalStaked + amount;
user.amount = user.amount + amount;
user.rewardTokenDebt = user.amount * pool.accRewardsPerShare / 1e12;
if (sushiPid != uint256(0)) {
masterChef.updatePool(sushiPid);
user.sushiRewardDebt = user.amount * masterChef.poolInfo(sushiPid).accSushiPerShare / 1e12;
masterChef.deposit(sushiPid, amount);
}
if (amount > 0 && pool.vpForDeposit) {
lockManager.grantVotingPower(msg.sender, address(pool.token), amount);
}
if (pendingSushiTokens > 0) {
_safeSushiTransfer(msg.sender, pendingSushiTokens);
}
emit Deposit(msg.sender, pid, amount);
}
function _withdraw(
uint256 pid,
uint256 amount,
PoolInfo storage pool,
UserInfo storage user
) internal {
require(user.amount >= amount, "RM::_withdraw: amount > user balance");
updatePool(pid);
uint256 sushiPid = sushiPools[address(pool.token)];
if (sushiPid != uint256(0)) {
masterChef.updatePool(sushiPid);
uint256 pendingSushiTokens = user.amount * masterChef.poolInfo(sushiPid).accSushiPerShare / 1e12 - user.sushiRewardDebt;
masterChef.withdraw(sushiPid, amount);
user.sushiRewardDebt = (user.amount - amount) * masterChef.poolInfo(sushiPid).accSushiPerShare / 1e12;
if (pendingSushiTokens > 0) {
_safeSushiTransfer(msg.sender, pendingSushiTokens);
}
}
uint256 pendingRewards = user.amount * pool.accRewardsPerShare / 1e12 - user.rewardTokenDebt;
user.amount = user.amount - amount;
user.rewardTokenDebt = user.amount * pool.accRewardsPerShare / 1e12;
if (pendingRewards > 0) {
_distributeRewards(msg.sender, pendingRewards, pool.vestingPercent, pool.vestingPeriod, pool.vestingCliff, pool.vpForVesting);
}
if (pool.vpForDeposit) {
lockManager.removeVotingPower(msg.sender, address(pool.token), amount);
}
pool.totalStaked = pool.totalStaked - amount;
pool.token.safeTransfer(msg.sender, amount);
emit Withdraw(msg.sender, pid, amount);
}
function _distributeRewards(
address account,
uint256 rewardAmount,
uint32 vestingPercent,
uint16 vestingPeriod,
uint16 vestingCliff,
bool vestingVotingPower
) internal {
rewardToken.mint(address(this), rewardAmount);
uint256 vestingRewards = rewardAmount * vestingPercent / 1000000;
if(vestingRewards > 0) {
vault.lockTokens(address(rewardToken), address(this), account, 0, vestingRewards, vestingPeriod, vestingCliff, vestingVotingPower);
}
rewardToken.safeTransfer(msg.sender, rewardAmount - vestingRewards);
}
function _safeSushiTransfer(address to, uint256 amount) internal {
uint256 sushiBalance = sushiToken.balanceOf(address(this));
if (amount > sushiBalance) {
sushiToken.safeTransfer(to, sushiBalance);
} else {
sushiToken.safeTransfer(to, amount);
}
}
}
文件 15 的 15:SafeERC20.sol
pragma solidity ^0.8.0;
import "../interfaces/IERC20.sol";
import "./Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
{
"compilationTarget": {
"contracts/RewardsManager.sol": "RewardsManager"
},
"evmVersion": "berlin",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 999999
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_lockManager","type":"address"},{"internalType":"address","name":"_vault","type":"address"},{"internalType":"address","name":"_rewardToken","type":"address"},{"internalType":"address","name":"_sushiToken","type":"address"},{"internalType":"address","name":"_masterChef","type":"address"},{"internalType":"uint256","name":"_startBlock","type":"uint256"},{"internalType":"uint256","name":"_rewardTokensPerBlock","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"addressType","type":"string"},{"indexed":true,"internalType":"address","name":"oldAddress","type":"address"},{"indexed":true,"internalType":"address","name":"newAddress","type":"address"}],"name":"ChangedAddress","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"ChangedOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"oldRewardTokensPerBlock","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"newRewardTokensPerBlock","type":"uint256"}],"name":"ChangedRewardTokensPerBlock","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"uint256","name":"pid","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"uint256","name":"pid","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EmergencyWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"pid","type":"uint256"},{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"allocPoints","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalAllocPoints","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rewardStartBlock","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"sushiPid","type":"uint256"},{"indexed":false,"internalType":"bool","name":"vpForDeposit","type":"bool"},{"indexed":false,"internalType":"bool","name":"vpForVesting","type":"bool"}],"name":"PoolAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"pid","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"oldAllocPoints","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newAllocPoints","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newTotalAllocPoints","type":"uint256"}],"name":"PoolUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"startBlock","type":"uint256"}],"name":"SetRewardsStartBlock","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"uint256","name":"pid","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[{"internalType":"uint256","name":"allocPoint","type":"uint256"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint32","name":"vestingPercent","type":"uint32"},{"internalType":"uint16","name":"vestingPeriod","type":"uint16"},{"internalType":"uint16","name":"vestingCliff","type":"uint16"},{"internalType":"bool","name":"withUpdate","type":"bool"},{"internalType":"uint256","name":"sushiPid","type":"uint256"},{"internalType":"bool","name":"vpForDeposit","type":"bool"},{"internalType":"bool","name":"vpForVesting","type":"bool"}],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"changeOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"depositWithPermit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"}],"name":"emergencyWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"from","type":"uint256"},{"internalType":"uint256","name":"to","type":"uint256"}],"name":"getMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"lockManager","outputs":[{"internalType":"contract ILockManager","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"massUpdatePools","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"masterChef","outputs":[{"internalType":"contract IMasterChef","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"address","name":"account","type":"address"}],"name":"pendingRewardTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"address","name":"account","type":"address"}],"name":"pendingSushi","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"poolInfo","outputs":[{"internalType":"contract IERC20Extended","name":"token","type":"address"},{"internalType":"uint256","name":"allocPoint","type":"uint256"},{"internalType":"uint256","name":"lastRewardBlock","type":"uint256"},{"internalType":"uint256","name":"accRewardsPerShare","type":"uint256"},{"internalType":"uint32","name":"vestingPercent","type":"uint32"},{"internalType":"uint16","name":"vestingPeriod","type":"uint16"},{"internalType":"uint16","name":"vestingCliff","type":"uint16"},{"internalType":"uint256","name":"totalStaked","type":"uint256"},{"internalType":"bool","name":"vpForDeposit","type":"bool"},{"internalType":"bool","name":"vpForVesting","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"poolLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"address","name":"receiver","type":"address"}],"name":"rescueTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardToken","outputs":[{"internalType":"contract IERC20Extended","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardTokensPerBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardsActive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"uint256","name":"allocPoint","type":"uint256"},{"internalType":"bool","name":"withUpdate","type":"bool"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAddress","type":"address"}],"name":"setLockManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAddress","type":"address"}],"name":"setMasterChef","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newRewardTokensPerBlock","type":"uint256"}],"name":"setRewardsPerBlock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newToken","type":"address"}],"name":"setSushiToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAddress","type":"address"}],"name":"setVault","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"startBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"sushiPools","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sushiToken","outputs":[{"internalType":"contract IERC20Extended","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokensToApprove","type":"address[]"},{"internalType":"uint256[]","name":"approvalAmounts","type":"uint256[]"},{"internalType":"address","name":"spender","type":"address"}],"name":"tokenAllow","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalAllocPoint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"}],"name":"updatePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"rewardTokenDebt","type":"uint256"},{"internalType":"uint256","name":"sushiRewardDebt","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"vault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"pid","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]