Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.8.3;
- import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
- import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
- import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
- /*
- * LIFE TOKEN CONTRACT
- */
- contract NftExampleToken1155 is ERC20,ERC1155Holder {
- IERC1155 public nft;
- mapping(uint256=> address) public tokenOwnerOf;
- mapping(uint256=> uint256) public tokenStakedAt;
- uint256 public EMISSION_RATE=(50*10**decimals())/1 days;
- /* ========== CONSTRUCTOR ========== */
- constructor(address _nft) ERC20("Example", "EXAMPLE") {
- nft=IERC1155(_nft);
- }
- function stake(uint256 tokenId) external{
- nft.safeTransferFrom(msg.sender, address(this), tokenId, 1, "");
- tokenOwnerOf[tokenId]=msg.sender;
- tokenStakedAt[tokenId]=block.timestamp;
- }
- function calculateTokens(uint256 tokenId)public view returns(uint){
- uint256 timeElapsed=block.timestamp-tokenStakedAt[tokenId];
- return timeElapsed*EMISSION_RATE;
- }
- modifier checkOwner(uint256 tokenId){
- require(tokenOwnerOf[tokenId]==msg.sender,"You are not the owner");
- _;
- }
- function unstake(uint256 tokenId) external checkOwner(tokenId){
- _mint(msg.sender, calculateTokens(tokenId));
- nft.safeTransferFrom(address(this),msg.sender,tokenId, 1, "");
- delete tokenOwnerOf[tokenId];
- delete tokenStakedAt[tokenId];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement