Created
February 13, 2022 15:54
-
-
Save Chmarusso/1eb113ac1114baece27d856fa0a4472d 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 "@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)")); | |
} | |
} |
looks bang bang bang
cool
how to do a prevention record for cross id unstack
userA => stack id 1
userB => stack id 2
userB => call unstack with id 1 instead of 2 (<-- current is success) but the userB take the NFT id 1 instead of his id 2
@cryptobeginner
Are you mentioning a flaw that UserB when unstaking does not take his own nft?
Great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does the function onERC1155Received do?