File size: 2,513 Bytes
cc183a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {IERC4907} from "./interfaces/IERC4907.sol";
import {IERC5192} from "./interfaces/IERC5192.sol";
abstract contract LenientLockCollectionBase is ERC721, IERC4907, IERC5192 {
struct UserInfo {
address user;
uint64 expires;
}
address public immutable issuer;
mapping(uint256 => UserInfo) private _users;
mapping(uint256 => bool) private _locked;
constructor() ERC721("Layered Collection", "LAYER") {
issuer = msg.sender;
}
function mint(address to, uint256 tokenId) external {
require(msg.sender == issuer, "only issuer");
_mint(to, tokenId);
}
function setLocked(uint256 tokenId, bool value) public {
require(msg.sender == _requireOwned(tokenId), "only owner");
_locked[tokenId] = value;
if (value) emit Locked(tokenId);
else emit Unlocked(tokenId);
}
function locked(uint256 tokenId) public view returns (bool) {
_requireOwned(tokenId);
return _locked[tokenId];
}
function setUser(uint256 tokenId, address user, uint64 expires) public {
require(_isAuthorized(_requireOwned(tokenId), msg.sender, tokenId), "caller is not owner nor approved");
require(expires > block.timestamp, "expiry must be in the future");
_users[tokenId] = UserInfo(user, expires);
emit UpdateUser(tokenId, user, expires);
}
function userOf(uint256 tokenId) public view returns (address) {
return block.timestamp < _users[tokenId].expires ? _users[tokenId].user : address(0);
}
function userExpires(uint256 tokenId) public view returns (uint256) {
return _users[tokenId].expires;
}
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(IERC4907).interfaceId || interfaceId == type(IERC5192).interfaceId
|| super.supportsInterface(interfaceId);
}
function _update(address to, uint256 tokenId, address auth) internal override returns (address from) {
from = super._update(to, tokenId, auth);
if (from != address(0)) {
if (from != to && _users[tokenId].user != address(0)) {
delete _users[tokenId];
emit UpdateUser(tokenId, address(0), 0);
}
}
}
}
contract LenientLockCollection is LenientLockCollectionBase {}
|