Skip to content

Commit

Permalink
Update End rental function
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Sep 14, 2023
1 parent c1c7615 commit 03d4ed9
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 27 deletions.
21 changes: 3 additions & 18 deletions contracts/OriumRentalProtocol.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,29 +240,14 @@ contract OriumRentalProtocol is Initializable, AccessControlUpgradeable, EIP712U

function endRental(address _tokenAddress, uint256 _tokenId) external {
address _owner = IERC721(_tokenAddress).ownerOf(_tokenId);
address _taker = IRolesRegistry(rolesRegistry).lastGrantee(USER_ROLE, _owner, _tokenAddress, _tokenId);
require(
IRolesRegistry(rolesRegistry).hasUniqueRole(USER_ROLE, _tokenAddress, _tokenId, _owner, _taker),
IRolesRegistry(rolesRegistry).hasUniqueRole(USER_ROLE, _tokenAddress, _tokenId, _owner, msg.sender),
"OriumRentalProtocol: Invalid role"
);

require(msg.sender == _taker, "OriumRentalProtocol: Only taker can end rental");
require(_taker != address(0), "OriumRentalProtocol: NFT is not rented");
IRolesRegistry(rolesRegistry).revokeRoleFrom(USER_ROLE, _tokenAddress, _tokenId, _owner, msg.sender);

if (msg.sender == _owner) {
uint64 _expirationDate = IRolesRegistry(rolesRegistry).roleExpirationDate(
USER_ROLE,
_tokenAddress,
_tokenId,
_owner,
_taker
);
require(block.timestamp > _expirationDate, "OriumRentalProtocol: Rental hasn't ended yet");
}

IRolesRegistry(rolesRegistry).revokeRoleFrom(USER_ROLE, _tokenAddress, _tokenId, _owner, _taker);

emit RentalEnded(_owner, _taker, _tokenAddress, _tokenId);
emit RentalEnded(_owner, msg.sender, _tokenAddress, _tokenId);
}

function hashRentalOffer(RentalOffer memory offer) public view returns (bytes32) {
Expand Down
234 changes: 225 additions & 9 deletions contracts/interfaces/IRolesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,236 @@

pragma solidity 0.8.9;

import { IERC7432 } from "./IERC7432.sol";
import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

/// @notice The Nft Roles interface enables granting and revoking temporary roles for tokens.
interface IRolesRegistry is IERC7432 {

/// @notice Returns the last grantee of a role.
/// @title ERC-7432 Non-Fungible Token Roles
/// @dev See https://eips.ethereum.org/EIPS/eip-7432
/// Note: the ERC-165 identifier for this interface is 0xd7e151ef.
interface IRolesRegistry is IERC165 {
struct RoleData {
uint64 expirationDate;
bytes data;
}

/** Events **/

/// @notice Emitted when a role is granted.
/// @param _role The role identifier.
/// @param _grantor The role creator
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
function lastGrantee(
/// @param _grantor The user assigning the role.
/// @param _grantee The user receiving the role.
/// @param _expirationDate The expiration date of the role.
/// @param _data Any additional data about the role.
event RoleGranted(
bytes32 indexed _role,
address indexed _tokenAddress,
uint256 indexed _tokenId,
address _grantor,
address _grantee,
uint64 _expirationDate,
bytes _data
);

/// @notice Emitted when a role is revoked.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _revoker The user revoking the role.
/// @param _grantee The user that receives the role revocation.
event RoleRevoked(
bytes32 indexed _role,
address indexed _tokenAddress,
uint256 indexed _tokenId,
address _revoker,
address _grantee
);

/// @notice Emitted when a user is approved to manage any role on behalf of another user.
/// @param _tokenAddress The token address.
/// @param _operator The user approved to grant and revoke roles.
/// @param _isApproved The approval status.
event RoleApprovalForAll(
address indexed _tokenAddress,
address indexed _operator,
bool _isApproved
);

/// @notice Emitted when a user is approved to manage the roles of an NFT on behalf of another user.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _operator The user approved to grant and revoke roles.
/// @param _isApproved The approval status.
event RoleApproval(
address indexed _tokenAddress,
uint256 indexed _tokenId,
address _operator,
bool _isApproved
);

/** External Functions **/

/// @notice Grants a role to a user.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantee The user receiving the role.
/// @param _expirationDate The expiration date of the role.
/// @param _data Any additional data about the role.
function grantRole(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantee,
uint64 _expirationDate,
bytes calldata _data
) external;

/// @notice Revokes a role from a user.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantee The user that receives the role revocation.
function revokeRole(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantee
) external;

/// @notice Grants a role on behalf of a user.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user assigning the role.
/// @param _grantee The user that receives the role.
/// @param _expirationDate The expiration date of the role.
/// @param _data Any additional data about the role.
function grantRoleFrom(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee,
uint64 _expirationDate,
bytes calldata _data
) external;

/// @notice Revokes a role on behalf of a user.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _revoker The user revoking the role.
/// @param _grantee The user that receives the role revocation.
function revokeRoleFrom(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _revoker,
address _grantee
) external;

/// @notice Approves operator to grant and revoke any roles on behalf of another user.
/// @param _tokenAddress The token address.
/// @param _operator The user approved to grant and revoke roles.
/// @param _approved The approval status.
function setRoleApprovalForAll(
address _tokenAddress,
uint256 _tokenId
) external view returns (address grantee_);
}
address _operator,
bool _approved
) external;

/// @notice Approves operator to grant and revoke roles of an NFT on behalf of another user.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _operator The user approved to grant and revoke roles.
/// @param _approved The approval status.
function approveRole(
address _tokenAddress,
uint256 _tokenId,
address _operator,
bool _approved
) external;

/** View Functions **/

/// @notice Checks if a user has a role.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user that assigned the role.
/// @param _grantee The user that received the role.
function hasRole(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee
) external view returns (bool);

/// @notice Checks if a user has a unique role.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user that assigned the role.
/// @param _grantee The user that received the role.
function hasUniqueRole(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee
) external view returns (bool);

/// @notice Returns the custom data of a role assignment.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user that assigned the role.
/// @param _grantee The user that received the role.
function roleData(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee
) external view returns (bytes memory data_);

/// @notice Returns the expiration date of a role assignment.
/// @param _role The role identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user that assigned the role.
/// @param _grantee The user that received the role.
function roleExpirationDate(
bytes32 _role,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee
) external view returns (uint64 expirationDate_);

/// @notice Checks if the grantor approved the operator for all NFTs.
/// @param _tokenAddress The token address.
/// @param _grantor The user that approved the operator.
/// @param _operator The user that can grant and revoke roles.
function isRoleApprovedForAll(
address _tokenAddress,
address _grantor,
address _operator
) external view returns (bool);

/// @notice Checks if the grantor approved the operator for a specific NFT.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _grantor The user that approved the operator.
/// @param _operator The user approved to grant and revoke roles.
function getApprovedRole(
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _operator
) external view returns (bool);

}

0 comments on commit 03d4ed9

Please sign in to comment.