|
| 1 | +pragma solidity ^0.8.0; |
| 2 | + |
| 3 | +import "../../utils/Context.sol"; |
| 4 | +import "./extensions/IERC721Roles.sol"; |
| 5 | +import "../../utils/introspection/ERC165.sol"; |
| 6 | + |
| 7 | +contract ERC721RolesRentalAgreement is Context, IERC721RolesManager, ERC165 { |
| 8 | + // The status of the rent |
| 9 | + enum RentalStatus { |
| 10 | + pending, |
| 11 | + active, |
| 12 | + finished |
| 13 | + } |
| 14 | + |
| 15 | + // A representation of rental agreement terms |
| 16 | + struct RentalAgreement { |
| 17 | + // The duration of the rent, in seconds |
| 18 | + uint32 rentalDuration; |
| 19 | + // The timestamp after which the rental agreement has expired and is no longer valid |
| 20 | + uint32 expirationDate; |
| 21 | + // The timestamp corresponding to the start of the rental period |
| 22 | + uint32 startTime; |
| 23 | + // The fees in wei that a renter needs to pay to start the rent |
| 24 | + uint256 rentalFees; |
| 25 | + RentalStatus rentalStatus; |
| 26 | + } |
| 27 | + |
| 28 | + // The ERC721Roles token for which this contract can be used as a roles manager |
| 29 | + IERC721Roles public erc721Contract; |
| 30 | + |
| 31 | + // Mapping from tokenId to rental agreement |
| 32 | + mapping(uint256 => RentalAgreement) public tokenIdToRentalAgreement; |
| 33 | + |
| 34 | + // Mapping addresses to balances |
| 35 | + mapping(address => uint256) public balances; |
| 36 | + |
| 37 | + // The identifier of the role Renter |
| 38 | + bytes4 public renterRoleId = bytes4(keccak256("ERC721Roles::Renter")); |
| 39 | + |
| 40 | + constructor(IERC721Roles _erc721Contract) { |
| 41 | + erc721Contract = _erc721Contract; |
| 42 | + } |
| 43 | + |
| 44 | + // ===== Modifiers ====== // |
| 45 | + modifier onlyErc721Contract() { |
| 46 | + require( |
| 47 | + _msgSender() == address(erc721Contract), |
| 48 | + "ERC721RolesRentalAgreement: only erc721Contract contract can modify state" |
| 49 | + ); |
| 50 | + _; |
| 51 | + } |
| 52 | + |
| 53 | + function afterRolesManagerRemoved(uint256 tokenId) external view onlyErc721Contract { |
| 54 | + RentalAgreement memory agreement = tokenIdToRentalAgreement[tokenId]; |
| 55 | + require( |
| 56 | + agreement.rentalStatus != RentalStatus.active, |
| 57 | + "ERC721RolesRentalAgreement: can't remove the roles manager contract if there is an active rental" |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + // Allow the token's owner or operator to set up a new rental agreement. |
| 62 | + function setRentalAgreement( |
| 63 | + uint256 tokenId, |
| 64 | + uint32 duration, |
| 65 | + uint32 expirationDate, |
| 66 | + uint256 fees |
| 67 | + ) public { |
| 68 | + require( |
| 69 | + _isOwnerOrApproved(tokenId, _msgSender()), |
| 70 | + "ERC721RolesRentalAgreement: only owner or approver can set up a rental agreement" |
| 71 | + ); |
| 72 | + |
| 73 | + RentalAgreement memory currentAgreement = tokenIdToRentalAgreement[tokenId]; |
| 74 | + require( |
| 75 | + currentAgreement.rentalStatus != RentalStatus.active, |
| 76 | + "ERC721RolesRentalAgreement: can't update rental agreement if there is an active one already" |
| 77 | + ); |
| 78 | + |
| 79 | + RentalAgreement memory rentalAgreement = RentalAgreement( |
| 80 | + duration, |
| 81 | + expirationDate, |
| 82 | + 0, |
| 83 | + fees, |
| 84 | + RentalStatus.pending |
| 85 | + ); |
| 86 | + // Set the rental agreement |
| 87 | + tokenIdToRentalAgreement[tokenId] = rentalAgreement; |
| 88 | + } |
| 89 | + |
| 90 | + // startRental allows an address to start the rent by paying the fees |
| 91 | + function startRental(address forAddress, uint256 tokenId) public payable { |
| 92 | + RentalAgreement memory rentalAgreement = tokenIdToRentalAgreement[tokenId]; |
| 93 | + require( |
| 94 | + block.timestamp <= rentalAgreement.expirationDate, |
| 95 | + "ERC721RolesRentalAgreement: rental agreement expired" |
| 96 | + ); |
| 97 | + require( |
| 98 | + rentalAgreement.rentalStatus != RentalStatus.active, |
| 99 | + "ERC721RolesRentalAgreement: rental already in progress" |
| 100 | + ); |
| 101 | + |
| 102 | + uint256 rentalFees = rentalAgreement.rentalFees; |
| 103 | + require(msg.value >= rentalFees, "ERC721RolesRentalAgreement: value below the rental fees"); |
| 104 | + |
| 105 | + address owner = erc721Contract.ownerOf(tokenId); |
| 106 | + // Credit the fees to the owner's balance |
| 107 | + balances[owner] += rentalFees; |
| 108 | + // Credit the remaining value to the sender's balance |
| 109 | + balances[_msgSender()] += msg.value - rentalFees; |
| 110 | + |
| 111 | + // Start the rental |
| 112 | + rentalAgreement.rentalStatus = RentalStatus.active; |
| 113 | + rentalAgreement.startTime = uint32(block.timestamp); |
| 114 | + |
| 115 | + // Reflect the role in the ERC721 token |
| 116 | + erc721Contract.grantRole(forAddress, tokenId, renterRoleId); |
| 117 | + } |
| 118 | + |
| 119 | + // stopRental stops the rental |
| 120 | + function stopRental(address forAddress, uint256 tokenId) public { |
| 121 | + RentalAgreement memory rentalAgreement = tokenIdToRentalAgreement[tokenId]; |
| 122 | + require(rentalAgreement.rentalStatus == RentalStatus.active, "ERC721RolesRentalAgreement: rental not active"); |
| 123 | + require( |
| 124 | + block.timestamp - rentalAgreement.startTime >= rentalAgreement.rentalDuration, |
| 125 | + "ERC721RolesRentalAgreement: rental still ongoing" |
| 126 | + ); |
| 127 | + |
| 128 | + // Stop the rental |
| 129 | + rentalAgreement.rentalStatus = RentalStatus.finished; |
| 130 | + |
| 131 | + // Revoke the renter's role |
| 132 | + erc721Contract.revokeRole(forAddress, tokenId, renterRoleId); |
| 133 | + } |
| 134 | + |
| 135 | + // afterRoleGranted will be called back in `erc721Contract.grantRole` |
| 136 | + function afterRoleGranted( |
| 137 | + address fromAddress, |
| 138 | + address, |
| 139 | + uint256, |
| 140 | + bytes4 |
| 141 | + ) external view onlyErc721Contract { |
| 142 | + require(fromAddress == address(this), "ERC721RolesRentalAgreement: only this contract can set up renter roles"); |
| 143 | + } |
| 144 | + |
| 145 | + // afterRoleRevoked will be called back in `erc721Contract.revokeRole` |
| 146 | + function afterRoleRevoked( |
| 147 | + address fromAddress, |
| 148 | + address, |
| 149 | + uint256, |
| 150 | + bytes4 |
| 151 | + ) external view onlyErc721Contract { |
| 152 | + require(fromAddress == address(this), "ERC721RolesRentalAgreement: only this contract can revoke renter roles"); |
| 153 | + } |
| 154 | + |
| 155 | + function _isOwnerOrApproved(uint256 tokenId, address sender) internal view returns (bool) { |
| 156 | + address owner = erc721Contract.ownerOf(tokenId); |
| 157 | + return |
| 158 | + owner == sender || |
| 159 | + erc721Contract.getApproved(tokenId) == sender || |
| 160 | + erc721Contract.isApprovedForAll(owner, sender); |
| 161 | + } |
| 162 | + |
| 163 | + // Allow addresses to redeem their funds |
| 164 | + function redeemFunds(uint256 _value) public { |
| 165 | + require(_value <= balances[_msgSender()], "ERC721RolesRentalAgreement: not enough funds to redeem"); |
| 166 | + |
| 167 | + balances[_msgSender()] -= _value; |
| 168 | + |
| 169 | + // Check if the transfer is successful. |
| 170 | + require(_attemptETHTransfer(_msgSender(), _value), "ERC721RolesRentalAgreement: ETH transfer failed"); |
| 171 | + } |
| 172 | + |
| 173 | + function _attemptETHTransfer(address _to, uint256 _value) internal returns (bool) { |
| 174 | + // Here increase the gas limit a reasonable amount above the default, and try |
| 175 | + // to send ETH to the recipient. |
| 176 | + // NOTE: This might allow the recipient to attempt a limited reentrancy attack. |
| 177 | + (bool success, ) = _to.call{value: _value, gas: 30000}(""); |
| 178 | + return success; |
| 179 | + } |
| 180 | +} |
0 commit comments