File size: 4,559 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {IERC5006} from "./interfaces/IERC5006.sol";
contract Rentable1155 is ERC1155, IERC5006 {
address public immutable issuer;
bool public ownerMayEndEarly;
uint256 private _curRecordId;
mapping(uint256 => UserRecord) private _records;
mapping(uint256 => mapping(address => uint256)) private _frozenBalances;
mapping(uint256 => mapping(address => uint256[])) private _userRecordIds;
constructor(bool ownerMayEndEarly_) ERC1155("") {
issuer = msg.sender;
ownerMayEndEarly = ownerMayEndEarly_;
}
function mint(address to, uint256 tokenId, uint256 amount, bytes memory data) public {
require(msg.sender == issuer, "only issuer");
_mint(to, tokenId, amount, data);
}
function mint(address to, uint256 tokenId, uint256 amount) external {
mint(to, tokenId, amount, "");
}
function createUserRecord(
address owner,
address user,
uint256 tokenId,
uint64 amount,
uint64 expiry
) external returns (uint256) {
require(msg.sender == owner || isApprovedForAll(owner, msg.sender), "caller is not owner nor approved");
require(user != address(0), "user cannot be zero address");
require(amount > 0, "amount must be positive");
require(expiry > block.timestamp, "expiry must be in future");
require(
balanceOf(owner, tokenId) - _frozenBalances[tokenId][owner] >= amount,
"amount exceeds available balance"
);
_frozenBalances[tokenId][owner] += amount;
uint256 recordId = ++_curRecordId;
_records[recordId] = UserRecord(tokenId, owner, amount, user, expiry);
_userRecordIds[tokenId][user].push(recordId);
emit CreateUserRecord(recordId, tokenId, amount, owner, user, expiry);
return recordId;
}
function deleteUserRecord(uint256 recordId) external {
UserRecord memory record = _records[recordId];
require(record.owner != address(0), "record does not exist");
bool isUser = msg.sender == record.user;
bool isOwnerOrApproved = msg.sender == record.owner || isApprovedForAll(record.owner, msg.sender);
require(isUser || isOwnerOrApproved, "caller is not user nor owner/approved");
if (isOwnerOrApproved && !isUser) {
if (block.timestamp < record.expiry) {
require(ownerMayEndEarly, "owner cannot end early");
}
}
_frozenBalances[record.tokenId][record.owner] -= record.amount;
delete _records[recordId];
uint256[] storage recordIds = _userRecordIds[record.tokenId][record.user];
for (uint256 i = 0; i < recordIds.length; i++) {
if (recordIds[i] == recordId) {
recordIds[i] = recordIds[recordIds.length - 1];
recordIds.pop();
break;
}
}
emit DeleteUserRecord(recordId);
}
function frozenBalanceOf(address account, uint256 tokenId) external view returns (uint256) {
return _frozenBalances[tokenId][account];
}
function usableBalanceOf(address account, uint256 tokenId) external view returns (uint256) {
uint256 total = 0;
uint256[] storage recordIds = _userRecordIds[tokenId][account];
for (uint256 i = 0; i < recordIds.length; i++) {
UserRecord storage rec = _records[recordIds[i]];
if (block.timestamp < rec.expiry) {
total += rec.amount;
}
}
return total;
}
function userRecordOf(uint256 recordId) external view returns (UserRecord memory) {
return _records[recordId];
}
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC5006).interfaceId || super.supportsInterface(interfaceId);
}
function _update(
address from,
address to,
uint256[] memory ids,
uint256[] memory values
) internal virtual override {
super._update(from, to, ids, values);
if (from != address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
require(
balanceOf(from, id) >= _frozenBalances[id][from],
"transfer exceeds available balance"
);
}
}
}
}
|