Skip to content

Commit

Permalink
ON-489: End Rental with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Sep 29, 2023
1 parent ae6d21a commit 0b96abb
Show file tree
Hide file tree
Showing 2 changed files with 363 additions and 228 deletions.
70 changes: 70 additions & 0 deletions contracts/OriumMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ contract OriumMarketplace is Initializable, OwnableUpgradeable, PausableUpgradea
*/
event RentalOfferCancelled(uint256 indexed nonce, address indexed lender);

/**
* @param tokenAddress The address of the contract of the NFT rented
* @param tokenId The tokenId of the rented NFT
* @param nonce The nonce of the rental offer
* @param lender The address of the lender
* @param borrower The address of the borrower
*/
event RentalEnded(
address indexed tokenAddress,
uint256 indexed tokenId,
uint256 indexed nonce,
address lender,
address borrower
);

/** ######### Modifiers ########### **/

/**
Expand Down Expand Up @@ -403,6 +418,61 @@ contract OriumMarketplace is Initializable, OwnableUpgradeable, PausableUpgradea
);
}

/**
* @notice Ends the rental.
* @dev Can only be called by the borrower.
* @dev Borrower needs to approve marketplace to revoke the roles.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
*/
function endRental(RentalOffer memory _offer) external {
_validateEndRental(_offer);

_batchRevokeRole(_offer.roles, _offer.tokenAddress, _offer.tokenId, _offer.lender, _offer.borrower);

emit RentalEnded(_offer.tokenAddress, _offer.tokenId, _offer.nonce, _offer.lender, _offer.borrower);
}

/**
* @dev Validates the end rental.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
*/
function _validateEndRental(RentalOffer memory _offer) internal view {
bytes32 _offerHash = hashRentalOffer(_offer);
require(isCreated[_offerHash], "OriumMarketplace: Offer not created");
require(msg.sender == _offer.borrower, "OriumMarketplace: Only borrower can end a rental");
require(nonceDeadline[_offer.lender][_offer.nonce] > block.timestamp, "OriumMarketplace: Rental expired");
require(
IRolesRegistry(rolesRegistry).hasUniqueRole(
_offer.roles[0], // We just need to check for the first role, since all roles are granted at the same time when renting
_offer.tokenAddress,
_offer.tokenId,
_offer.lender,
_offer.borrower
),
"OriumMarketplace: Borrower does not have the role anymore"
);
}

/**
* @dev Revokes the roles from the borrower.
* @param _roles The array of roles to be revoked from the borrower
* @param _tokenAddress The address of the contract of the NFT to rent
* @param _tokenId The tokenId of the NFT to rent
* @param _grantor The address of the user lending the NFT
* @param _grantee The address of the user renting the NFT
*/
function _batchRevokeRole(
bytes32[] memory _roles,
address _tokenAddress,
uint256 _tokenId,
address _grantor,
address _grantee
) internal {
for (uint256 i = 0; i < _roles.length; i++) {
IRolesRegistry(rolesRegistry).revokeRoleFrom(_roles[i], _tokenAddress, _tokenId, _grantor, _grantee);
}
}

/** ######### Getters ########### **/

/**
Expand Down
Loading

0 comments on commit 0b96abb

Please sign in to comment.