文件 1 的 7: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 的 7:DobieStaker.sol
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract DobieStaker is ERC721Holder, Ownable {
IERC721 public nft;
mapping(uint256 => address) public tokenOwnerOf;
mapping(uint256 => uint256) public tokenStakedAt;
mapping(address => uint256[]) public tokensStakedbyAddress;
constructor(address _nft) {
nft = IERC721(_nft);
}
function stake(uint256 tokenId) external {
nft.safeTransferFrom(msg.sender, address(this), tokenId);
tokenOwnerOf[tokenId] = msg.sender;
tokenStakedAt[tokenId] = block.timestamp;
tokensStakedbyAddress[msg.sender].push(tokenId);
}
function calculateUserLevel(address _userAddress) private view returns (uint256) {
if (tokensStakedbyAddress[_userAddress].length == 0) {
return 0;
}
uint256 timeElapsedInterval1 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][0]];
if (tokensStakedbyAddress[_userAddress].length < 5) {
return uint256(timeElapsedInterval1/604800) + 1;
} else if (tokensStakedbyAddress[_userAddress].length < 10) {
uint256 timeElapsedInterval2 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][4]];
return uint256((timeElapsedInterval1 + timeElapsedInterval2)/604800) + 1;
} else if (tokensStakedbyAddress[_userAddress].length < 20) {
uint256 timeElapsedInterval2 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][4]];
uint256 timeElapsedInterval3 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][9]];
return uint256((timeElapsedInterval1 + timeElapsedInterval2 + timeElapsedInterval3)/604800) + 1;
} else if (tokensStakedbyAddress[_userAddress].length >= 20) {
uint256 timeElapsedInterval2 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][4]];
uint256 timeElapsedInterval3 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][9]];
uint256 timeElapsedInterval4 = block.timestamp - tokenStakedAt[tokensStakedbyAddress[_userAddress][19]];
return uint256((timeElapsedInterval1 + timeElapsedInterval2 + timeElapsedInterval3 + timeElapsedInterval4)/604800) + 1;
}
}
function displayLevel() public view returns (uint256) {
return calculateUserLevel(msg.sender);
}
function adminDisplayLevel(address _userAddress) public onlyOwner view returns (uint256) {
return calculateUserLevel(_userAddress);
}
function unstakeAll() external {
require(tokensStakedbyAddress[msg.sender].length > 0, "You can't unstake");
for(uint256 i = 0; i < tokensStakedbyAddress[msg.sender].length; i++) {
nft.transferFrom(address(this), msg.sender, tokensStakedbyAddress[msg.sender][i]);
delete tokenOwnerOf[tokensStakedbyAddress[msg.sender][i]];
delete tokenStakedAt[tokensStakedbyAddress[msg.sender][i]];
}
delete tokensStakedbyAddress[msg.sender];
}
}
文件 3 的 7: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 的 7:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 5 的 7: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);
}
文件 6 的 7:IERC721Receiver.sol
pragma solidity ^0.8.0;
interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
文件 7 的 7: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);
}
}
{
"compilationTarget": {
"contracts/DobieStaker.sol": "DobieStaker"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_nft","type":"address"}],"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":[{"internalType":"address","name":"_userAddress","type":"address"}],"name":"adminDisplayLevel","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"displayLevel","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nft","outputs":[{"internalType":"contract IERC721","name":"","type":"address"}],"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":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokenOwnerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokenStakedAt","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokensStakedbyAddress","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":"unstakeAll","outputs":[],"stateMutability":"nonpayable","type":"function"}]