Created
March 7, 2023 23:36
-
-
Save fulldecent/49afb959160a7a39b463018a2e9d11bc to your computer and use it in GitHub Desktop.
Bio NFT 2 demo
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.4; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "./IERC4907.sol"; | |
contract MyToken is ERC721, IERC4907 { | |
struct UserInfo { | |
address user; // address of user role | |
uint64 expires; // unix timestamp, user expires after this time | |
} | |
mapping (uint256 => UserInfo) private _uses; | |
constructor() ERC721("MyToken", "MTK") {} | |
/// @inheritdoc IERC4907 | |
function setUser(uint256 tokenId, address user, uint64 expires) external virtual{ | |
require(_isApprovedOrOwner(msg.sender, tokenId), "ERC4907: transfer caller is not owner nor approved"); | |
UserInfo storage info = _uses[tokenId]; | |
info.user = user; | |
info.expires = expires; | |
emit UpdateUser(tokenId, user, expires); | |
} | |
/// @inheritdoc IERC4907 | |
function userOf(uint256 tokenId) external view returns(address) { | |
if (uint256(_uses[tokenId].expires) >= block.timestamp){ | |
return _uses[tokenId].user; | |
} else { | |
return address(0); | |
} | |
} | |
/// @inheritdoc IERC4907 | |
function userExpires(uint256 tokenId) external view returns(uint256){ | |
return _uses[tokenId].expires; | |
} | |
/// @inheritdoc IERC165 | |
function supportsInterface(bytes4 interfaceId) public view override returns (bool) { | |
return interfaceId == type(IERC4907).interfaceId || super.supportsInterface(interfaceId); | |
} | |
function _beforeTokenTransfer( | |
address from, | |
address to, | |
uint256 tokenId, | |
uint256 batchSize | |
) internal override { | |
super._beforeTokenTransfer(from, to, tokenId, batchSize); | |
if (from != to && _uses[tokenId].user != address(0)) { | |
delete _uses[tokenId]; | |
emit UpdateUser(tokenId, address(0), 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment