编译器
0.8.15+commit.e14f2714
文件 1 的 5: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
) internal 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 的 5: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 的 5: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);
}
文件 4 的 5:NodeStaking.sol
pragma solidity 0.8.15;
import {SafeERC20} from "openzeppelin/token/ERC20/utils/SafeERC20.sol";
import {IERC20Metadata} from "openzeppelin/token/ERC20/extensions/IERC20Metadata.sol";
error AmountLessThanStakedAmountOrZero();
error CallerNotGovernance();
error EtherNotAccepted();
error InsufficientFunds();
error NoPendingRewardsToClaim();
error NoStakeFound();
error RewardDistributionPeriodHasExpired();
error RewardPerBlockIsNotSet();
error SameRewardToken();
error ZeroAddress();
error ZeroInput();
contract NodeStaking {
using SafeERC20 for IERC20Metadata;
struct UserInfo {
uint256 lastUpdateRewardToken;
uint256 amount;
uint256 rewardDebt;
}
enum TxType {
STAKE,
UNSTAKE,
CLAIM,
EMERGENCY
}
IERC20Metadata public immutable nodeToken;
IERC20Metadata public rewardToken;
address public governance;
uint256 public constant ONE = 1e9;
uint256 public accRewardPerNode;
uint256 public lastUpdateBlock;
uint256 public totalNodeStaked;
uint256 public stakerCount;
uint256 public currentRewardPerBlock;
uint256 public periodEndBlock;
uint256 public lastUpdateRewardToken;
mapping(address => UserInfo) public userInfo;
event StakeOrUnstakeOrClaim(
address indexed user,
uint256 amount,
uint256 pendingReward,
TxType txType
);
event NewRewardPeriod(
uint256 numberBlocksToDistributeRewards,
uint256 newRewardPerBlock,
uint256 rewardToDistribute,
uint256 rewardExpirationBlock
);
event GovernanceChanged(
address indexed oldGovernance,
address indexed newGovernance
);
event RewardTokenChanged(
address indexed oldRewardToken,
address indexed newRewardToken
);
event PeriodEndBlockUpdate(
uint256 numberBlocksToDistributeRewards,
uint256 rewardExpirationBlock
);
constructor(
address _governance,
address _rewardToken,
address _nodeToken
) {
if (
_governance == address(0) ||
_rewardToken == address(0) ||
_nodeToken == address(0)
) revert ZeroAddress();
governance = _governance;
rewardToken = IERC20Metadata(_rewardToken);
nodeToken = IERC20Metadata(_nodeToken);
emit GovernanceChanged(address(0), _governance);
emit RewardTokenChanged(address(0), _rewardToken);
}
receive() external payable {
revert EtherNotAccepted();
}
modifier onlyGovernance() {
if (msg.sender != governance) revert CallerNotGovernance();
_;
}
function setGovernance(address _newGovernance) external onlyGovernance {
if (_newGovernance == address(0)) revert ZeroAddress();
emit GovernanceChanged(governance, _newGovernance);
governance = _newGovernance;
}
function updateRewardToken(address _newRewardToken)
external
onlyGovernance
{
if (_newRewardToken == address(rewardToken)) revert SameRewardToken();
if (_newRewardToken == address(0)) revert ZeroAddress();
accRewardPerNode = 0;
lastUpdateBlock = _lastRewardBlock();
lastUpdateRewardToken = block.timestamp;
emit RewardTokenChanged(address(rewardToken), _newRewardToken);
rewardToken = IERC20Metadata(_newRewardToken);
}
function updateRewards(uint256 _reward, uint256 _rewardDurationInBlocks)
external
onlyGovernance
{
if (_rewardDurationInBlocks == 0) revert ZeroInput();
_updateRewardPerNodeAndLastBlock();
if (block.number >= periodEndBlock) {
if (_reward == 0) revert ZeroInput();
currentRewardPerBlock = _reward / _rewardDurationInBlocks;
}
else {
currentRewardPerBlock =
(_reward +
((periodEndBlock - block.number) * currentRewardPerBlock)) /
_rewardDurationInBlocks;
}
lastUpdateBlock = block.number;
periodEndBlock = block.number + _rewardDurationInBlocks;
emit NewRewardPeriod(
_rewardDurationInBlocks,
currentRewardPerBlock,
_reward,
periodEndBlock
);
}
function updateRewardEndBlock(uint256 _expireDurationInBlocks)
external
onlyGovernance
{
_updateRewardPerNodeAndLastBlock();
lastUpdateBlock = block.number;
periodEndBlock = block.number + _expireDurationInBlocks;
emit PeriodEndBlockUpdate(_expireDurationInBlocks, periodEndBlock);
}
function stake(address _to,uint256 _amount) external {
if (_amount == 0) revert ZeroInput();
if (_to == address(0)) revert ZeroAddress();
if (currentRewardPerBlock == 0) revert RewardPerBlockIsNotSet();
if (block.number >= periodEndBlock)
revert RewardDistributionPeriodHasExpired();
if (rewardToken.balanceOf(address(this)) == 0)
revert InsufficientFunds();
_stakeOrUnstakeOrClaim( _to,_amount, TxType.STAKE);
}
function unstake(uint256 _amount) external {
if ((_amount > userInfo[msg.sender].amount) || _amount == 0)
revert AmountLessThanStakedAmountOrZero();
_stakeOrUnstakeOrClaim(msg.sender, _amount, TxType.UNSTAKE);
}
function emergencyUnstake() external {
if (userInfo[msg.sender].amount > 0) {
_stakeOrUnstakeOrClaim(
msg.sender,
userInfo[msg.sender].amount,
TxType.EMERGENCY
);
} else revert NoStakeFound();
}
function claim() external {
_stakeOrUnstakeOrClaim(
msg.sender,
userInfo[msg.sender].amount,
TxType.CLAIM
);
}
function calculatePendingRewards(address _user)
external
view
returns (uint256)
{
uint256 newAccRewardPerNode;
if (totalNodeStaked != 0) {
newAccRewardPerNode =
accRewardPerNode +
(((_lastRewardBlock() - lastUpdateBlock) *
(currentRewardPerBlock * ONE)) / totalNodeStaked);
if (newAccRewardPerNode == 0) return 0;
} else return 0;
uint256 rewardDebt = userInfo[_user].rewardDebt;
if (userInfo[_user].lastUpdateRewardToken < lastUpdateRewardToken)
rewardDebt = 0;
uint256 pendingRewards = ((userInfo[_user].amount *
newAccRewardPerNode) / ONE) - rewardDebt;
return pendingRewards;
}
function lastRewardBlock() external view returns (uint256) {
return _lastRewardBlock();
}
function _stakeOrUnstakeOrClaim(
address _to,
uint256 _amount,
TxType _txType
) private {
_updateRewardPerNodeAndLastBlock();
_resetDebtIfNewRewardToken(_to);
UserInfo storage user = userInfo[_to];
uint256 pendingRewards;
if (TxType.EMERGENCY != _txType) {
if (user.amount > 0) {
pendingRewards = _calculatePendingRewards(_to);
if (pendingRewards > 0) {
if (pendingRewards > rewardToken.balanceOf(address(this)))
revert InsufficientFunds();
rewardToken.safeTransfer(_to, pendingRewards);
}
else if (TxType.CLAIM == _txType)
revert NoPendingRewardsToClaim();
}
else if (TxType.CLAIM == _txType) revert NoPendingRewardsToClaim();
}
if (TxType.STAKE == _txType) {
nodeToken.safeTransferFrom(msg.sender, address(this), _amount);
if (user.amount == 0) stakerCount++;
user.amount += _amount;
totalNodeStaked += _amount;
} else if (TxType.UNSTAKE == _txType || TxType.EMERGENCY == _txType) {
user.amount -= _amount;
totalNodeStaked -= _amount;
nodeToken.safeTransfer(_to, _amount);
if (user.amount == 0) stakerCount--;
}
user.rewardDebt = (user.amount * accRewardPerNode) / ONE;
emit StakeOrUnstakeOrClaim(_to, _amount, pendingRewards, _txType);
}
function _resetDebtIfNewRewardToken(address _to) private {
if (userInfo[_to].lastUpdateRewardToken < lastUpdateRewardToken) {
userInfo[_to].rewardDebt = 0;
userInfo[_to].lastUpdateRewardToken = lastUpdateRewardToken;
}
}
function _updateRewardPerNodeAndLastBlock() private {
if (totalNodeStaked == 0) {
lastUpdateBlock = block.number;
return;
}
accRewardPerNode +=
((_lastRewardBlock() - lastUpdateBlock) *
(currentRewardPerBlock * ONE)) /
totalNodeStaked;
if (block.number != lastUpdateBlock)
lastUpdateBlock = _lastRewardBlock();
}
function _calculatePendingRewards(address _user)
private
view
returns (uint256)
{
return
((userInfo[_user].amount * accRewardPerNode) / ONE) -
userInfo[_user].rewardDebt;
}
function _lastRewardBlock() private view returns (uint256) {
return block.number < periodEndBlock ? block.number : periodEndBlock;
}
}
文件 5 的 5:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.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 _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": {
"src/NodeStaking.sol": "NodeStaking"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 10000
},
"remappings": [
":ds-test/=lib/forge-std/lib/ds-test/src/",
":forge-std/=lib/forge-std/src/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
":openzeppelin/=lib/openzeppelin-contracts/contracts/"
]
}
[{"inputs":[{"internalType":"address","name":"_governance","type":"address"},{"internalType":"address","name":"_rewardToken","type":"address"},{"internalType":"address","name":"_nodeToken","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AmountLessThanStakedAmountOrZero","type":"error"},{"inputs":[],"name":"CallerNotGovernance","type":"error"},{"inputs":[],"name":"EtherNotAccepted","type":"error"},{"inputs":[],"name":"InsufficientFunds","type":"error"},{"inputs":[],"name":"NoPendingRewardsToClaim","type":"error"},{"inputs":[],"name":"NoStakeFound","type":"error"},{"inputs":[],"name":"RewardDistributionPeriodHasExpired","type":"error"},{"inputs":[],"name":"RewardPerBlockIsNotSet","type":"error"},{"inputs":[],"name":"SameRewardToken","type":"error"},{"inputs":[],"name":"ZeroAddress","type":"error"},{"inputs":[],"name":"ZeroInput","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldGovernance","type":"address"},{"indexed":true,"internalType":"address","name":"newGovernance","type":"address"}],"name":"GovernanceChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"numberBlocksToDistributeRewards","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newRewardPerBlock","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rewardToDistribute","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rewardExpirationBlock","type":"uint256"}],"name":"NewRewardPeriod","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"numberBlocksToDistributeRewards","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rewardExpirationBlock","type":"uint256"}],"name":"PeriodEndBlockUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldRewardToken","type":"address"},{"indexed":true,"internalType":"address","name":"newRewardToken","type":"address"}],"name":"RewardTokenChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"pendingReward","type":"uint256"},{"indexed":false,"internalType":"enum NodeStaking.TxType","name":"txType","type":"uint8"}],"name":"StakeOrUnstakeOrClaim","type":"event"},{"inputs":[],"name":"ONE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"accRewardPerNode","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"calculatePendingRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"currentRewardPerBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"emergencyUnstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"governance","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastRewardBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastUpdateBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastUpdateRewardToken","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nodeToken","outputs":[{"internalType":"contract IERC20Metadata","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodEndBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rewardToken","outputs":[{"internalType":"contract IERC20Metadata","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_newGovernance","type":"address"}],"name":"setGovernance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"stakerCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalNodeStaked","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"unstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_expireDurationInBlocks","type":"uint256"}],"name":"updateRewardEndBlock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newRewardToken","type":"address"}],"name":"updateRewardToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_reward","type":"uint256"},{"internalType":"uint256","name":"_rewardDurationInBlocks","type":"uint256"}],"name":"updateRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"lastUpdateRewardToken","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"rewardDebt","type":"uint256"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]