-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathRoyaltyEngineV1.sol
More file actions
423 lines (397 loc) · 20 KB
/
RoyaltyEngineV1.sol
File metadata and controls
423 lines (397 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @author: manifold.xyz
import { ERC165, IERC165 } from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import { ERC165Checker } from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { AddressUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol";
import { SuperRareContracts } from "./libraries/SuperRareContracts.sol";
import { IManifold } from "./specs/IManifold.sol";
import { IRaribleV1, IRaribleV2 } from "./specs/IRarible.sol";
import { IFoundation } from "./specs/IFoundation.sol";
import { ISuperRareRegistry } from "./specs/ISuperRare.sol";
import { IEIP2981 } from "./specs/IEIP2981.sol";
import { IZoraOverride } from "./specs/IZoraOverride.sol";
import { IArtBlocksOverride } from "./specs/IArtBlocksOverride.sol";
import { IKODAV2Override } from "./specs/IKODAV2Override.sol";
import { IRoyaltyEngineV1 } from "./IRoyaltyEngineV1.sol";
import { IRoyaltyRegistry } from "./IRoyaltyRegistry.sol";
import { IRoyaltySplitter, Recipient } from "./overrides/IRoyaltySplitter.sol";
import { IFallbackRegistry } from "./overrides/IFallbackRegistry.sol";
/**
* @dev Engine to lookup royalty configurations
*/
contract RoyaltyEngineV1 is ERC165, OwnableUpgradeable, IRoyaltyEngineV1 {
using AddressUpgradeable for address;
// Use int16 for specs to support future spec additions
// When we add a spec, we also decrement the NONE value
// Anything > NONE and <= NOT_CONFIGURED is considered not configured
int16 private constant NONE = -1;
int16 private constant NOT_CONFIGURED = 0;
int16 private constant MANIFOLD = 1;
int16 private constant RARIBLEV1 = 2;
int16 private constant RARIBLEV2 = 3;
int16 private constant FOUNDATION = 4;
int16 private constant EIP2981 = 5;
int16 private constant SUPERRARE = 6;
int16 private constant ZORA = 7;
int16 private constant ARTBLOCKS = 8;
int16 private constant KNOWNORIGINV2 = 9;
int16 private constant ROYALTY_SPLITTER = 10;
int16 private constant FALLBACK = type(int16).max;
mapping(address => int16) _specCache;
address public royaltyRegistry;
IFallbackRegistry public immutable FALLBACK_REGISTRY;
constructor(address fallbackRegistry) {
FALLBACK_REGISTRY = IFallbackRegistry(fallbackRegistry);
}
function initialize(address _initialOwner, address royaltyRegistry_) public initializer {
_transferOwnership(_initialOwner);
require(ERC165Checker.supportsInterface(royaltyRegistry_, type(IRoyaltyRegistry).interfaceId));
royaltyRegistry = royaltyRegistry_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override (ERC165, IERC165) returns (bool) {
return interfaceId == type(IRoyaltyEngineV1).interfaceId || super.supportsInterface(interfaceId);
}
/**
* @dev Invalidate the cached spec (useful for situations where tooken royalty implementation changes to a different spec)
*/
function invalidateCachedRoyaltySpec(address tokenAddress) public {
address royaltyAddress = IRoyaltyRegistry(royaltyRegistry).getRoyaltyLookupAddress(tokenAddress);
delete _specCache[royaltyAddress];
}
/**
* @dev View function to get the cached spec of a token
*/
function getCachedRoyaltySpec(address tokenAddress) public view returns (int16) {
address royaltyAddress = IRoyaltyRegistry(royaltyRegistry).getRoyaltyLookupAddress(tokenAddress);
return _specCache[royaltyAddress];
}
/**
* @dev See {IRoyaltyEngineV1-getRoyalty}
*/
function getRoyalty(address tokenAddress, uint256 tokenId, uint256 value)
public
override
returns (address payable[] memory recipients, uint256[] memory amounts)
{
// External call to limit gas
try this._getRoyaltyAndSpec{gas: 100000}(tokenAddress, tokenId, value) returns (
address payable[] memory _recipients,
uint256[] memory _amounts,
int16 spec,
address royaltyAddress,
bool addToCache
) {
if (addToCache) _specCache[royaltyAddress] = spec;
return (_recipients, _amounts);
} catch {
revert("Invalid royalty amount");
}
}
/**
* @dev See {IRoyaltyEngineV1-getRoyaltyView}.
*/
function getRoyaltyView(address tokenAddress, uint256 tokenId, uint256 value)
public
view
override
returns (address payable[] memory recipients, uint256[] memory amounts)
{
// External call to limit gas
try this._getRoyaltyAndSpec{gas: 100000}(tokenAddress, tokenId, value) returns (
address payable[] memory _recipients, uint256[] memory _amounts, int16, address, bool
) {
return (_recipients, _amounts);
} catch {
revert("Invalid royalty amount");
}
}
/**
* @dev Get the royalty and royalty spec for a given token
*
* returns recipients array, amounts array, royalty spec, royalty address, whether or not to add to cache
*/
function _getRoyaltyAndSpec(address tokenAddress, uint256 tokenId, uint256 value)
external
view
returns (
address payable[] memory recipients,
uint256[] memory amounts,
int16 spec,
address royaltyAddress,
bool addToCache
)
{
require(msg.sender == address(this), "Only Engine");
royaltyAddress = IRoyaltyRegistry(royaltyRegistry).getRoyaltyLookupAddress(tokenAddress);
spec = _specCache[royaltyAddress];
if (spec <= NOT_CONFIGURED && spec > NONE) {
// No spec configured yet, so we need to detect the spec
addToCache = true;
// SuperRare handling
if (tokenAddress == SuperRareContracts.SUPERRARE_V1 || tokenAddress == SuperRareContracts.SUPERRARE_V2) {
try ISuperRareRegistry(SuperRareContracts.SUPERRARE_REGISTRY).tokenCreator(tokenAddress, tokenId)
returns (address payable creator) {
try ISuperRareRegistry(SuperRareContracts.SUPERRARE_REGISTRY).calculateRoyaltyFee(
tokenAddress, tokenId, value
) returns (uint256 amount) {
recipients = new address payable[](1);
amounts = new uint256[](1);
recipients[0] = creator;
amounts[0] = amount;
return (recipients, amounts, SUPERRARE, royaltyAddress, addToCache);
} catch { }
} catch { }
}
try IEIP2981(royaltyAddress).royaltyInfo(tokenId, value) returns (address recipient, uint256 amount) {
require(amount < value, "Invalid royalty amount");
uint32 recipientSize;
assembly {
recipientSize := extcodesize(recipient)
}
if (recipientSize > 0) {
try IRoyaltySplitter(recipient).getRecipients() returns (Recipient[] memory splitRecipients) {
recipients = new address payable[](splitRecipients.length);
amounts = new uint256[](splitRecipients.length);
uint256 sum = 0;
uint256 splitRecipientsLength = splitRecipients.length;
for (uint256 i = 0; i < splitRecipientsLength;) {
Recipient memory splitRecipient = splitRecipients[i];
recipients[i] = payable(splitRecipient.recipient);
uint256 splitAmount = splitRecipient.bps * amount / 10000;
amounts[i] = splitAmount;
sum += splitAmount;
unchecked {
++i;
}
}
// sum can be less than amount, otherwise small-value listings can break
require(sum <= amount, "Invalid split");
return (recipients, amounts, ROYALTY_SPLITTER, royaltyAddress, addToCache);
} catch { }
}
// Supports EIP2981. Return amounts
recipients = new address payable[](1);
amounts = new uint256[](1);
recipients[0] = payable(recipient);
amounts[0] = amount;
return (recipients, amounts, EIP2981, royaltyAddress, addToCache);
} catch { }
try IManifold(royaltyAddress).getRoyalties(tokenId) returns (
address payable[] memory recipients_, uint256[] memory bps
) {
// Supports manifold interface. Compute amounts
require(recipients_.length == bps.length);
return (recipients_, _computeAmounts(value, bps), MANIFOLD, royaltyAddress, addToCache);
} catch { }
try IRaribleV2(royaltyAddress).getRaribleV2Royalties(tokenId) returns (IRaribleV2.Part[] memory royalties) {
// Supports rarible v2 interface. Compute amounts
recipients = new address payable[](royalties.length);
amounts = new uint256[](royalties.length);
uint256 totalAmount;
for (uint256 i = 0; i < royalties.length; i++) {
recipients[i] = royalties[i].account;
amounts[i] = value * royalties[i].value / 10000;
totalAmount += amounts[i];
}
require(totalAmount < value, "Invalid royalty amount");
return (recipients, amounts, RARIBLEV2, royaltyAddress, addToCache);
} catch { }
try IRaribleV1(royaltyAddress).getFeeRecipients(tokenId) returns (address payable[] memory recipients_) {
// Supports rarible v1 interface. Compute amounts
recipients_ = IRaribleV1(royaltyAddress).getFeeRecipients(tokenId);
try IRaribleV1(royaltyAddress).getFeeBps(tokenId) returns (uint256[] memory bps) {
require(recipients_.length == bps.length);
return (recipients_, _computeAmounts(value, bps), RARIBLEV1, royaltyAddress, addToCache);
} catch { }
} catch { }
try IFoundation(royaltyAddress).getFees(tokenId) returns (
address payable[] memory recipients_, uint256[] memory bps
) {
// Supports foundation interface. Compute amounts
require(recipients_.length == bps.length);
return (recipients_, _computeAmounts(value, bps), FOUNDATION, royaltyAddress, addToCache);
} catch { }
try IZoraOverride(royaltyAddress).convertBidShares(tokenAddress, tokenId) returns (
address payable[] memory recipients_, uint256[] memory bps
) {
// Support Zora override
require(recipients_.length == bps.length);
return (recipients_, _computeAmounts(value, bps), ZORA, royaltyAddress, addToCache);
} catch { }
try IArtBlocksOverride(royaltyAddress).getRoyalties(tokenAddress, tokenId) returns (
address payable[] memory recipients_, uint256[] memory bps
) {
// Support Art Blocks override
require(recipients_.length == bps.length);
return (recipients_, _computeAmounts(value, bps), ARTBLOCKS, royaltyAddress, addToCache);
} catch { }
try IKODAV2Override(royaltyAddress).getKODAV2RoyaltyInfo(tokenAddress, tokenId, value) returns (
address payable[] memory _recipients, uint256[] memory _amounts
) {
// Support KODA V2 override
require(_recipients.length == _amounts.length);
return (_recipients, _amounts, KNOWNORIGINV2, royaltyAddress, addToCache);
} catch { }
try FALLBACK_REGISTRY.getRecipients(tokenAddress) returns (Recipient[] memory _recipients) {
uint256 recipientsLength = _recipients.length;
if (recipientsLength > 0) {
return _calculateFallback(_recipients, recipientsLength, value, royaltyAddress, addToCache);
}
} catch { }
// No supported royalties configured
return (recipients, amounts, NONE, royaltyAddress, addToCache);
} else {
// Spec exists, just execute the appropriate one
addToCache = false;
if (spec == NONE) {
return (recipients, amounts, spec, royaltyAddress, addToCache);
} else if (spec == FALLBACK) {
Recipient[] memory _recipients = FALLBACK_REGISTRY.getRecipients(tokenAddress);
return _calculateFallback(_recipients, _recipients.length, value, royaltyAddress, addToCache);
} else if (spec == MANIFOLD) {
// Manifold spec
uint256[] memory bps;
(recipients, bps) = IManifold(royaltyAddress).getRoyalties(tokenId);
require(recipients.length == bps.length);
return (recipients, _computeAmounts(value, bps), spec, royaltyAddress, addToCache);
} else if (spec == RARIBLEV2) {
// Rarible v2 spec
IRaribleV2.Part[] memory royalties;
royalties = IRaribleV2(royaltyAddress).getRaribleV2Royalties(tokenId);
recipients = new address payable[](royalties.length);
amounts = new uint256[](royalties.length);
uint256 totalAmount;
for (uint256 i = 0; i < royalties.length; i++) {
recipients[i] = royalties[i].account;
amounts[i] = value * royalties[i].value / 10000;
totalAmount += amounts[i];
}
require(totalAmount < value, "Invalid royalty amount");
return (recipients, amounts, spec, royaltyAddress, addToCache);
} else if (spec == RARIBLEV1) {
// Rarible v1 spec
uint256[] memory bps;
recipients = IRaribleV1(royaltyAddress).getFeeRecipients(tokenId);
bps = IRaribleV1(royaltyAddress).getFeeBps(tokenId);
require(recipients.length == bps.length);
return (recipients, _computeAmounts(value, bps), spec, royaltyAddress, addToCache);
} else if (spec == FOUNDATION) {
// Foundation spec
uint256[] memory bps;
(recipients, bps) = IFoundation(royaltyAddress).getFees(tokenId);
require(recipients.length == bps.length);
return (recipients, _computeAmounts(value, bps), spec, royaltyAddress, addToCache);
} else if (spec == EIP2981 || spec == ROYALTY_SPLITTER) {
// EIP2981 spec
(address recipient, uint256 amount) = IEIP2981(royaltyAddress).royaltyInfo(tokenId, value);
require(amount < value, "Invalid royalty amount");
if (spec == ROYALTY_SPLITTER) {
Recipient[] memory splitRecipients = IRoyaltySplitter(recipient).getRecipients();
recipients = new address payable[](splitRecipients.length);
amounts = new uint256[](splitRecipients.length);
uint256 sum = 0;
uint256 splitRecipientsLength = splitRecipients.length;
for (uint256 i = 0; i < splitRecipientsLength;) {
Recipient memory splitRecipient = splitRecipients[i];
recipients[i] = payable(splitRecipient.recipient);
uint256 splitAmount = splitRecipient.bps * amount / 10000;
amounts[i] = splitAmount;
sum += splitAmount;
unchecked {
++i;
}
}
// sum can be less than amount, otherwise small-value listings can break
require(sum <= value, "Invalid split");
return (recipients, amounts, spec, royaltyAddress, addToCache);
}
recipients = new address payable[](1);
amounts = new uint256[](1);
recipients[0] = payable(recipient);
amounts[0] = amount;
return (recipients, amounts, spec, royaltyAddress, addToCache);
} else if (spec == SUPERRARE) {
// SUPERRARE spec
address payable creator =
ISuperRareRegistry(SuperRareContracts.SUPERRARE_REGISTRY).tokenCreator(tokenAddress, tokenId);
uint256 amount = ISuperRareRegistry(SuperRareContracts.SUPERRARE_REGISTRY).calculateRoyaltyFee(
tokenAddress, tokenId, value
);
recipients = new address payable[](1);
amounts = new uint256[](1);
recipients[0] = creator;
amounts[0] = amount;
return (recipients, amounts, spec, royaltyAddress, addToCache);
} else if (spec == ZORA) {
// Zora spec
uint256[] memory bps;
(recipients, bps) = IZoraOverride(royaltyAddress).convertBidShares(tokenAddress, tokenId);
require(recipients.length == bps.length);
return (recipients, _computeAmounts(value, bps), spec, royaltyAddress, addToCache);
} else if (spec == ARTBLOCKS) {
// Art Blocks spec
uint256[] memory bps;
(recipients, bps) = IArtBlocksOverride(royaltyAddress).getRoyalties(tokenAddress, tokenId);
require(recipients.length == bps.length);
return (recipients, _computeAmounts(value, bps), spec, royaltyAddress, addToCache);
} else if (spec == KNOWNORIGINV2) {
// KnownOrigin.io V2 spec (V3 falls under EIP2981)
(recipients, amounts) =
IKODAV2Override(royaltyAddress).getKODAV2RoyaltyInfo(tokenAddress, tokenId, value);
require(recipients.length == amounts.length);
return (recipients, amounts, spec, royaltyAddress, addToCache);
}
}
}
function _calculateFallback(
Recipient[] memory _recipients,
uint256 recipientsLength,
uint256 value,
address royaltyAddress,
bool addToCache
)
internal
pure
returns (
address payable[] memory recipients,
uint256[] memory amounts,
int16 spec,
address _royaltyAddress,
bool _addToCache
)
{
recipients = new address payable[](recipientsLength);
amounts = new uint256[](recipientsLength);
uint256 totalAmount;
for (uint256 i = 0; i < recipientsLength;) {
Recipient memory recipient = _recipients[i];
recipients[i] = payable(recipient.recipient);
uint256 amount = value * recipient.bps / 10_000;
amounts[i] = amount;
totalAmount += amount;
unchecked {
++i;
}
}
require(totalAmount < value, "Invalid royalty amount");
return (recipients, amounts, FALLBACK, royaltyAddress, addToCache);
}
/**
* Compute royalty amounts
*/
function _computeAmounts(uint256 value, uint256[] memory bps) private pure returns (uint256[] memory amounts) {
amounts = new uint256[](bps.length);
uint256 totalAmount;
for (uint256 i = 0; i < bps.length; i++) {
amounts[i] = value * bps[i] / 10000;
totalAmount += amounts[i];
}
require(totalAmount < value, "Invalid royalty amount");
return amounts;
}
}