编译器
0.8.19+commit.7dd6d404
文件 1 的 16:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 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 functionCallWithValue(target, data, 0, "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");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 2 的 16:BalanceManagement.sol
pragma solidity 0.8.19;
import { ITokenBalance } from './interfaces/ITokenBalance.sol';
import { ManagerRole } from './roles/ManagerRole.sol';
import './helpers/TransferHelper.sol' as TransferHelper;
import './Constants.sol' as Constants;
abstract contract BalanceManagement is ManagerRole {
error ReservedTokenError();
function cleanup(address _tokenAddress, uint256 _tokenAmount) external onlyManager {
if (isReservedToken(_tokenAddress)) {
revert ReservedTokenError();
}
if (_tokenAddress == Constants.NATIVE_TOKEN_ADDRESS) {
TransferHelper.safeTransferNative(msg.sender, _tokenAmount);
} else {
TransferHelper.safeTransfer(_tokenAddress, msg.sender, _tokenAmount);
}
}
function tokenBalance(address _tokenAddress) public view returns (uint256) {
if (_tokenAddress == Constants.NATIVE_TOKEN_ADDRESS) {
return address(this).balance;
} else {
return ITokenBalance(_tokenAddress).balanceOf(address(this));
}
}
function isReservedToken(address _tokenAddress) public view virtual returns (bool) {
}
}
文件 3 的 16:Constants.sol
pragma solidity 0.8.19;
uint256 constant DECIMALS_DEFAULT = 18;
uint256 constant INFINITY = type(uint256).max;
uint256 constant LIST_SIZE_LIMIT_DEFAULT = 100;
uint256 constant LIST_SIZE_LIMIT_ROUTERS = 200;
uint256 constant MILLIPERCENT_FACTOR = 100_000;
address constant NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
文件 4 的 16:Context.sol
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) {
return msg.data;
}
}
文件 5 的 16:DataStructures.sol
pragma solidity 0.8.19;
struct OptionalValue {
bool isSet;
uint256 value;
}
struct KeyToValue {
uint256 key;
uint256 value;
}
struct KeyToAddressValue {
uint256 key;
address value;
}
struct AccountToFlag {
address account;
bool flag;
}
error ListSizeLimitError();
function combinedMapSet(
mapping(uint256 => address) storage _map,
uint256[] storage _keyList,
mapping(uint256 => OptionalValue) storage _keyIndexMap,
uint256 _key,
address _value,
uint256 _sizeLimit
) returns (bool isNewKey) {
isNewKey = !_keyIndexMap[_key].isSet;
if (isNewKey) {
uniqueListAdd(_keyList, _keyIndexMap, _key, _sizeLimit);
}
_map[_key] = _value;
}
function combinedMapRemove(
mapping(uint256 => address) storage _map,
uint256[] storage _keyList,
mapping(uint256 => OptionalValue) storage _keyIndexMap,
uint256 _key
) returns (bool isChanged) {
isChanged = _keyIndexMap[_key].isSet;
if (isChanged) {
delete _map[_key];
uniqueListRemove(_keyList, _keyIndexMap, _key);
}
}
function uniqueListAdd(
uint256[] storage _list,
mapping(uint256 => OptionalValue) storage _indexMap,
uint256 _value,
uint256 _sizeLimit
) returns (bool isChanged) {
isChanged = !_indexMap[_value].isSet;
if (isChanged) {
if (_list.length >= _sizeLimit) {
revert ListSizeLimitError();
}
_indexMap[_value] = OptionalValue(true, _list.length);
_list.push(_value);
}
}
function uniqueListRemove(
uint256[] storage _list,
mapping(uint256 => OptionalValue) storage _indexMap,
uint256 _value
) returns (bool isChanged) {
OptionalValue storage indexItem = _indexMap[_value];
isChanged = indexItem.isSet;
if (isChanged) {
uint256 itemIndex = indexItem.value;
uint256 lastIndex = _list.length - 1;
if (itemIndex != lastIndex) {
uint256 lastValue = _list[lastIndex];
_list[itemIndex] = lastValue;
_indexMap[lastValue].value = itemIndex;
}
_list.pop();
delete _indexMap[_value];
}
}
function uniqueAddressListAdd(
address[] storage _list,
mapping(address => OptionalValue) storage _indexMap,
address _value,
uint256 _sizeLimit
) returns (bool isChanged) {
isChanged = !_indexMap[_value].isSet;
if (isChanged) {
if (_list.length >= _sizeLimit) {
revert ListSizeLimitError();
}
_indexMap[_value] = OptionalValue(true, _list.length);
_list.push(_value);
}
}
function uniqueAddressListRemove(
address[] storage _list,
mapping(address => OptionalValue) storage _indexMap,
address _value
) returns (bool isChanged) {
OptionalValue storage indexItem = _indexMap[_value];
isChanged = indexItem.isSet;
if (isChanged) {
uint256 itemIndex = indexItem.value;
uint256 lastIndex = _list.length - 1;
if (itemIndex != lastIndex) {
address lastValue = _list[lastIndex];
_list[itemIndex] = lastValue;
_indexMap[lastValue].value = itemIndex;
}
_list.pop();
delete _indexMap[_value];
}
}
function uniqueAddressListUpdate(
address[] storage _list,
mapping(address => OptionalValue) storage _indexMap,
address _value,
bool _flag,
uint256 _sizeLimit
) returns (bool isChanged) {
return
_flag
? uniqueAddressListAdd(_list, _indexMap, _value, _sizeLimit)
: uniqueAddressListRemove(_list, _indexMap, _value);
}
文件 6 的 16:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, 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 from,
address to,
uint256 amount
) external returns (bool);
}
文件 7 的 16:IRevenueShare.sol
pragma solidity 0.8.19;
interface IRevenueShare {
function withdraw() external;
function lock(uint256 _amount) external;
function lock(uint256 _amount, address _user) external;
}
文件 8 的 16:ITokenBalance.sol
pragma solidity 0.8.19;
interface ITokenBalance {
function balanceOf(address _account) external view returns (uint256);
}
文件 9 的 16:ManagerRole.sol
pragma solidity 0.8.19;
import { Ownable } from '@openzeppelin/contracts/access/Ownable.sol';
import { RoleBearers } from './RoleBearers.sol';
abstract contract ManagerRole is Ownable, RoleBearers {
bytes32 private constant ROLE_KEY = keccak256('Manager');
event SetManager(address indexed account, bool indexed value);
event RenounceManagerRole(address indexed account);
error OnlyManagerError();
modifier onlyManager() {
if (!isManager(msg.sender)) {
revert OnlyManagerError();
}
_;
}
function setManager(address _account, bool _value) public onlyOwner {
_setRoleBearer(ROLE_KEY, _account, _value);
emit SetManager(_account, _value);
}
function renounceManagerRole() external onlyManager {
_setRoleBearer(ROLE_KEY, msg.sender, false);
emit RenounceManagerRole(msg.sender);
}
function managerCount() external view returns (uint256) {
return _roleBearerCount(ROLE_KEY);
}
function fullManagerList() external view returns (address[] memory) {
return _fullRoleBearerList(ROLE_KEY);
}
function isManager(address _account) public view returns (bool) {
return _isRoleBearer(ROLE_KEY, _account);
}
function _initRoles(
address _owner,
address[] memory _managers,
bool _addOwnerToManagers
) internal {
address ownerAddress = _owner == address(0) ? msg.sender : _owner;
for (uint256 index; index < _managers.length; index++) {
setManager(_managers[index], true);
}
if (_addOwnerToManagers && !isManager(ownerAddress)) {
setManager(ownerAddress, true);
}
if (ownerAddress != msg.sender) {
transferOwnership(ownerAddress);
}
}
}
文件 10 的 16:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 11 的 16:Pausable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Pausable is Context {
event Paused(address account);
event Unpaused(address account);
bool private _paused;
constructor() {
_paused = false;
}
modifier whenNotPaused() {
_requireNotPaused();
_;
}
modifier whenPaused() {
_requirePaused();
_;
}
function paused() public view virtual returns (bool) {
return _paused;
}
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
文件 12 的 16:RoleBearers.sol
pragma solidity 0.8.19;
import '../Constants.sol' as Constants;
import '../DataStructures.sol' as DataStructures;
abstract contract RoleBearers {
mapping(bytes32 => address[] ) private roleBearerTable;
mapping(bytes32 => mapping(address => DataStructures.OptionalValue ))
private roleBearerIndexTable;
function _setRoleBearer(bytes32 _roleKey, address _account, bool _value) internal {
DataStructures.uniqueAddressListUpdate(
roleBearerTable[_roleKey],
roleBearerIndexTable[_roleKey],
_account,
_value,
Constants.LIST_SIZE_LIMIT_DEFAULT
);
}
function _isRoleBearer(bytes32 _roleKey, address _account) internal view returns (bool) {
return roleBearerIndexTable[_roleKey][_account].isSet;
}
function _roleBearerCount(bytes32 _roleKey) internal view returns (uint256) {
return roleBearerTable[_roleKey].length;
}
function _fullRoleBearerList(bytes32 _roleKey) internal view returns (address[] memory) {
return roleBearerTable[_roleKey];
}
}
文件 13 的 16:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/draft-IERC20Permit.sol";
import "../../../utils/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 safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
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");
}
}
}
文件 14 的 16:StablecoinFarm.sol
pragma solidity 0.8.19;
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { Pausable } from '../Pausable.sol';
import { BalanceManagement } from '../BalanceManagement.sol';
import { IRevenueShare } from '../interfaces/IRevenueShare.sol';
contract StablecoinFarm is Pausable, BalanceManagement {
using SafeERC20 for IERC20;
struct VestedBalance {
uint256 amount;
uint256 unlockTime;
}
struct UserInfo {
uint256 amount;
uint256 rewardDebt;
uint256 remainingRewardTokenAmount;
}
struct PoolInfo {
address stakingToken;
uint256 stakingTokenTotalAmount;
uint256 accumulatedRewardTokenPerShare;
uint32 lastRewardTime;
uint16 allocationPoint;
}
address public immutable rewardToken;
address public treasury;
address public LPRevenueShare;
uint256 public rewardTokenPerSecond;
PoolInfo[] public poolInfo;
mapping(address => bool) public isStakingTokenSet;
mapping(uint256 => mapping(address => UserInfo)) public userInfo;
mapping(uint256 => mapping(address => VestedBalance[])) public userVested;
uint256 public totalAllocationPoint = 0;
uint32 public immutable startTime;
uint32 public endTime;
uint256 public immutable vestingDuration;
uint256 public exitEarlyUserShare = 500;
uint256 public exitEarlyTreasuryShare = 200;
uint256 public exitEarlyLPShare = 300;
uint256 private constant SHARE_PRECISION = 1e18;
event Staked(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 WithdrawVesting(address indexed user, uint256 amount);
event Vested(address indexed user, uint256 indexed pid, uint256 amount);
event Locked(address indexed user, uint256 indexed pid, uint256 amount);
event ExitEarly(address indexed user, uint256 amount);
constructor(
address _rewardToken,
uint256 _rewardTokenPerSecond,
uint32 _startTime,
uint32 _endTimeAddSeconds,
uint256 _vestingDuration
) {
rewardToken = _rewardToken;
rewardTokenPerSecond = _rewardTokenPerSecond;
startTime = _startTime;
endTime = startTime + _endTimeAddSeconds;
vestingDuration = _vestingDuration;
}
function setTreasury(address _newTreasury) external onlyOwner {
require(_newTreasury != address(0), 'Zero address error');
treasury = _newTreasury;
}
function setLPRevenueShare(address _newRevenueShare) external onlyOwner {
require(_newRevenueShare != address(0), 'Zero address error');
LPRevenueShare = _newRevenueShare;
}
function setPercentsShare(
uint256 _userPercent,
uint256 _treasuryPercent,
uint256 _lpPercent
) external onlyOwner {
require(
_userPercent + _treasuryPercent + _lpPercent == 1000,
'Total percentage should be 100% in total'
);
exitEarlyUserShare = _userPercent;
exitEarlyTreasuryShare = _treasuryPercent;
exitEarlyLPShare = _lpPercent;
}
function stake(uint256 _pid, uint256 _amount) external whenNotPaused {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
_updatePool(_pid);
IERC20(pool.stakingToken).safeTransferFrom(msg.sender, address(this), _amount);
user.remainingRewardTokenAmount = pendingRewardToken(_pid, msg.sender);
user.amount += _amount;
pool.stakingTokenTotalAmount += _amount;
user.rewardDebt = (user.amount * pool.accumulatedRewardTokenPerShare) / SHARE_PRECISION;
emit Staked(msg.sender, _pid, _amount);
}
function withdraw(uint256 _pid, uint256 _amount) external whenNotPaused {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
if (user.amount < _amount) {
revert('Can not withdraw this amount');
}
_updatePool(_pid);
user.remainingRewardTokenAmount = pendingRewardToken(_pid, msg.sender);
user.amount -= _amount;
pool.stakingTokenTotalAmount -= _amount;
user.rewardDebt = (user.amount * pool.accumulatedRewardTokenPerShare) / SHARE_PRECISION;
IERC20(pool.stakingToken).safeTransfer(msg.sender, _amount);
emit Withdraw(msg.sender, _pid, _amount);
}
function emergencyWithdraw(uint256 _pid) external {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
uint256 userAmount = user.amount;
pool.stakingTokenTotalAmount -= userAmount;
user.amount = 0;
user.rewardDebt = 0;
user.remainingRewardTokenAmount = 0;
IERC20(pool.stakingToken).safeTransfer(msg.sender, userAmount);
emit EmergencyWithdraw(msg.sender, _pid, userAmount);
}
function changeEndTime(uint32 _addSeconds) external onlyManager {
endTime += _addSeconds;
}
function setRewardTokenPerSecond(
uint256 _rewardTokenPerSecond,
bool _withUpdate
) external onlyManager {
if (_withUpdate) {
_massUpdatePools();
}
rewardTokenPerSecond = _rewardTokenPerSecond;
}
function add(uint16 _allocPoint, address _stakingToken, bool _withUpdate) external onlyManager {
require(!isStakingTokenSet[_stakingToken], 'Staking token was already set');
require(poolInfo.length < 5, 'No more then 5 pools can be added');
if (_withUpdate) {
_massUpdatePools();
}
uint256 lastRewardTime = block.timestamp > startTime ? block.timestamp : startTime;
totalAllocationPoint += _allocPoint;
poolInfo.push(
PoolInfo({
stakingToken: _stakingToken,
stakingTokenTotalAmount: 0,
allocationPoint: _allocPoint,
lastRewardTime: uint32(lastRewardTime),
accumulatedRewardTokenPerShare: 0
})
);
isStakingTokenSet[_stakingToken] = true;
}
function set(uint256 _pid, uint16 _allocPoint, bool _withUpdate) external onlyManager {
if (_withUpdate) {
_massUpdatePools();
}
totalAllocationPoint = totalAllocationPoint - poolInfo[_pid].allocationPoint + _allocPoint;
poolInfo[_pid].allocationPoint = _allocPoint;
}
function massUpdatePools() external whenNotPaused {
_massUpdatePools();
}
function updatePool(uint256 _pid) external whenNotPaused {
_updatePool(_pid);
}
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
function vest(uint256 _pid) external whenNotPaused {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
_updatePool(_pid);
uint256 pending = pendingRewardToken(_pid, msg.sender);
require(pending > 0, 'Amount of tokens can not be zero value');
uint256 unlockTime = block.timestamp + vestingDuration;
VestedBalance[] storage vestings = userVested[_pid][msg.sender];
require(vestings.length <= 100, 'User can not execute vest function more than 100 times');
vestings.push(VestedBalance({ amount: pending, unlockTime: unlockTime }));
user.remainingRewardTokenAmount = 0;
user.rewardDebt = (user.amount * pool.accumulatedRewardTokenPerShare) / SHARE_PRECISION;
emit Vested(msg.sender, _pid, pending);
}
function withdrawVestedRewards(uint256 _pid) external {
_updatePool(_pid);
(uint256 vested, , ) = checkVestingBalances(_pid, msg.sender);
uint256 amount;
if (vested > 0) {
uint256 length = userVested[_pid][msg.sender].length;
for (uint256 i = 0; i < length; i++) {
uint256 vestAmount = userVested[_pid][msg.sender][i].amount;
if (userVested[_pid][msg.sender][i].unlockTime > block.timestamp) {
break;
}
amount = amount + vestAmount;
delete userVested[_pid][msg.sender][i];
}
}
if (amount > 0) {
safeRewardTransfer(msg.sender, amount);
} else {
revert('Tokens are not available for now');
}
emit WithdrawVesting(msg.sender, amount);
}
function exitEarly(uint256 _pid) external {
_updatePool(_pid);
(, uint256 vesting, ) = checkVestingBalances(_pid, msg.sender);
require(vesting > 0, 'Total vesting tokens can not be zero');
uint256 amountUser = (vesting * exitEarlyUserShare) / 1000;
uint256 amountTreasury = (vesting * exitEarlyTreasuryShare) / 1000;
uint256 amountLP = (vesting * exitEarlyLPShare) / 1000;
safeRewardTransfer(msg.sender, amountUser);
IERC20(rewardToken).safeTransfer(treasury, amountTreasury);
IERC20(rewardToken).safeTransfer(LPRevenueShare, amountLP);
_cleanVestingBalances(_pid, msg.sender);
emit ExitEarly(msg.sender, amountUser);
}
function lockVesting(uint256 _pid) external {
revert('Unsupported operation');
}
function lockPending(uint256 _pid) external {
revert('Unsupported operation');
}
function getMultiplier(uint256 _from, uint256 _to) public view returns (uint256) {
_from = _from > startTime ? _from : startTime;
if (_from > endTime || _to < startTime) {
return 0;
} else if (_to > endTime) {
return endTime - _from;
} else return _to - _from;
}
function isReservedToken(address _tokenAddress) public view override returns (bool) {
uint256 length = poolInfo.length;
for (uint256 pid; pid < length; ++pid) {
if (_tokenAddress == poolInfo[pid].stakingToken) {
return true;
}
}
return false;
}
function pendingRewardToken(uint256 _pid, address _user) public view returns (uint256 pending) {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 acc = pool.accumulatedRewardTokenPerShare;
if (block.timestamp > pool.lastRewardTime && pool.stakingTokenTotalAmount != 0) {
uint256 multiplier = getMultiplier(pool.lastRewardTime, block.timestamp);
uint256 tokenReward = (multiplier * rewardTokenPerSecond * pool.allocationPoint) /
totalAllocationPoint;
acc += (tokenReward * SHARE_PRECISION) / pool.stakingTokenTotalAmount;
}
pending =
(user.amount * acc) /
SHARE_PRECISION -
user.rewardDebt +
user.remainingRewardTokenAmount;
}
function checkVestingBalances(
uint256 _pid,
address _user
)
public
view
returns (
uint256 vestedTotal,
uint256 vestingTotal,
VestedBalance[] memory vestData
)
{
VestedBalance[] storage vests = userVested[_pid][_user];
uint256 index;
for (uint256 i = 0; i < vests.length; i++) {
if (vests[i].unlockTime > block.timestamp) {
if (index == 0) {
vestData = new VestedBalance[](vests.length - i);
}
vestData[index] = vests[i];
index++;
vestingTotal += vests[i].amount;
} else {
vestedTotal = vestedTotal + vests[i].amount;
}
}
}
function _cleanVestingBalances(uint256 _pid, address _user) internal {
VestedBalance[] storage vests = userVested[_pid][_user];
for (uint256 i = 0; i < vests.length; i++) {
if (vests[i].unlockTime > block.timestamp) {
delete vests[i];
}
}
}
function safeRewardTransfer(address _to, uint256 _amount) private {
uint256 rewardTokenBalance = IERC20(rewardToken).balanceOf(address(this));
if (_amount > rewardTokenBalance) {
revert('Not enough tokens on the smart contract');
} else {
IERC20(rewardToken).safeTransfer(_to, _amount);
}
}
function _massUpdatePools() private {
uint256 length = poolInfo.length;
for (uint256 pid; pid < length; ++pid) {
_updatePool(pid);
}
}
function _updatePool(uint256 _pid) private {
PoolInfo storage pool = poolInfo[_pid];
if (block.timestamp <= pool.lastRewardTime) {
return;
}
if (pool.stakingTokenTotalAmount == 0) {
pool.lastRewardTime = uint32(block.timestamp);
return;
}
uint256 multiplier = getMultiplier(pool.lastRewardTime, block.timestamp);
uint256 rewardTokenAmount = (multiplier * rewardTokenPerSecond * pool.allocationPoint) /
totalAllocationPoint;
pool.accumulatedRewardTokenPerShare +=
(rewardTokenAmount * SHARE_PRECISION) /
pool.stakingTokenTotalAmount;
pool.lastRewardTime = uint32(block.timestamp);
}
}
文件 15 的 16:TransferHelper.sol
pragma solidity 0.8.19;
error SafeApproveError();
error SafeTransferError();
error SafeTransferFromError();
error SafeTransferNativeError();
function safeApprove(address _token, address _to, uint256 _value) {
(bool success, bytes memory data) = _token.call(
abi.encodeWithSelector(0x095ea7b3, _to, _value)
);
bool condition = success && (data.length == 0 || abi.decode(data, (bool)));
if (!condition) {
revert SafeApproveError();
}
}
function safeTransfer(address _token, address _to, uint256 _value) {
(bool success, bytes memory data) = _token.call(
abi.encodeWithSelector(0xa9059cbb, _to, _value)
);
bool condition = success && (data.length == 0 || abi.decode(data, (bool)));
if (!condition) {
revert SafeTransferError();
}
}
function safeTransferFrom(address _token, address _from, address _to, uint256 _value) {
(bool success, bytes memory data) = _token.call(
abi.encodeWithSelector(0x23b872dd, _from, _to, _value)
);
bool condition = success && (data.length == 0 || abi.decode(data, (bool)));
if (!condition) {
revert SafeTransferFromError();
}
}
function safeTransferNative(address _to, uint256 _value) {
(bool success, ) = _to.call{ value: _value }(new bytes(0));
if (!success) {
revert SafeTransferNativeError();
}
}
文件 16 的 16:draft-IERC20Permit.sol
pragma solidity ^0.8.0;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
{
"compilationTarget": {
"contracts/farm/StablecoinFarm.sol": "StablecoinFarm"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_rewardToken","type":"address"},{"internalType":"uint256","name":"_rewardTokenPerSecond","type":"uint256"},{"internalType":"uint32","name":"_startTime","type":"uint32"},{"internalType":"uint32","name":"_endTimeAddSeconds","type":"uint32"},{"internalType":"uint256","name":"_vestingDuration","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ListSizeLimitError","type":"error"},{"inputs":[],"name":"OnlyManagerError","type":"error"},{"inputs":[],"name":"ReservedTokenError","type":"error"},{"inputs":[],"name":"SafeTransferError","type":"error"},{"inputs":[],"name":"SafeTransferNativeError","type":"error"},{"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":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"ExitEarly","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":"Locked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"RenounceManagerRole","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"bool","name":"value","type":"bool"}],"name":"SetManager","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":"Staked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","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":"Vested","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"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"WithdrawVesting","type":"event"},{"inputs":[],"name":"LPRevenueShare","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint16","name":"_allocPoint","type":"uint16"},{"internalType":"address","name":"_stakingToken","type":"address"},{"internalType":"bool","name":"_withUpdate","type":"bool"}],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint32","name":"_addSeconds","type":"uint32"}],"name":"changeEndTime","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"},{"internalType":"address","name":"_user","type":"address"}],"name":"checkVestingBalances","outputs":[{"internalType":"uint256","name":"vestedTotal","type":"uint256"},{"internalType":"uint256","name":"vestingTotal","type":"uint256"},{"components":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"unlockTime","type":"uint256"}],"internalType":"struct StablecoinFarm.VestedBalance[]","name":"vestData","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"},{"internalType":"uint256","name":"_tokenAmount","type":"uint256"}],"name":"cleanup","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"emergencyWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"endTime","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"exitEarly","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"exitEarlyLPShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exitEarlyTreasuryShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exitEarlyUserShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fullManagerList","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_from","type":"uint256"},{"internalType":"uint256","name":"_to","type":"uint256"}],"name":"getMultiplier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"isManager","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"}],"name":"isReservedToken","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isStakingTokenSet","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"lockPending","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"lockVesting","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"managerCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"massUpdatePools","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"},{"internalType":"address","name":"_user","type":"address"}],"name":"pendingRewardToken","outputs":[{"internalType":"uint256","name":"pending","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"poolInfo","outputs":[{"internalType":"address","name":"stakingToken","type":"address"},{"internalType":"uint256","name":"stakingTokenTotalAmount","type":"uint256"},{"internalType":"uint256","name":"accumulatedRewardTokenPerShare","type":"uint256"},{"internalType":"uint32","name":"lastRewardTime","type":"uint32"},{"internalType":"uint16","name":"allocationPoint","type":"uint16"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"poolLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceManagerRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardTokenPerSecond","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"},{"internalType":"uint16","name":"_allocPoint","type":"uint16"},{"internalType":"bool","name":"_withUpdate","type":"bool"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newRevenueShare","type":"address"}],"name":"setLPRevenueShare","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"bool","name":"_value","type":"bool"}],"name":"setManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_userPercent","type":"uint256"},{"internalType":"uint256","name":"_treasuryPercent","type":"uint256"},{"internalType":"uint256","name":"_lpPercent","type":"uint256"}],"name":"setPercentsShare","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_rewardTokenPerSecond","type":"uint256"},{"internalType":"bool","name":"_withUpdate","type":"bool"}],"name":"setRewardTokenPerSecond","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newTreasury","type":"address"}],"name":"setTreasury","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"}],"name":"tokenBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalAllocationPoint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"treasury","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","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":"rewardDebt","type":"uint256"},{"internalType":"uint256","name":"remainingRewardTokenAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"userVested","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"unlockTime","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"vest","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vestingDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_pid","type":"uint256"}],"name":"withdrawVestedRewards","outputs":[],"stateMutability":"nonpayable","type":"function"}]