Skip to content

Instantly share code, notes, and snippets.

@Chmarusso
Created February 13, 2022 15:54
Show Gist options
  • Save Chmarusso/1eb113ac1114baece27d856fa0a4472d to your computer and use it in GitHub Desktop.
Save Chmarusso/1eb113ac1114baece27d856fa0a4472d 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)"));
}
}
@IamTalha-Sajid
Copy link

Best

@BiWinBi
Copy link

BiWinBi commented Apr 15, 2022

The Best

@MatthewSamush
Copy link

Best

@DoctorNasa
Copy link

Nice

@JaviMarciano
Copy link

What does the function onERC1155Received do?

@Daudxu
Copy link

Daudxu commented May 12, 2022

looks bang bang bang

@PONDARA
Copy link

PONDARA commented Jun 10, 2022

cool

@Renaenys
Copy link

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

@achilds1
Copy link

achilds1 commented Aug 9, 2022

@cryptobeginner
Are you mentioning a flaw that UserB when unstaking does not take his own nft?

@AhmedCoolProjects
Copy link

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment