Advertisement
danny1995

NftExampleToken1155.sol

Oct 29th, 2022 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.3;
  3.  
  4. import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
  5. import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
  6. import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
  7.  
  8. /*
  9. * LIFE TOKEN CONTRACT
  10. */
  11. contract NftExampleToken1155 is ERC20,ERC1155Holder {
  12.  
  13. IERC1155 public nft;
  14. mapping(uint256=> address) public tokenOwnerOf;
  15. mapping(uint256=> uint256) public tokenStakedAt;
  16. uint256 public EMISSION_RATE=(50*10**decimals())/1 days;
  17.  
  18. /* ========== CONSTRUCTOR ========== */
  19. constructor(address _nft) ERC20("Example", "EXAMPLE") {
  20.  
  21. nft=IERC1155(_nft);
  22. }
  23.  
  24. function stake(uint256 tokenId) external{
  25. nft.safeTransferFrom(msg.sender, address(this), tokenId, 1, "");
  26. tokenOwnerOf[tokenId]=msg.sender;
  27. tokenStakedAt[tokenId]=block.timestamp;
  28. }
  29.  
  30. function calculateTokens(uint256 tokenId)public view returns(uint){
  31.  
  32. uint256 timeElapsed=block.timestamp-tokenStakedAt[tokenId];
  33. return timeElapsed*EMISSION_RATE;
  34.  
  35. }
  36.  
  37. modifier checkOwner(uint256 tokenId){
  38. require(tokenOwnerOf[tokenId]==msg.sender,"You are not the owner");
  39. _;
  40. }
  41.  
  42. function unstake(uint256 tokenId) external checkOwner(tokenId){
  43. _mint(msg.sender, calculateTokens(tokenId));
  44. nft.safeTransferFrom(address(this),msg.sender,tokenId, 1, "");
  45. delete tokenOwnerOf[tokenId];
  46. delete tokenStakedAt[tokenId];
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement