编译器
0.8.11+commit.d7f03943
文件 1 的 6: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;
}
}
文件 2 的 6:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 3 的 6: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
) external;
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
function approve(address to, uint256 tokenId) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function setApprovalForAll(address operator, bool _approved) external;
function isApprovedForAll(address owner, address operator) external view returns (bool);
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
}
文件 4 的 6:IStakingReward.sol
pragma solidity ^0.8.0;
interface IStakingReward {
function willStakeTokens(address account, uint16[] calldata tokenIds) external;
function willUnstakeTokens(address account, uint16[] calldata tokenIds) external;
function willBeReplacedByContract(address stakingRewardContract) external;
function didReplaceContract(address stakingRewardContract) external;
}
文件 5 的 6: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);
}
}
文件 6 的 6:TheHumanoidsStaking.sol
pragma solidity =0.8.11;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./IStakingReward.sol";
contract TheHumanoidsStaking is Ownable {
bool public canStake;
address public stakingRewardContract;
IERC721 private constant _NFT_CONTRACT = IERC721(0x3a5051566b2241285BE871f650C445A88A970edd);
struct Token {
address owner;
uint64 timestamp;
uint16 index;
}
mapping(uint256 => Token) private _tokens;
mapping(address => uint16[]) private _stakedTokens;
constructor() {}
function setStaking(bool _canStake) external onlyOwner {
canStake = _canStake;
}
function setStakingRewardContract(address newStakingRewardContract) external onlyOwner {
address oldStakingRewardContract = stakingRewardContract;
require(newStakingRewardContract != oldStakingRewardContract, "New staking reward contract is same as old contract");
if (oldStakingRewardContract != address(0)) {
IStakingReward(oldStakingRewardContract).willBeReplacedByContract(newStakingRewardContract);
}
stakingRewardContract = newStakingRewardContract;
if (newStakingRewardContract != address(0)) {
IStakingReward(newStakingRewardContract).didReplaceContract(oldStakingRewardContract);
}
}
function stakedTokensBalanceOf(address account) external view returns (uint256) {
return _stakedTokens[account].length;
}
function stakedTokensOf(address account) external view returns (uint16[] memory) {
return _stakedTokens[account];
}
function ownerOf(uint256 tokenId) external view returns (address) {
address owner = _tokens[tokenId].owner;
require(owner != address(0), "Token not staked");
return owner;
}
function timestampOf(uint256 tokenId) external view returns (uint256) {
uint256 timestamp = uint256(_tokens[tokenId].timestamp);
require(timestamp != 0, "Token not staked");
return timestamp;
}
function stake(uint16[] calldata tokenIds) external {
require(canStake, "Staking not enabled");
uint length = tokenIds.length;
require(length > 0, "tokenIds array is empty");
if (stakingRewardContract != address(0)) {
IStakingReward(stakingRewardContract).willStakeTokens(msg.sender, tokenIds);
}
uint64 timestamp = uint64(block.timestamp);
uint16[] storage stakedTokens = _stakedTokens[msg.sender];
uint16 index = uint16(stakedTokens.length);
unchecked {
for (uint i=0; i<length; i++) {
uint16 tokenId = tokenIds[i];
_NFT_CONTRACT.transferFrom(msg.sender, address(this), tokenId);
Token storage token = _tokens[tokenId];
token.owner = msg.sender;
token.timestamp = timestamp;
token.index = index;
index++;
stakedTokens.push(tokenId);
}
}
}
function unstake(uint16[] calldata tokenIds) external {
uint length = tokenIds.length;
require(length > 0, "tokenIds array is empty");
if (stakingRewardContract != address(0)) {
IStakingReward(stakingRewardContract).willUnstakeTokens(msg.sender, tokenIds);
}
unchecked {
uint16[] storage stakedTokens = _stakedTokens[msg.sender];
for (uint i=0; i<length; i++) {
uint256 tokenId = tokenIds[i];
require(_tokens[tokenId].owner == msg.sender, "Token not staked or not owned");
uint index = _tokens[tokenId].index;
uint lastIndex = stakedTokens.length-1;
if (index != lastIndex) {
uint16 lastTokenId = stakedTokens[lastIndex];
stakedTokens[index] = lastTokenId;
_tokens[lastTokenId].index = uint16(index);
}
stakedTokens.pop();
delete _tokens[tokenId];
_NFT_CONTRACT.transferFrom(address(this), msg.sender, tokenId);
}
}
}
function unstakeAll() external {
uint16[] storage stakedTokens = _stakedTokens[msg.sender];
uint length = stakedTokens.length;
require(length > 0, "Nothing staked");
if (stakingRewardContract != address(0)) {
IStakingReward(stakingRewardContract).willUnstakeTokens(msg.sender, stakedTokens);
}
unchecked {
for (uint i=0; i<length; i++) {
uint256 tokenId = stakedTokens[i];
delete _tokens[tokenId];
_NFT_CONTRACT.transferFrom(address(this), msg.sender, tokenId);
}
}
delete _stakedTokens[msg.sender];
}
}
{
"compilationTarget": {
"contracts/TheHumanoidsStaking.sol": "TheHumanoidsStaking"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"canStake","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_canStake","type":"bool"}],"name":"setStaking","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newStakingRewardContract","type":"address"}],"name":"setStakingRewardContract","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16[]","name":"tokenIds","type":"uint16[]"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"stakedTokensBalanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"stakedTokensOf","outputs":[{"internalType":"uint16[]","name":"","type":"uint16[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"stakingRewardContract","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"timestampOf","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":[{"internalType":"uint16[]","name":"tokenIds","type":"uint16[]"}],"name":"unstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unstakeAll","outputs":[],"stateMutability":"nonpayable","type":"function"}]