Skip to content

Commit

Permalink
feat: add a view function in RoyaltiesRegistry
Browse files Browse the repository at this point in the history
Add a function that return the royalties without touching the storage.
The previous one that has a royalty type cache is named: getRoyaltiesWithTypeCache now
  • Loading branch information
adjisb committed Nov 1, 2023
1 parent bebf1a1 commit 3a8883f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
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
14 changes: 10 additions & 4 deletions packages/marketplace/contracts/interfaces/IRoyaltiesProvider.sol
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

1 comment on commit 3a8883f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

96.89%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts
   Exchange.sol95.65%96.67%93.75%95.65%173, 64
   ExchangeCore.sol98.84%96.67%100%100%80
   OrderValidator.sol97.87%95.45%100%100%35
   RoyaltiesRegistry.sol95.10%86.11%100%97.73%130–131, 181, 203–204, 250, 64
   TransferManager.sol95.97%90%100%98.61%152, 155, 248, 252, 79
   Whitelist.sol69.35%50%78.57%78.57%103, 107, 110, 118, 121, 124, 140–141, 53, 65, 65–66, 70, 70–71, 75
packages/marketplace/contracts/interfaces
   IOrderValidator.sol100%100%100%100%
   IRoyaltiesProvider.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%
   IWhitelist.sol100%100%100%100%
packages/marketplace/contracts/libraries
   LibAsset.sol100%100%100%100%
   LibMath.sol100%100%100%100%
   LibOrder.sol100%100%100%100%

Please sign in to comment.