-
-
Save BiWinBi/659b516aa27f29df2158e637cf06131d to your computer and use it in GitHub Desktop.
Super Simple NFT staking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.7; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/utils/ERC721Holder.sol "; | |
contract NftStaker { | |
IERC721 public parentNFT; | |
struct Stake { | |
uint256 tokenId; | |
uint256 timestamp; | |
} | |
// map staker address to stake details | |
mapping(address => Stake) public stakes; | |
// map staker to total staking time | |
mapping(address => uint256) public stakingTime; | |
constructor() { | |
parentNFT = IERC721(0xdccB0e678bEA8FE3d97921CbFF85Be757a223312); // Change it to your NFT contract addr | |
} | |
function stake(uint256 _tokenId) public { | |
stakes[msg.sender] = Stake(_tokenId, block.timestamp); | |
parentNFT.safeTransferFrom(msg.sender, address(this), _tokenId, "0x00"); | |
} | |
function unstake() public { | |
parentNFT.safeTransferFrom(address(this), msg.sender, stakes[msg.sender].tokenId, "0x00"); | |
stakingTime[msg.sender] += (block.timestamp - stakes[msg.sender].timestamp); | |
delete stakes[msg.sender]; | |
} | |
function onERC721Received( | |
address, | |
address, | |
uint256, | |
bytes memory | |
) external pure returns (bytes4) { | |
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment