Skip to content

Instantly share code, notes, and snippets.

@DoctorNasa
Forked from Chmarusso/DamnSimpleNftStake.sol
Created April 23, 2022 11:53
Show Gist options
  • Save DoctorNasa/6816328f7365e957ef057daf3822c7aa to your computer and use it in GitHub Desktop.
Save DoctorNasa/6816328f7365e957ef057daf3822c7aa to your computer and use it in GitHub Desktop.
Super Simple NFT staking
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
contract NftStaker {
IERC1155 public parentNFT;
struct Stake {
uint256 tokenId;
uint256 amount;
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 = IERC1155(0xd9145CCE52D386f254917e481eB44e9943F39138); // Change it to your NFT contract addr
}
function stake(uint256 _tokenId, uint256 _amount) public {
stakes[msg.sender] = Stake(_tokenId, _amount, block.timestamp);
parentNFT.safeTransferFrom(msg.sender, address(this), _tokenId, _amount, "0x00");
}
function unstake() public {
parentNFT.safeTransferFrom(address(this), msg.sender, stakes[msg.sender].tokenId, stakes[msg.sender].amount, "0x00");
stakingTime[msg.sender] += (block.timestamp - stakes[msg.sender].timestamp);
delete stakes[msg.sender];
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4) {
return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment