Skip to content

Commit

Permalink
Merge pull request #57 from OriumNetwork/ON-932-update-marketplace-sft
Browse files Browse the repository at this point in the history
On 932 update marketplace sft
  • Loading branch information
EduardoMelo00 authored Jul 14, 2024
2 parents 7240d13 + 3bf755b commit 225c1c0
Show file tree
Hide file tree
Showing 11 changed files with 1,580 additions and 492 deletions.
87 changes: 46 additions & 41 deletions contracts/OriumSftMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Initializable } from '@openzeppelin/contracts-upgradeable/proxy/utils/I
import { PausableUpgradeable } from '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import { IERC1155 } from '@openzeppelin/contracts/token/ERC1155/IERC1155.sol';
import { IERC7589 } from './interfaces/IERC7589.sol';
import { IERC7589Legacy } from './interfaces/IERC7589Legacy.sol';
import { LibOriumSftMarketplace, RentalOffer, CommitAndGrantRoleParams } from './libraries/LibOriumSftMarketplace.sol';
import { IOriumMarketplaceRoyalties } from './interfaces/IOriumMarketplaceRoyalties.sol';

Expand All @@ -21,6 +22,9 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
/// @dev oriumMarketplaceRoyalties stores the collection royalties and fees
address public oriumMarketplaceRoyalties;

/// @dev Aavegotchi Wearable address (legacy, only valid on Polygon)
address public constant aavegotchiWearableAddress = 0x58de9AaBCaeEC0f69883C94318810ad79Cc6a44f;

/// @dev hashedOffer => bool
mapping(bytes32 => bool) public isCreated;

Expand Down Expand Up @@ -128,9 +132,10 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
_validateCreateRentalOffer(_offer, _rolesRegistryAddress);

if (_offer.commitmentId == 0) {
_offer.commitmentId = IERC7589(_rolesRegistryAddress).commitTokens(
_offer.lender,
_offer.commitmentId = _commitOrLockTokens(
_offer.tokenAddress,
_rolesRegistryAddress,
_offer.lender,
_offer.tokenId,
_offer.tokenAmount
);
Expand Down Expand Up @@ -198,36 +203,6 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
emit RentalStarted(_offer.lender, _offer.nonce, msg.sender, _expirationDate);
}

/**
* @notice Cancels a rental offer.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
*/
function cancelRentalOffer(RentalOffer calldata _offer) external whenNotPaused {
bytes32 _offerHash = LibOriumSftMarketplace.hashRentalOffer(_offer);
require(isCreated[_offerHash], 'OriumSftMarketplace: Offer not created');
require(msg.sender == _offer.lender, 'OriumSftMarketplace: Only lender can cancel a rental offer');
require(
nonceDeadline[_offer.lender][_offer.nonce] > block.timestamp,
'OriumSftMarketplace: Nonce expired or not used yet'
);

IERC7589 _rolesRegistry = IERC7589(
IOriumMarketplaceRoyalties(oriumMarketplaceRoyalties).sftRolesRegistryOf(_offer.tokenAddress)
);
// if There are no active rentals, release tokens (else, tokens will be released via `batchReleaseTokens`)
// this is ok for single-role tokens, but tokens with multiple roles might revert if another non-revocable role exists
// if token amount is 0, it means that the tokens were already released, trying to release it again would revert the transaction
if (
rentals[_offerHash].expirationDate < block.timestamp &&
_rolesRegistry.tokenAmountOf(_offer.commitmentId) > 0
) {
_rolesRegistry.releaseTokens(_offer.commitmentId);
}

nonceDeadline[msg.sender][_offer.nonce] = uint64(block.timestamp);
emit RentalOfferCancelled(_offer.lender, _offer.nonce);
}

/**
* @notice Delist a rental offer.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
Expand All @@ -242,10 +217,18 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
*/
function delistRentalOfferAndWithdraw(RentalOffer calldata _offer) external whenNotPaused {
_delistRentalOffer(_offer);
IERC7589 _rolesRegistry = IERC7589(
IOriumMarketplaceRoyalties(oriumMarketplaceRoyalties).sftRolesRegistryOf(_offer.tokenAddress)
);
_rolesRegistry.releaseTokens(_offer.commitmentId);

if (_offer.tokenAddress == aavegotchiWearableAddress) {
IERC7589Legacy _rolesRegistryLegacy = IERC7589Legacy(
IOriumMarketplaceRoyalties(oriumMarketplaceRoyalties).sftRolesRegistryOf(_offer.tokenAddress)
);
_rolesRegistryLegacy.releaseTokens(_offer.commitmentId);
} else {
IERC7589 _rolesRegistry = IERC7589(
IOriumMarketplaceRoyalties(oriumMarketplaceRoyalties).sftRolesRegistryOf(_offer.tokenAddress)
);
_rolesRegistry.unlockTokens(_offer.commitmentId);
}
}

/**
Expand Down Expand Up @@ -301,12 +284,12 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
_params[i].tokenAddress
);

IERC7589 _rolesRegistry = IERC7589(_rolesRegistryAddress);
uint256 _validCommitmentId = _params[i].commitmentId;
if (_params[i].commitmentId == 0) {
_validCommitmentId = _rolesRegistry.commitTokens(
msg.sender,
_validCommitmentId = _commitOrLockTokens(
_params[i].tokenAddress,
_rolesRegistryAddress,
msg.sender,
_params[i].tokenId,
_params[i].tokenAmount
);
Expand All @@ -323,7 +306,7 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
);
}

_rolesRegistry.grantRole(
IERC7589(_rolesRegistryAddress).grantRole(
_validCommitmentId,
_params[i].role,
_params[i].grantee,
Expand Down Expand Up @@ -358,6 +341,29 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
}

/** ######### Internals ########### **/

/**
* @dev Validates if is wearable address to use commit or lock functions.
* @param _tokenAddress The SFT tokenAddresses.
* @param _rolesRegistryAddress roles registry address.
* @param _lender The address of the user lending the SFT.
* @param _tokenId The tokenId of the SFT to rent.
* @param _tokenAmount The amount of SFT to rent.
*/
function _commitOrLockTokens(
address _tokenAddress,
address _rolesRegistryAddress,
address _lender,
uint256 _tokenId,
uint256 _tokenAmount
) internal returns (uint256) {
if (_tokenAddress == aavegotchiWearableAddress) {
return IERC7589Legacy(_rolesRegistryAddress).commitTokens(_lender, _tokenAddress, _tokenId, _tokenAmount);
} else {
return IERC7589(_rolesRegistryAddress).lockTokens(_lender, _tokenAddress, _tokenId, _tokenAmount);
}
}

/**
* @dev Validates the create rental offer.
* @param _offer The rental offer struct.
Expand Down Expand Up @@ -490,6 +496,5 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
function setOriumMarketplaceRoyalties(address _oriumMarketplaceRoyalties) external onlyOwner {
oriumMarketplaceRoyalties = _oriumMarketplaceRoyalties;
}

/** ######### Getters ########### **/
}
28 changes: 0 additions & 28 deletions contracts/interfaces/ICommitTokensAndGrantRoleExtension.sol

This file was deleted.

Loading

0 comments on commit 225c1c0

Please sign in to comment.