Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a view function in RoyaltiesRegistry #1284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/marketplace/contracts/Exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ contract Exchange is
_unpause();
}

/// @notice Return all the information to calculate how much will be deduced from an exchange
/// @param token collection token address
/// @param tokenId tokenId for this collection
/// @return feePrimary fees for the primary market
/// @return feeSecondary fees for the secondary market
/// @return royalties royalties taken (receiver, valuePercent)
function getDeductibles(
address token,
uint256 tokenId
) external view returns (uint256 feePrimary, uint256 feeSecondary, IRoyaltiesProvider.Part[] memory royalties) {
return (protocolFeePrimary, protocolFeeSecondary, royaltiesRegistry.getRoyalties(token, tokenId));
}

/// @dev Check if fees & royalties should be skipped for users with the EXCHANGE_ADMIN_ROLE.
/// @param from Address to check.
/// @return True if fees should be skipped, false otherwise.
Expand Down
62 changes: 42 additions & 20 deletions packages/marketplace/contracts/RoyaltiesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,21 @@ contract RoyaltiesRegistry is OwnableUpgradeable, IRoyaltiesProvider {
/// @param token Address of the token.
/// @param tokenId ID of the token.
/// @return An array containing royalty parts.
function getRoyalties(address token, uint256 tokenId) external override returns (Part[] memory) {
function getRoyalties(address token, uint256 tokenId) external view override returns (Part[] memory) {
uint256 royaltiesProviderData = royaltiesProviders[token];
address royaltiesProvider = address(uint160(royaltiesProviderData));
RoyaltiesType royaltiesType = _getRoyaltiesType(royaltiesProviderData);
if (royaltiesType == RoyaltiesType.UNSET) {
royaltiesType = _calculateRoyaltiesType(token, royaltiesProvider);
}
return _getRoyalties(royaltiesType, token, tokenId, royaltiesProvider);
}

/// @notice Fetches royalties for a given token and token ID (cache the type if needed).
/// @param token Address of the token.
/// @param tokenId ID of the token.
/// @return An array containing royalty parts.
function getRoyaltiesWithTypeCache(address token, uint256 tokenId) external override returns (Part[] memory) {
uint256 royaltiesProviderData = royaltiesProviders[token];

address royaltiesProvider = address(uint160(royaltiesProviderData));
Expand All @@ -137,24 +151,7 @@ contract RoyaltiesRegistry is OwnableUpgradeable, IRoyaltiesProvider {
//saving royalties type
_setRoyaltiesType(token, royaltiesType, royaltiesProvider);
}

//case royaltiesType = 1, royalties are set in royaltiesByToken
if (royaltiesType == RoyaltiesType.BY_TOKEN) {
return royaltiesByToken[token].royalties;
}

//case royaltiesType = 2, royalties from external provider
if (royaltiesType == RoyaltiesType.EXTERNAL_PROVIDER) {
return _providerExtractor(token, tokenId, royaltiesProvider);
}

//case royaltiesType = 3, royalties EIP-2981
if (royaltiesType == RoyaltiesType.EIP2981) {
return _getRoyaltiesEIP2981(token, tokenId);
}

// case royaltiesType = 4, unknown/empty royalties
return new Part[](0);
return _getRoyalties(royaltiesType, token, tokenId, royaltiesProvider);
}

/// @notice Returns provider address for token contract from royaltiesProviders mapping
Expand Down Expand Up @@ -267,7 +264,7 @@ contract RoyaltiesRegistry is OwnableUpgradeable, IRoyaltiesProvider {
address token,
uint256 tokenId,
address providerAddress
) internal returns (Part[] memory) {
) internal view returns (Part[] memory) {
try IRoyaltiesProvider(providerAddress).getRoyalties(token, tokenId) returns (Part[] memory result) {
return result;
} catch {
Expand All @@ -292,6 +289,31 @@ contract RoyaltiesRegistry is OwnableUpgradeable, IRoyaltiesProvider {
return result;
}

/// @notice Fetches royalties for a given token and token ID.
/// @param royaltiesType type of royalty
/// @param token Address of the token.
/// @param tokenId ID of the token.
/// @param royaltiesProvider The address of the royalties provider.
/// @return An array containing royalty parts.
function _getRoyalties(
RoyaltiesType royaltiesType,
address token,
uint256 tokenId,
address royaltiesProvider
) internal view returns (Part[] memory) {
if (royaltiesType == RoyaltiesType.BY_TOKEN) {
return royaltiesByToken[token].royalties;
}
if (royaltiesType == RoyaltiesType.EXTERNAL_PROVIDER) {
return _providerExtractor(token, tokenId, royaltiesProvider);
}
if (royaltiesType == RoyaltiesType.EIP2981) {
return _getRoyaltiesEIP2981(token, tokenId);
}
// case royaltiesType = 4, unknown/empty royalties
return new Part[](0);
}

// slither-disable-next-line unused-state
uint256[50] private __gap;
}
2 changes: 1 addition & 1 deletion packages/marketplace/contracts/TransferManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ abstract contract TransferManager is Initializable, ITransferManager {
DealSide memory nftSide
) internal returns (uint256) {
(address token, uint256 tokenId) = LibAsset.decodeToken(nftSide.asset.assetType);
IRoyaltiesProvider.Part[] memory royalties = royaltiesRegistry.getRoyalties(token, tokenId);
IRoyaltiesProvider.Part[] memory royalties = royaltiesRegistry.getRoyaltiesWithTypeCache(token, tokenId);
uint256 totalRoyalties;
uint256 len = royalties.length;
for (uint256 i; i < len; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ interface IRoyaltiesProvider {
uint256 value;
}

/// @notice Calculates all roaylties in token for tokenId
/// @notice Calculates all royalties in token for tokenId
/// @param token Address of token
/// @param tokenId of the token we want to calculate royalites
/// @return A LibPart.Part with allroyalties for token
function getRoyalties(address token, uint256 tokenId) external returns (Part[] memory);
/// @param tokenId of the token we want to calculate royalties
/// @return A LibPart.Part with all royalties for token
function getRoyalties(address token, uint256 tokenId) external view returns (Part[] memory);

/// @notice Calculates all royalties in token for tokenId as getRoyalties caching the royalty type
/// @param token Address of token
/// @param tokenId of the token we want to calculate royalties
/// @return A LibPart.Part with all royalties for token
function getRoyaltiesWithTypeCache(address token, uint256 tokenId) external returns (Part[] memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ contract RoyaltiesProviderMock is IRoyaltiesProvider {
return royaltiesTest[token][tokenId];
}

function getRoyaltiesWithTypeCache(address token, uint256 tokenId) external view override returns (Part[] memory) {
return royaltiesTest[token][tokenId];
}

function initializeProvider(address token, uint256 tokenId, Part[] memory royalties) public {
delete royaltiesTest[token][tokenId];
for (uint256 i = 0; i < royalties.length; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion packages/marketplace/test/RoyaltiesRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('RoyaltiesRegistry.sol', function () {
)
).to.be.equal(0);

await RoyaltiesRegistryAsUser.getRoyalties(
await RoyaltiesRegistryAsUser.getRoyaltiesWithTypeCache(
await ERC721WithRoyaltyV2981.getAddress(),
1
);
Expand Down
Loading