编译器
0.8.14+commit.80d49f37
文件 1 的 17: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 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 的 17: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;
}
}
文件 3 的 17:ERC721Holder.sol
pragma solidity ^0.8.0;
import "../IERC721Receiver.sol";
contract ERC721Holder is IERC721Receiver {
function onERC721Received(
address,
address,
uint256,
bytes memory
) public virtual override returns (bytes4) {
return this.onERC721Received.selector;
}
}
文件 4 的 17:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 5 的 17: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);
}
文件 6 的 17:IERC721.sol
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
interface IERC721 is IERC165 {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
function approve(address to, uint256 tokenId) external;
function setApprovalForAll(address operator, bool _approved) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
文件 7 的 17:IERC721Receiver.sol
pragma solidity ^0.8.0;
interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
文件 8 的 17:IMintableERC20.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IMintableERC20 is IERC20 {
function mint(address destination, uint256 amount) external;
}
文件 9 的 17:IRecoverable.sol
pragma solidity ^0.8.0;
interface IRecoverable {
function recoverNonFungibleToken(address _token, uint256 _tokenId) external;
function recoverToken(address _token) external;
function recoverEth(address payable _to) external;
}
文件 10 的 17:MinterAccess.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
abstract contract MinterAccess is Ownable {
mapping(address => bool) private _minters;
event MinterAdded(address indexed minter);
event MinterRemoved(address indexed minter);
modifier onlyMinters() {
require(_minters[_msgSender()], "Mintable: Caller is not minter");
_;
}
function isMinter(address account) public view returns (bool) {
return _minters[account];
}
function addMinter(address minter) external onlyOwner {
require(!_minters[minter], "Mintable: Already minter");
_minters[minter] = true;
emit MinterAdded(minter);
}
function removeMinter(address minter) external onlyOwner {
require(_minters[minter], "Mintable: Not minter");
_minters[minter] = false;
emit MinterRemoved(minter);
}
}
文件 11 的 17:NftStakingPool.sol
pragma solidity ^0.8.0;
pragma abicoder v2;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./Poolable.sol";
import "./Recoverable.sol";
contract NftStakingPool is Ownable, Poolable, Recoverable, ReentrancyGuard, ERC721Holder {
using SafeERC20 for IERC20;
struct PoolDeposit {
address owner;
uint64 pool;
uint256 depositDate;
uint256 claimed;
}
struct MultiStakeParam {
uint256[] tokenIds;
uint256 poolId;
}
IERC20 public rewardToken;
mapping(address => mapping(uint256 => PoolDeposit)) private _deposits;
mapping(address => uint256) private _userRewards;
event Stake(address indexed account, uint256 poolId, address indexed collection, uint256 tokenId);
event Unstake(address indexed account, address indexed collection, uint256 tokenId);
event BatchStake(address indexed account, uint256 poolId, address indexed collection, uint256[] tokenIds);
event BatchUnstake(address indexed account, address indexed collection, uint256[] tokenIds);
event Claimed(address indexed account, address indexed collection, uint256 tokenId, uint256 rewards, uint256 pool);
event ClaimedMulti(address indexed account, MultiStakeParam[] groups, uint256 rewards);
constructor(IERC20 _rewardToken) {
rewardToken = _rewardToken;
}
function _sendRewards(address destination, uint256 amount) internal virtual {
rewardToken.safeTransfer(destination, amount);
}
function _sendAndUpdateRewards(address account, uint256 amount) internal {
if (amount > 0) {
_userRewards[account] = _userRewards[account] + amount;
_sendRewards(account, amount);
}
}
function _getPendingRewardAmounts(PoolDeposit memory deposit, Pool memory pool) internal view returns (uint256) {
uint256 reward = 0;
uint256 dt = deposit.depositDate;
while (dt != 0 && pool.lockDuration != 0) {
dt += pool.lockDuration;
if (dt > block.timestamp) break;
reward += pool.rewardAmount;
if (pool.endRewardDate != 0 && dt > pool.endRewardDate) break;
}
if (reward <= deposit.claimed) {
return 0;
}
return reward - deposit.claimed;
}
function _stake(
address account,
address collection,
uint256 tokenId,
uint256 poolId
) internal {
require(_deposits[collection][tokenId].owner == address(0), "Stake: Token already staked");
_deposits[collection][tokenId] = PoolDeposit({
owner: account,
pool: uint64(poolId),
depositDate: block.timestamp,
claimed: 0
});
IERC721(collection).safeTransferFrom(account, address(this), tokenId);
}
function stake(uint256 poolId, uint256 tokenId) external nonReentrant whenPoolOpened(poolId) {
address account = _msgSender();
Pool memory pool = getPool(poolId);
_stake(account, pool.collection, tokenId, poolId);
emit Stake(account, poolId, pool.collection, tokenId);
}
function _unstake(
address account,
address collection,
uint256 tokenId
) internal returns (uint256) {
PoolDeposit storage deposit = _deposits[collection][tokenId];
require(isUnlockable(deposit.pool, deposit.depositDate), "Stake: Not yet unstakable");
Pool memory pool = getPool(deposit.pool);
uint256 rewards = _getPendingRewardAmounts(deposit, pool);
if (rewards > 0) {
deposit.claimed += rewards;
}
delete _deposits[collection][tokenId];
IERC721(collection).safeTransferFrom(address(this), account, tokenId);
return rewards;
}
function unstake(address collection, uint256 tokenId) external nonReentrant {
require(_deposits[collection][tokenId].owner == _msgSender(), "Stake: Not owner of token");
address account = _msgSender();
uint256 rewards = _unstake(account, collection, tokenId);
_sendAndUpdateRewards(account, rewards);
emit Unstake(account, collection, tokenId);
}
function _restake(
uint256 newPoolId,
address collection,
uint256 tokenId
) internal returns (uint256) {
require(isPoolOpened(newPoolId), "Stake: Pool is closed");
require(collectionForPool(newPoolId) == collection, "Stake: Invalid collection");
PoolDeposit storage deposit = _deposits[collection][tokenId];
Pool memory oldPool = getPool(deposit.pool);
require(isUnlockable(deposit.pool, deposit.depositDate), "Stake: Not yet unstakable");
uint256 rewards = _getPendingRewardAmounts(deposit, oldPool);
deposit.pool = uint64(newPoolId);
deposit.depositDate = block.timestamp;
deposit.claimed = 0;
return rewards;
}
function restake(
uint256 newPoolId,
address collection,
uint256 tokenId
) external nonReentrant {
require(_deposits[collection][tokenId].owner != address(0), "Stake: Token not staked");
require(_deposits[collection][tokenId].owner == _msgSender(), "Stake: Not owner of token");
address account = _msgSender();
uint256 rewards = _restake(newPoolId, collection, tokenId);
_sendAndUpdateRewards(account, rewards);
emit Unstake(account, collection, tokenId);
emit Stake(account, newPoolId, collection, tokenId);
}
function _batchStake(
address account,
uint256 poolId,
uint256[] memory tokenIds
) internal whenPoolOpened(poolId) {
Pool memory pool = getPool(poolId);
for (uint256 i = 0; i < tokenIds.length; i++) {
_stake(account, pool.collection, tokenIds[i], poolId);
}
emit BatchStake(account, poolId, pool.collection, tokenIds);
}
function _batchUnstake(
address account,
address collection,
uint256[] memory tokenIds
) internal {
uint256 rewards = 0;
for (uint256 i = 0; i < tokenIds.length; i++) {
require(_deposits[collection][tokenIds[i]].owner == account, "Stake: Not owner of token");
rewards = rewards + _unstake(account, collection, tokenIds[i]);
}
_sendAndUpdateRewards(account, rewards);
emit BatchUnstake(account, collection, tokenIds);
}
function _batchRestake(
address account,
uint256 poolId,
address collection,
uint256[] memory tokenIds
) internal {
uint256 rewards = 0;
for (uint256 i = 0; i < tokenIds.length; i++) {
require(_deposits[collection][tokenIds[i]].owner == account, "Stake: Not owner of token");
rewards += _restake(poolId, collection, tokenIds[i]);
}
_sendAndUpdateRewards(account, rewards);
emit BatchUnstake(account, collection, tokenIds);
emit BatchStake(account, poolId, collection, tokenIds);
}
function batchStake(uint256 poolId, uint256[] calldata tokenIds) external nonReentrant {
_batchStake(_msgSender(), poolId, tokenIds);
}
function batchUnstake(address collection, uint256[] calldata tokenIds) external nonReentrant {
_batchUnstake(_msgSender(), collection, tokenIds);
}
function batchRestake(
uint256 poolId,
address collection,
uint256[] calldata tokenIds
) external nonReentrant {
_batchRestake(_msgSender(), poolId, collection, tokenIds);
}
function stakeMulti(MultiStakeParam[] memory groups) external nonReentrant {
address account = _msgSender();
for (uint256 i = 0; i < groups.length; i++) {
_batchStake(account, groups[i].poolId, groups[i].tokenIds);
}
}
function unstakeMulti(MultiStakeParam[] memory groups) external nonReentrant {
address account = _msgSender();
for (uint256 i = 0; i < groups.length; i++) {
address collection = getPool(groups[i].poolId).collection;
_batchUnstake(account, collection, groups[i].tokenIds);
}
}
function restakeMulti(MultiStakeParam[] memory groups) external nonReentrant {
address account = _msgSender();
for (uint256 i = 0; i < groups.length; i++) {
address collection = getPool(groups[i].poolId).collection;
_batchRestake(account, groups[i].poolId, collection, groups[i].tokenIds);
}
}
function claim(address collection, uint256 tokenId) external {
address account = _msgSender();
PoolDeposit storage deposit = _deposits[collection][tokenId];
require(deposit.owner == account, "Stake: Not owner of token");
require(isUnlockable(deposit.pool, deposit.depositDate), "Stake: Not yet unstakable");
Pool memory pool = getPool(deposit.pool);
uint256 rewards = _getPendingRewardAmounts(deposit, pool);
if (rewards > 0) {
deposit.claimed += rewards;
}
_sendAndUpdateRewards(account, rewards);
emit Claimed(account, collection, tokenId, rewards, deposit.pool);
}
function claimMulti(MultiStakeParam[] memory groups) external {
address account = _msgSender();
uint256 rewards = 0;
for (uint256 i = 0; i < groups.length; i++) {
Pool memory pool = getPool(groups[i].poolId);
for (uint256 u = 0; u < groups[i].tokenIds.length; u++) {
PoolDeposit storage deposit = _deposits[pool.collection][groups[i].tokenIds[u]];
require(deposit.owner == _msgSender(), "Stake: Not owner of token");
require(isUnlockable(deposit.pool, deposit.depositDate), "Stake: Not yet unstakable");
uint256 depositRewards = _getPendingRewardAmounts(deposit, pool);
if (depositRewards > 0) {
deposit.claimed += depositRewards;
rewards += depositRewards;
}
}
}
_sendAndUpdateRewards(account, rewards);
emit ClaimedMulti(account, groups, rewards);
}
function isTokenUnlocked(address collection, uint256 tokenId) public view returns (bool) {
require(_deposits[collection][tokenId].owner != address(0), "Stake: Token not staked");
return isUnlocked(_deposits[collection][tokenId].pool, _deposits[collection][tokenId].depositDate);
}
function getStakeInfo(address collection, uint256 tokenId)
external
view
returns (
address owner,
uint256 poolId,
uint256 depositDate,
uint256 unlockDate,
uint256 rewardDate,
uint256 totalClaimed
)
{
if (_deposits[collection][tokenId].owner == address(0)) {
return (address(0), 0, 0, 0, 0, 0);
}
PoolDeposit memory deposit = _deposits[collection][tokenId];
Pool memory pool = getPool(deposit.pool);
return (
deposit.owner,
deposit.pool,
deposit.depositDate,
deposit.depositDate + pool.minDuration,
deposit.depositDate + pool.lockDuration,
deposit.claimed
);
}
function getUserTotalRewards(address account) external view returns (uint256) {
return _userRewards[account];
}
function recoverNonFungibleToken(address _token, uint256 _tokenId) external override onlyOwner {
require(_deposits[_token][_tokenId].owner == address(0), "Stake: Cannot recover staked token");
IERC721(_token).transferFrom(address(this), address(msg.sender), _tokenId);
emit NonFungibleTokenRecovery(_token, _tokenId);
}
}
文件 12 的 17: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());
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
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);
}
}
文件 13 的 17:Poolable.sol
pragma solidity ^0.8.0;
pragma abicoder v2;
import "@openzeppelin/contracts/access/Ownable.sol";
abstract contract Poolable is Ownable {
struct Pool {
address collection;
uint256 lockDuration;
uint256 minDuration;
uint256 endRewardDate;
uint256 rewardAmount;
}
uint256 public poolsLength;
mapping(uint256 => Pool) private _pools;
event PoolAdded(uint256 poolIndex, Pool pool);
event PoolUpdated(uint256 poolIndex, Pool pool);
modifier whenPoolOpened(uint256 poolIndex) {
require(
isPoolOpened(poolIndex),
"Poolable: Pool is closed"
);
_;
}
modifier whenUnlocked(uint256 poolIndex, uint256 depositDate) {
require(isUnlocked(poolIndex, depositDate), "Poolable: Not unlocked");
_;
}
function getPool(uint256 poolIndex) public view returns (Pool memory) {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
return _pools[poolIndex];
}
function addPool(Pool calldata pool) external onlyOwner {
uint256 poolIndex = poolsLength;
_pools[poolIndex] = pool;
poolsLength = poolsLength + 1;
emit PoolAdded(poolIndex, _pools[poolIndex]);
}
function updatePool(uint256 poolIndex, Pool calldata pool) external onlyOwner {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
Pool storage editedPool = _pools[poolIndex];
editedPool.lockDuration = pool.lockDuration;
editedPool.minDuration = pool.minDuration;
editedPool.endRewardDate = pool.endRewardDate;
editedPool.rewardAmount = pool.rewardAmount;
emit PoolUpdated(poolIndex, editedPool);
}
function closePool(uint256 poolIndex) external onlyOwner whenPoolOpened(poolIndex) {
Pool storage editedPool = _pools[poolIndex];
editedPool.endRewardDate = block.timestamp;
emit PoolUpdated(poolIndex, editedPool);
}
function isUnlocked(uint256 poolIndex, uint256 depositDate) internal view returns (bool) {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
require(depositDate < block.timestamp, "Poolable: Invalid deposit date");
return block.timestamp - depositDate >= _pools[poolIndex].lockDuration;
}
function isUnlockable(uint256 poolIndex, uint256 depositDate) internal view returns (bool) {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
require(depositDate < block.timestamp, "Poolable: Invalid deposit date");
return block.timestamp - depositDate >= _pools[poolIndex].minDuration;
}
function isPoolOpened(uint256 poolIndex) public view returns (bool) {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
return _pools[poolIndex].endRewardDate == 0 || _pools[poolIndex].endRewardDate > block.timestamp;
}
function collectionForPool(uint256 poolIndex) public view returns (address) {
require(poolIndex < poolsLength, "Poolable: Invalid poolIndex");
return _pools[poolIndex].collection;
}
}
文件 14 的 17:Recoverable.sol
pragma solidity ^0.8.0;
pragma abicoder v2;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "../interfaces/IRecoverable.sol";
abstract contract Recoverable is Ownable, IRecoverable {
using SafeERC20 for IERC20;
event NonFungibleTokenRecovery(address indexed token, uint256 tokenId);
event TokenRecovery(address indexed token, uint256 amount);
event EthRecovery(uint256 amount);
function recoverNonFungibleToken(address _token, uint256 _tokenId) external virtual onlyOwner {
IERC721(_token).transferFrom(address(this), address(msg.sender), _tokenId);
emit NonFungibleTokenRecovery(_token, _tokenId);
}
function recoverToken(address _token) external virtual onlyOwner {
uint256 balance = IERC20(_token).balanceOf(address(this));
require(balance != 0, "Operations: Cannot recover zero balance");
IERC20(_token).safeTransfer(address(msg.sender), balance);
emit TokenRecovery(_token, balance);
}
function recoverEth(address payable _to) external virtual onlyOwner {
uint256 balance = address(this).balance;
_to.transfer(balance);
emit EthRecovery(balance);
}
}
文件 15 的 17: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;
}
}
文件 16 的 17: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");
}
}
}
文件 17 的 17:UnifiedStaking.sol
pragma solidity ^0.8.0;
import "./interfaces/IMintableERC20.sol";
import "./libraries/NftStakingPool.sol";
import "./libraries/MinterAccess.sol";
contract UnifiedStaking is NftStakingPool, MinterAccess {
constructor(IMintableERC20 _rewardToken) NftStakingPool(_rewardToken) {}
function _sendRewards(address destination, uint256 amount) internal override {
uint256 b = rewardToken.balanceOf(address(this));
if (b >= amount) super._sendRewards(destination, amount);
else IMintableERC20(address(rewardToken)).mint(destination, amount);
}
function stakeFrom(
address from,
uint256 poolId,
uint256 tokenId
) external onlyMinters whenPoolOpened(poolId) {
require(from != address(0), "Stake: address(0)");
Pool memory pool = getPool(poolId);
_stake(from, pool.collection, tokenId, poolId);
emit Stake(from, poolId, pool.collection, tokenId);
}
function batchStakeFrom(
address from,
uint256 poolId,
uint256[] calldata tokenIds
) external onlyMinters whenPoolOpened(poolId) {
require(from != address(0), "Stake: address(0)");
Pool memory pool = getPool(poolId);
for (uint256 i = 0; i < tokenIds.length; i++) {
_stake(from, pool.collection, tokenIds[i], poolId);
}
emit BatchStake(from, poolId, pool.collection, tokenIds);
}
}
{
"compilationTarget": {
"contracts/UnifiedStaking.sol": "UnifiedStaking"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 500
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IMintableERC20","name":"_rewardToken","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"poolId","type":"uint256"},{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":false,"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"BatchStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":false,"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"BatchUnstake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rewards","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"pool","type":"uint256"}],"name":"Claimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"components":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"},{"internalType":"uint256","name":"poolId","type":"uint256"}],"indexed":false,"internalType":"struct NftStakingPool.MultiStakeParam[]","name":"groups","type":"tuple[]"},{"indexed":false,"internalType":"uint256","name":"rewards","type":"uint256"}],"name":"ClaimedMulti","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"EthRecovery","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"minter","type":"address"}],"name":"MinterAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"minter","type":"address"}],"name":"MinterRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"NonFungibleTokenRecovery","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":"uint256","name":"poolIndex","type":"uint256"},{"components":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"lockDuration","type":"uint256"},{"internalType":"uint256","name":"minDuration","type":"uint256"},{"internalType":"uint256","name":"endRewardDate","type":"uint256"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"}],"indexed":false,"internalType":"struct Poolable.Pool","name":"pool","type":"tuple"}],"name":"PoolAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"poolIndex","type":"uint256"},{"components":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"lockDuration","type":"uint256"},{"internalType":"uint256","name":"minDuration","type":"uint256"},{"internalType":"uint256","name":"endRewardDate","type":"uint256"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"}],"indexed":false,"internalType":"struct Poolable.Pool","name":"pool","type":"tuple"}],"name":"PoolUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"poolId","type":"uint256"},{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Stake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TokenRecovery","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Unstake","type":"event"},{"inputs":[{"internalType":"address","name":"minter","type":"address"}],"name":"addMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"lockDuration","type":"uint256"},{"internalType":"uint256","name":"minDuration","type":"uint256"},{"internalType":"uint256","name":"endRewardDate","type":"uint256"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"}],"internalType":"struct Poolable.Pool","name":"pool","type":"tuple"}],"name":"addPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"batchRestake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"batchStake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"batchStakeFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"batchUnstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"},{"internalType":"uint256","name":"poolId","type":"uint256"}],"internalType":"struct NftStakingPool.MultiStakeParam[]","name":"groups","type":"tuple[]"}],"name":"claimMulti","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolIndex","type":"uint256"}],"name":"closePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolIndex","type":"uint256"}],"name":"collectionForPool","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolIndex","type":"uint256"}],"name":"getPool","outputs":[{"components":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"lockDuration","type":"uint256"},{"internalType":"uint256","name":"minDuration","type":"uint256"},{"internalType":"uint256","name":"endRewardDate","type":"uint256"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"}],"internalType":"struct Poolable.Pool","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getStakeInfo","outputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"uint256","name":"depositDate","type":"uint256"},{"internalType":"uint256","name":"unlockDate","type":"uint256"},{"internalType":"uint256","name":"rewardDate","type":"uint256"},{"internalType":"uint256","name":"totalClaimed","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"getUserTotalRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"isMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolIndex","type":"uint256"}],"name":"isPoolOpened","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"isTokenUnlocked","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onERC721Received","outputs":[{"internalType":"bytes4","name":"","type":"bytes4"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"poolsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address payable","name":"_to","type":"address"}],"name":"recoverEth","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"recoverNonFungibleToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"}],"name":"recoverToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"minter","type":"address"}],"name":"removeMinter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newPoolId","type":"uint256"},{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"restake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"},{"internalType":"uint256","name":"poolId","type":"uint256"}],"internalType":"struct NftStakingPool.MultiStakeParam[]","name":"groups","type":"tuple[]"}],"name":"restakeMulti","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardToken","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"poolId","type":"uint256"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"stakeFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"},{"internalType":"uint256","name":"poolId","type":"uint256"}],"internalType":"struct NftStakingPool.MultiStakeParam[]","name":"groups","type":"tuple[]"}],"name":"stakeMulti","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"unstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"},{"internalType":"uint256","name":"poolId","type":"uint256"}],"internalType":"struct NftStakingPool.MultiStakeParam[]","name":"groups","type":"tuple[]"}],"name":"unstakeMulti","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"poolIndex","type":"uint256"},{"components":[{"internalType":"address","name":"collection","type":"address"},{"internalType":"uint256","name":"lockDuration","type":"uint256"},{"internalType":"uint256","name":"minDuration","type":"uint256"},{"internalType":"uint256","name":"endRewardDate","type":"uint256"},{"internalType":"uint256","name":"rewardAmount","type":"uint256"}],"internalType":"struct Poolable.Pool","name":"pool","type":"tuple"}],"name":"updatePool","outputs":[],"stateMutability":"nonpayable","type":"function"}]