@lukso/lsp8-contracts 0.15.0 → 0.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +2 -2
  2. package/artifacts/ILSP8IdentifiableDigitalAsset.json +0 -112
  3. package/artifacts/LSP8Burnable.json +0 -11
  4. package/artifacts/LSP8BurnableInitAbstract.json +0 -16
  5. package/artifacts/LSP8CappedSupply.json +0 -11
  6. package/artifacts/LSP8CappedSupplyInitAbstract.json +0 -16
  7. package/artifacts/LSP8Enumerable.json +0 -11
  8. package/artifacts/LSP8EnumerableInitAbstract.json +0 -16
  9. package/artifacts/LSP8IdentifiableDigitalAsset.json +0 -11
  10. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +0 -16
  11. package/artifacts/LSP8Mintable.json +2 -13
  12. package/artifacts/LSP8MintableInit.json +2 -13
  13. package/artifacts/LSP8Votes.json +1230 -0
  14. package/artifacts/LSP8VotesInitAbstract.json +1222 -0
  15. package/contracts/ILSP8IdentifiableDigitalAsset.sol +2 -8
  16. package/contracts/LSP8Constants.sol +4 -0
  17. package/contracts/LSP8IdentifiableDigitalAsset.sol +796 -36
  18. package/contracts/LSP8IdentifiableDigitalAssetInitAbstract.sol +806 -36
  19. package/contracts/extensions/LSP8CappedSupply.sol +1 -1
  20. package/contracts/extensions/LSP8CappedSupplyInitAbstract.sol +1 -1
  21. package/contracts/extensions/LSP8Enumerable.sol +6 -5
  22. package/contracts/extensions/LSP8EnumerableInitAbstract.sol +5 -5
  23. package/contracts/extensions/LSP8Votes.sol +94 -0
  24. package/contracts/extensions/LSP8VotesConstants.sol +8 -0
  25. package/contracts/extensions/LSP8VotesInitAbstract.sol +116 -0
  26. package/dist/index.cjs +5 -1
  27. package/dist/index.d.cts +18 -16
  28. package/dist/index.d.mts +18 -16
  29. package/dist/index.d.ts +18 -16
  30. package/dist/index.mjs +5 -1
  31. package/package.json +5 -6
  32. package/types/index.ts +3958 -1854
  33. package/contracts/LSP8IdentifiableDigitalAssetCore.sol +0 -809
  34. package/types/common.ts +0 -131
  35. package/types/contracts/ILSP8IdentifiableDigitalAsset.ts +0 -706
  36. package/types/contracts/LSP8IdentifiableDigitalAsset.ts +0 -778
  37. package/types/contracts/LSP8IdentifiableDigitalAssetInitAbstract.ts +0 -813
  38. package/types/contracts/extensions/LSP8Burnable.ts +0 -797
  39. package/types/contracts/extensions/LSP8BurnableInitAbstract.ts +0 -829
  40. package/types/contracts/extensions/LSP8CappedSupply.ts +0 -792
  41. package/types/contracts/extensions/LSP8CappedSupplyInitAbstract.ts +0 -824
  42. package/types/contracts/extensions/LSP8Enumerable.ts +0 -790
  43. package/types/contracts/extensions/LSP8EnumerableInitAbstract.ts +0 -821
  44. package/types/contracts/presets/LSP8Mintable.ts +0 -797
  45. package/types/contracts/presets/LSP8MintableInit.ts +0 -860
@@ -2,48 +2,75 @@
2
2
  pragma solidity ^0.8.12;
3
3
 
4
4
  // interfaces
5
- import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
6
-
7
- // modules
8
- import {ERC725YCore} from "@erc725/smart-contracts/contracts/ERC725YCore.sol";
9
5
  import {
10
- LSP8IdentifiableDigitalAssetCore
11
- } from "./LSP8IdentifiableDigitalAssetCore.sol";
6
+ ILSP1UniversalReceiver as ILSP1
7
+ } from "@lukso/lsp1-contracts/contracts/ILSP1UniversalReceiver.sol";
12
8
  import {
13
- LSP4DigitalAssetMetadata
14
- } from "@lukso/lsp4-contracts/contracts/LSP4DigitalAssetMetadata.sol";
9
+ ILSP8IdentifiableDigitalAsset
10
+ } from "./ILSP8IdentifiableDigitalAsset.sol";
15
11
 
12
+ // modules
16
13
  import {
17
- LSP4DigitalAssetMetadataCore
18
- } from "@lukso/lsp4-contracts/contracts/LSP4DigitalAssetMetadataCore.sol";
14
+ LSP4DigitalAssetMetadata,
15
+ ERC725Y
16
+ } from "@lukso/lsp4-contracts/contracts/LSP4DigitalAssetMetadata.sol";
19
17
 
20
18
  import {
21
19
  LSP17Extendable
22
20
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Extendable.sol";
23
21
 
24
22
  // libraries
23
+ import {
24
+ EnumerableSet
25
+ } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
26
+ import {
27
+ ERC165Checker
28
+ } from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
29
+ import {LSP1Utils} from "@lukso/lsp1-contracts/contracts/LSP1Utils.sol";
25
30
  import {LSP2Utils} from "@lukso/lsp2-contracts/contracts/LSP2Utils.sol";
26
31
 
27
32
  // constants
28
- import {_INTERFACEID_LSP8, _LSP8_TOKENID_FORMAT_KEY} from "./LSP8Constants.sol";
29
-
30
- // errors
31
33
  import {
32
- LSP8TokenContractCannotHoldValue,
33
- LSP8TokenIdFormatNotEditable
34
- } from "./LSP8Errors.sol";
35
-
34
+ _INTERFACEID_LSP1
35
+ } from "@lukso/lsp1-contracts/contracts/LSP1Constants.sol";
36
36
  import {
37
37
  _LSP17_EXTENSION_PREFIX
38
38
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Constants.sol";
39
+ import {
40
+ _INTERFACEID_LSP8,
41
+ _LSP8_TOKENID_FORMAT_KEY,
42
+ _TYPEID_LSP8_TOKENOPERATOR,
43
+ _TYPEID_LSP8_TOKENSSENDER,
44
+ _TYPEID_LSP8_TOKENSRECIPIENT
45
+ } from "./LSP8Constants.sol";
39
46
 
40
47
  // errors
41
-
42
48
  import {
43
49
  NoExtensionFoundForFunctionSelector,
44
50
  InvalidFunctionSelector,
45
51
  InvalidExtensionAddress
46
52
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Errors.sol";
53
+ import {
54
+ LSP8TokenContractCannotHoldValue,
55
+ LSP8TokenIdFormatNotEditable,
56
+ LSP8NonExistentTokenId,
57
+ LSP8NotTokenOwner,
58
+ LSP8CannotUseAddressZeroAsOperator,
59
+ LSP8TokenOwnerCannotBeOperator,
60
+ LSP8OperatorAlreadyAuthorized,
61
+ LSP8NotTokenOperator,
62
+ LSP8InvalidTransferBatch,
63
+ LSP8NonExistingOperator,
64
+ LSP8CannotSendToAddressZero,
65
+ LSP8TokenIdAlreadyMinted,
66
+ LSP8NotifyTokenReceiverContractMissingLSP1Interface,
67
+ LSP8NotifyTokenReceiverIsEOA,
68
+ LSP8TokenIdsDataLengthMismatch,
69
+ LSP8TokenIdsDataEmptyArray,
70
+ LSP8BatchCallFailed,
71
+ LSP8TokenOwnerChanged,
72
+ LSP8RevokeOperatorNotAuthorized
73
+ } from "./LSP8Errors.sol";
47
74
 
48
75
  /**
49
76
  * @title Implementation of a LSP8 Identifiable Digital Asset, a contract that represents a non-fungible token.
@@ -57,10 +84,26 @@ import {
57
84
  * For a generic mechanism, see {LSP7Mintable}.
58
85
  */
59
86
  abstract contract LSP8IdentifiableDigitalAsset is
87
+ ILSP8IdentifiableDigitalAsset,
60
88
  LSP4DigitalAssetMetadata,
61
- LSP8IdentifiableDigitalAssetCore,
62
89
  LSP17Extendable
63
90
  {
91
+ using EnumerableSet for EnumerableSet.AddressSet;
92
+ using EnumerableSet for EnumerableSet.Bytes32Set;
93
+
94
+ // --- Storage
95
+
96
+ uint256 internal _existingTokens;
97
+
98
+ // Mapping from `tokenId` to `tokenOwner`
99
+ mapping(bytes32 => address) internal _tokenOwners;
100
+
101
+ // Mapping `tokenOwner` to owned tokenIds
102
+ mapping(address => EnumerableSet.Bytes32Set) internal _ownedTokens;
103
+
104
+ // Mapping a `tokenId` to its authorized operator addresses.
105
+ mapping(bytes32 => EnumerableSet.AddressSet) internal _operators;
106
+
64
107
  /**
65
108
  * @notice Deploying a LSP8IdentifiableDigitalAsset with name `name_`, symbol `symbol_`, owned by address `newOwner_`
66
109
  * with tokenId format `lsp8TokenIdFormat_`.
@@ -90,7 +133,7 @@ abstract contract LSP8IdentifiableDigitalAsset is
90
133
  );
91
134
  }
92
135
 
93
- // fallback function
136
+ // fallback functions
94
137
 
95
138
  /**
96
139
  * @notice The `fallback` function was called with the following amount of native tokens: `msg.value`; and the following calldata: `callData`.
@@ -125,6 +168,7 @@ abstract contract LSP8IdentifiableDigitalAsset is
125
168
  * @dev Reverts whenever someone tries to send native tokens to a LSP8 contract.
126
169
  * @notice LSP8 contract cannot receive native tokens.
127
170
  */
171
+ // solhint-disable-next-line no-complex-fallback
128
172
  receive() external payable virtual {
129
173
  // revert on empty calls with no value
130
174
  if (msg.value == 0) {
@@ -193,9 +237,7 @@ abstract contract LSP8IdentifiableDigitalAsset is
193
237
  );
194
238
 
195
239
  // Check if there is an extension stored under the generated data key
196
- bytes memory extensionAddress = ERC725YCore._getData(
197
- mappedExtensionDataKey
198
- );
240
+ bytes memory extensionAddress = _getData(mappedExtensionDataKey);
199
241
  if (extensionAddress.length != 20 && extensionAddress.length != 0)
200
242
  revert InvalidExtensionAddress(extensionAddress);
201
243
 
@@ -203,17 +245,11 @@ abstract contract LSP8IdentifiableDigitalAsset is
203
245
  }
204
246
 
205
247
  /**
206
- * @inheritdoc IERC165
248
+ * @inheritdoc LSP17Extendable
207
249
  */
208
250
  function supportsInterface(
209
251
  bytes4 interfaceId
210
- )
211
- public
212
- view
213
- virtual
214
- override(IERC165, ERC725YCore, LSP17Extendable)
215
- returns (bool)
216
- {
252
+ ) public view virtual override(ERC725Y, LSP17Extendable) returns (bool) {
217
253
  return
218
254
  interfaceId == _INTERFACEID_LSP8 ||
219
255
  super.supportsInterface(interfaceId) ||
@@ -228,14 +264,738 @@ abstract contract LSP8IdentifiableDigitalAsset is
228
264
  function _setData(
229
265
  bytes32 dataKey,
230
266
  bytes memory dataValue
231
- )
232
- internal
233
- virtual
234
- override(LSP4DigitalAssetMetadata, LSP4DigitalAssetMetadataCore)
235
- {
267
+ ) internal virtual override {
236
268
  if (dataKey == _LSP8_TOKENID_FORMAT_KEY) {
237
269
  revert LSP8TokenIdFormatNotEditable();
238
270
  }
239
271
  LSP4DigitalAssetMetadata._setData(dataKey, dataValue);
240
272
  }
273
+
274
+ // --- Token queries
275
+
276
+ /**
277
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
278
+ */
279
+ function totalSupply() public view virtual override returns (uint256) {
280
+ return _existingTokens;
281
+ }
282
+
283
+ // --- Token owner queries
284
+
285
+ /**
286
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
287
+ */
288
+ function balanceOf(
289
+ address tokenOwner
290
+ ) public view virtual override returns (uint256) {
291
+ return _ownedTokens[tokenOwner].length();
292
+ }
293
+
294
+ /**
295
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
296
+ */
297
+ function tokenOwnerOf(
298
+ bytes32 tokenId
299
+ ) public view virtual override returns (address) {
300
+ address tokenOwner = _tokenOwners[tokenId];
301
+
302
+ if (tokenOwner == address(0)) {
303
+ revert LSP8NonExistentTokenId(tokenId);
304
+ }
305
+
306
+ return tokenOwner;
307
+ }
308
+
309
+ /**
310
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
311
+ */
312
+ function tokenIdsOf(
313
+ address tokenOwner
314
+ ) public view virtual override returns (bytes32[] memory) {
315
+ return _ownedTokens[tokenOwner].values();
316
+ }
317
+
318
+ // --- TokenId Metadata functionality
319
+
320
+ /**
321
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
322
+ */
323
+ function getDataForTokenId(
324
+ bytes32 tokenId,
325
+ bytes32 dataKey
326
+ ) public view virtual override returns (bytes memory dataValue) {
327
+ return _getDataForTokenId(tokenId, dataKey);
328
+ }
329
+
330
+ /**
331
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
332
+ */
333
+ function getDataBatchForTokenIds(
334
+ bytes32[] memory tokenIds,
335
+ bytes32[] memory dataKeys
336
+ ) public view virtual override returns (bytes[] memory dataValues) {
337
+ if (tokenIds.length != dataKeys.length) {
338
+ revert LSP8TokenIdsDataLengthMismatch();
339
+ }
340
+
341
+ dataValues = new bytes[](tokenIds.length);
342
+
343
+ for (uint256 i; i < tokenIds.length; ) {
344
+ dataValues[i] = _getDataForTokenId(tokenIds[i], dataKeys[i]);
345
+
346
+ // Increment the iterator in unchecked block to save gas
347
+ unchecked {
348
+ ++i;
349
+ }
350
+ }
351
+
352
+ return dataValues;
353
+ }
354
+
355
+ /**
356
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
357
+ */
358
+ function setDataForTokenId(
359
+ bytes32 tokenId,
360
+ bytes32 dataKey,
361
+ bytes memory dataValue
362
+ ) public virtual override onlyOwner {
363
+ _setDataForTokenId(tokenId, dataKey, dataValue);
364
+ }
365
+
366
+ /**
367
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
368
+ */
369
+ function setDataBatchForTokenIds(
370
+ bytes32[] memory tokenIds,
371
+ bytes32[] memory dataKeys,
372
+ bytes[] memory dataValues
373
+ ) public virtual override onlyOwner {
374
+ if (
375
+ tokenIds.length != dataKeys.length ||
376
+ dataKeys.length != dataValues.length
377
+ ) {
378
+ revert LSP8TokenIdsDataLengthMismatch();
379
+ }
380
+
381
+ if (tokenIds.length == 0) {
382
+ revert LSP8TokenIdsDataEmptyArray();
383
+ }
384
+
385
+ for (uint256 i; i < tokenIds.length; ) {
386
+ _setDataForTokenId(tokenIds[i], dataKeys[i], dataValues[i]);
387
+
388
+ // Increment the iterator in unchecked block to save gas
389
+ unchecked {
390
+ ++i;
391
+ }
392
+ }
393
+ }
394
+
395
+ // --- General functionality
396
+
397
+ /**
398
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
399
+ *
400
+ * @custom:info It's not possible to send value along the functions call due to the use of `delegatecall`.
401
+ */
402
+ function batchCalls(
403
+ bytes[] calldata data
404
+ ) public virtual override returns (bytes[] memory results) {
405
+ results = new bytes[](data.length);
406
+ for (uint256 i; i < data.length; ) {
407
+ (bool success, bytes memory result) = address(this).delegatecall(
408
+ data[i]
409
+ );
410
+
411
+ if (!success) {
412
+ // Look for revert reason and bubble it up if present
413
+ if (result.length != 0) {
414
+ // The easiest way to bubble the revert reason is using memory via assembly
415
+ // solhint-disable no-inline-assembly
416
+ /// @solidity memory-safe-assembly
417
+ assembly {
418
+ let returndata_size := mload(result)
419
+ revert(add(32, result), returndata_size)
420
+ }
421
+ } else {
422
+ revert LSP8BatchCallFailed({callIndex: i});
423
+ }
424
+ }
425
+
426
+ results[i] = result;
427
+
428
+ unchecked {
429
+ ++i;
430
+ }
431
+ }
432
+ }
433
+
434
+ // --- Operator functionality
435
+
436
+ /**
437
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
438
+ */
439
+ function authorizeOperator(
440
+ address operator,
441
+ bytes32 tokenId,
442
+ bytes memory operatorNotificationData
443
+ ) public virtual override {
444
+ address tokenOwner = tokenOwnerOf(tokenId);
445
+
446
+ if (tokenOwner != msg.sender) {
447
+ revert LSP8NotTokenOwner(tokenOwner, tokenId, msg.sender);
448
+ }
449
+
450
+ if (operator == address(0)) {
451
+ revert LSP8CannotUseAddressZeroAsOperator();
452
+ }
453
+
454
+ if (tokenOwner == operator) {
455
+ revert LSP8TokenOwnerCannotBeOperator();
456
+ }
457
+
458
+ bool isAdded = _operators[tokenId].add(operator);
459
+ if (!isAdded) revert LSP8OperatorAlreadyAuthorized(operator, tokenId);
460
+
461
+ emit OperatorAuthorizationChanged(
462
+ operator,
463
+ tokenOwner,
464
+ tokenId,
465
+ operatorNotificationData
466
+ );
467
+
468
+ bytes memory lsp1Data = abi.encode(
469
+ msg.sender,
470
+ tokenId,
471
+ true, // authorized
472
+ operatorNotificationData
473
+ );
474
+
475
+ _notifyTokenOperator(operator, lsp1Data);
476
+ }
477
+
478
+ /**
479
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
480
+ */
481
+ function revokeOperator(
482
+ address operator,
483
+ bytes32 tokenId,
484
+ bool notify,
485
+ bytes memory operatorNotificationData
486
+ ) public virtual override {
487
+ address tokenOwner = tokenOwnerOf(tokenId);
488
+
489
+ if (msg.sender != tokenOwner) {
490
+ if (operator != msg.sender) {
491
+ revert LSP8RevokeOperatorNotAuthorized(
492
+ msg.sender,
493
+ tokenOwner,
494
+ tokenId
495
+ );
496
+ }
497
+ }
498
+
499
+ if (operator == address(0)) {
500
+ revert LSP8CannotUseAddressZeroAsOperator();
501
+ }
502
+
503
+ if (tokenOwner == operator) {
504
+ revert LSP8TokenOwnerCannotBeOperator();
505
+ }
506
+
507
+ _revokeOperator(
508
+ operator,
509
+ tokenOwner,
510
+ tokenId,
511
+ notify,
512
+ operatorNotificationData
513
+ );
514
+
515
+ if (notify) {
516
+ bytes memory lsp1Data = abi.encode(
517
+ tokenOwner,
518
+ tokenId,
519
+ false, // unauthorized
520
+ operatorNotificationData
521
+ );
522
+
523
+ _notifyTokenOperator(operator, lsp1Data);
524
+ }
525
+ }
526
+
527
+ /**
528
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
529
+ */
530
+ function isOperatorFor(
531
+ address operator,
532
+ bytes32 tokenId
533
+ ) public view virtual override returns (bool) {
534
+ return _isOperatorOrOwner(operator, tokenId);
535
+ }
536
+
537
+ /**
538
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
539
+ */
540
+ function getOperatorsOf(
541
+ bytes32 tokenId
542
+ ) public view virtual override returns (address[] memory) {
543
+ _existsOrError(tokenId);
544
+
545
+ return _operators[tokenId].values();
546
+ }
547
+
548
+ /**
549
+ * @dev verifies if the `caller` is operator or owner for the `tokenId`
550
+ * @return true if `caller` is either operator or owner
551
+ */
552
+ function _isOperatorOrOwner(
553
+ address caller,
554
+ bytes32 tokenId
555
+ ) internal view virtual returns (bool) {
556
+ return (caller == tokenOwnerOf(tokenId) ||
557
+ _operators[tokenId].contains(caller));
558
+ }
559
+
560
+ // --- Transfer functionality
561
+
562
+ /**
563
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
564
+ */
565
+ function transfer(
566
+ address from,
567
+ address to,
568
+ bytes32 tokenId,
569
+ bool force,
570
+ bytes memory data
571
+ ) public virtual override {
572
+ if (!_isOperatorOrOwner(msg.sender, tokenId)) {
573
+ revert LSP8NotTokenOperator(tokenId, msg.sender);
574
+ }
575
+
576
+ _transfer(from, to, tokenId, force, data);
577
+ }
578
+
579
+ /**
580
+ * @inheritdoc ILSP8IdentifiableDigitalAsset
581
+ */
582
+ function transferBatch(
583
+ address[] memory from,
584
+ address[] memory to,
585
+ bytes32[] memory tokenId,
586
+ bool[] memory force,
587
+ bytes[] memory data
588
+ ) public virtual override {
589
+ uint256 fromLength = from.length;
590
+ if (
591
+ fromLength != to.length ||
592
+ fromLength != tokenId.length ||
593
+ fromLength != force.length ||
594
+ fromLength != data.length
595
+ ) {
596
+ revert LSP8InvalidTransferBatch();
597
+ }
598
+
599
+ for (uint256 i; i < fromLength; ) {
600
+ transfer(from[i], to[i], tokenId[i], force[i], data[i]);
601
+
602
+ unchecked {
603
+ ++i;
604
+ }
605
+ }
606
+ }
607
+
608
+ /**
609
+ * @dev removes `operator` from the list of operators for the `tokenId`
610
+ */
611
+ function _revokeOperator(
612
+ address operator,
613
+ address tokenOwner,
614
+ bytes32 tokenId,
615
+ bool notified,
616
+ bytes memory operatorNotificationData
617
+ ) internal virtual {
618
+ bool isRemoved = _operators[tokenId].remove(operator);
619
+ if (!isRemoved) revert LSP8NonExistingOperator(operator, tokenId);
620
+
621
+ emit OperatorRevoked(
622
+ operator,
623
+ tokenOwner,
624
+ tokenId,
625
+ notified,
626
+ operatorNotificationData
627
+ );
628
+ }
629
+
630
+ /**
631
+ * @dev revoke all the current operators for a specific `tokenId` token which belongs to `tokenOwner`.
632
+ *
633
+ * @param tokenOwner The address that is the owner of the `tokenId`.
634
+ * @param tokenId The token to remove the associated operators for.
635
+ */
636
+ function _clearOperators(
637
+ address tokenOwner,
638
+ bytes32 tokenId
639
+ ) internal virtual {
640
+ // here is a good example of why having multiple operators will be expensive.. we
641
+ // need to clear them on token transfer
642
+ //
643
+ // NOTE: this may cause a tx to fail if there is too many operators to clear, in which case
644
+ // the tokenOwner needs to call `revokeOperator` until there is less operators to clear and
645
+ // the desired `transfer` or `burn` call can succeed.
646
+ EnumerableSet.AddressSet storage operatorsForTokenId = _operators[
647
+ tokenId
648
+ ];
649
+
650
+ uint256 operatorListLength = operatorsForTokenId.length();
651
+ address operator;
652
+ for (uint256 i; i < operatorListLength; ) {
653
+ // we are emptying the list, always remove from index 0
654
+ operator = operatorsForTokenId.at(0);
655
+ _revokeOperator(operator, tokenOwner, tokenId, false, "");
656
+
657
+ unchecked {
658
+ ++i;
659
+ }
660
+ }
661
+ }
662
+
663
+ /**
664
+ * @dev Returns whether `tokenId` exists.
665
+ *
666
+ * Tokens start existing when they are minted ({_mint}), and stop existing when they are burned ({_burn}).
667
+ */
668
+ function _exists(bytes32 tokenId) internal view virtual returns (bool) {
669
+ return _tokenOwners[tokenId] != address(0);
670
+ }
671
+
672
+ /**
673
+ * @dev When `tokenId` does not exist then revert with an error.
674
+ */
675
+ function _existsOrError(bytes32 tokenId) internal view virtual {
676
+ if (!_exists(tokenId)) {
677
+ revert LSP8NonExistentTokenId(tokenId);
678
+ }
679
+ }
680
+
681
+ /**
682
+ * @dev Create `tokenId` by minting it and transfers it to `to`.
683
+ *
684
+ * @custom:info Any logic in the:
685
+ * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
686
+ * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the recipient via LSP1**.
687
+ *
688
+ * @param to The address that will receive the minted `tokenId`.
689
+ * @param tokenId The token ID to create (= mint).
690
+ * @param force When set to `true`, `to` may be any address. When set to `false`, `to` must be a contract that supports the LSP1 standard.
691
+ * @param data Any additional data the caller wants included in the emitted event, and sent in the hook of the `to` address.
692
+ *
693
+ * @custom:requirements
694
+ * - `tokenId` must not exist and not have been already minted.
695
+ * - `to` cannot be the zero address.
696
+
697
+ * @custom:events {Transfer} event with `address(0)` as `from` address.
698
+ */
699
+ function _mint(
700
+ address to,
701
+ bytes32 tokenId,
702
+ bool force,
703
+ bytes memory data
704
+ ) internal virtual {
705
+ if (to == address(0)) {
706
+ revert LSP8CannotSendToAddressZero();
707
+ }
708
+
709
+ // Check that `tokenId` is not already minted
710
+ if (_exists(tokenId)) {
711
+ revert LSP8TokenIdAlreadyMinted(tokenId);
712
+ }
713
+
714
+ _beforeTokenTransfer(address(0), to, tokenId, force, data);
715
+
716
+ // Check that `tokenId` was not minted inside the `_beforeTokenTransfer` hook
717
+ if (_exists(tokenId)) {
718
+ revert LSP8TokenIdAlreadyMinted(tokenId);
719
+ }
720
+
721
+ // token being minted
722
+ ++_existingTokens;
723
+
724
+ _ownedTokens[to].add(tokenId);
725
+ _tokenOwners[tokenId] = to;
726
+
727
+ emit Transfer(msg.sender, address(0), to, tokenId, force, data);
728
+
729
+ _afterTokenTransfer(address(0), to, tokenId, force, data);
730
+
731
+ bytes memory lsp1Data = abi.encode(
732
+ msg.sender,
733
+ address(0),
734
+ to,
735
+ tokenId,
736
+ data
737
+ );
738
+ _notifyTokenReceiver(to, force, lsp1Data);
739
+ }
740
+
741
+ /**
742
+ * @dev Burn a specific `tokenId`, removing the `tokenId` from the {tokenIdsOf} the caller and decreasing its {balanceOf} by -1.
743
+ * This will also clear all the operators allowed to transfer the `tokenId`.
744
+ *
745
+ * The owner of the `tokenId` will be notified about the `tokenId` being transferred through its LSP1 {universalReceiver}
746
+ * function, if it is a contract that supports the LSP1 interface. Its {universalReceiver} function will receive
747
+ * all the parameters in the calldata packed encoded.
748
+ *
749
+ * @custom:info Any logic in the:
750
+ * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
751
+ * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the sender via LSP1**.
752
+ *
753
+ * @param tokenId The token to burn.
754
+ * @param data Any additional data the caller wants included in the emitted event, and sent in the LSP1 hook on the token owner's address.
755
+ *
756
+ * @custom:hint In dApps, you can know which addresses are burning tokens by listening for the `Transfer` event and filter with the zero address as `to`.
757
+ *
758
+ * @custom:requirements
759
+ * - `tokenId` must exist.
760
+ *
761
+ * @custom:events {Transfer} event with `address(0)` as the `to` address.
762
+ */
763
+ function _burn(bytes32 tokenId, bytes memory data) internal virtual {
764
+ address tokenOwner = tokenOwnerOf(tokenId);
765
+
766
+ _beforeTokenTransfer(tokenOwner, address(0), tokenId, false, data);
767
+
768
+ // Re-fetch and update `tokenOwner` in case `tokenId`
769
+ // was transferred inside the `_beforeTokenTransfer` hook
770
+ tokenOwner = tokenOwnerOf(tokenId);
771
+
772
+ // token being burned
773
+ --_existingTokens;
774
+
775
+ _clearOperators(tokenOwner, tokenId);
776
+
777
+ _ownedTokens[tokenOwner].remove(tokenId);
778
+ delete _tokenOwners[tokenId];
779
+
780
+ emit Transfer(msg.sender, tokenOwner, address(0), tokenId, false, data);
781
+
782
+ _afterTokenTransfer(tokenOwner, address(0), tokenId, false, data);
783
+
784
+ bytes memory lsp1Data = abi.encode(
785
+ msg.sender,
786
+ tokenOwner,
787
+ address(0),
788
+ tokenId,
789
+ data
790
+ );
791
+
792
+ _notifyTokenSender(tokenOwner, lsp1Data);
793
+ }
794
+
795
+ /**
796
+ * @dev Change the owner of the `tokenId` from `from` to `to`.
797
+ *
798
+ * Both the sender and recipient will be notified of the `tokenId` being transferred through their LSP1 {universalReceiver}
799
+ * function, if they are contracts that support the LSP1 interface. Their `universalReceiver` function will receive
800
+ * all the parameters in the calldata packed encoded.
801
+ *
802
+ * @custom:info Any logic in the:
803
+ * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
804
+ * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the sender/recipient via LSP1**.
805
+ *
806
+ * @param from The sender address.
807
+ * @param to The recipient address.
808
+ * @param tokenId The token to transfer.
809
+ * @param force When set to `true`, `to` may be any address. When set to `false`, `to` must be a contract that supports the LSP1 standard.
810
+ * @param data Additional data the caller wants included in the emitted event, and sent in the hooks to `from` and `to` addresses.
811
+ *
812
+ * @custom:requirements
813
+ * - `to` cannot be the zero address.
814
+ * - `tokenId` token must be owned by `from`.
815
+ *
816
+ * @custom:events {Transfer} event.
817
+ *
818
+ * @custom:danger This internal function does not check if the sender is authorized or not to operate on the `tokenId`.
819
+ */
820
+ function _transfer(
821
+ address from,
822
+ address to,
823
+ bytes32 tokenId,
824
+ bool force,
825
+ bytes memory data
826
+ ) internal virtual {
827
+ address tokenOwner = tokenOwnerOf(tokenId);
828
+ if (tokenOwner != from) {
829
+ revert LSP8NotTokenOwner(tokenOwner, tokenId, from);
830
+ }
831
+
832
+ if (to == address(0)) {
833
+ revert LSP8CannotSendToAddressZero();
834
+ }
835
+
836
+ _beforeTokenTransfer(from, to, tokenId, force, data);
837
+
838
+ // Check that `tokenId`'s owner was not changed inside the `_beforeTokenTransfer` hook
839
+ address currentTokenOwner = tokenOwnerOf(tokenId);
840
+ if (tokenOwner != currentTokenOwner) {
841
+ revert LSP8TokenOwnerChanged(
842
+ tokenId,
843
+ tokenOwner,
844
+ currentTokenOwner
845
+ );
846
+ }
847
+
848
+ _clearOperators(from, tokenId);
849
+
850
+ _ownedTokens[from].remove(tokenId);
851
+ _ownedTokens[to].add(tokenId);
852
+ _tokenOwners[tokenId] = to;
853
+
854
+ emit Transfer(msg.sender, from, to, tokenId, force, data);
855
+
856
+ _afterTokenTransfer(from, to, tokenId, force, data);
857
+
858
+ bytes memory lsp1Data = abi.encode(msg.sender, from, to, tokenId, data);
859
+
860
+ _notifyTokenSender(from, lsp1Data);
861
+ _notifyTokenReceiver(to, force, lsp1Data);
862
+ }
863
+
864
+ /**
865
+ * @dev Sets data for a specific `tokenId` and `dataKey` in the ERC725Y storage
866
+ * The ERC725Y data key is the hash of the `tokenId` and `dataKey` concatenated
867
+ * @param tokenId The unique identifier for a token.
868
+ * @param dataKey The key for the data to set.
869
+ * @param dataValue The value to set for the given data key.
870
+ * @custom:events {TokenIdDataChanged} event.
871
+ */
872
+ function _setDataForTokenId(
873
+ bytes32 tokenId,
874
+ bytes32 dataKey,
875
+ bytes memory dataValue
876
+ ) internal virtual {
877
+ _store[keccak256(bytes.concat(tokenId, dataKey))] = dataValue;
878
+ emit TokenIdDataChanged(tokenId, dataKey, dataValue);
879
+ }
880
+
881
+ /**
882
+ * @dev Retrieves data for a specific `tokenId` and `dataKey` from the ERC725Y storage
883
+ * The ERC725Y data key is the hash of the `tokenId` and `dataKey` concatenated
884
+ * @param tokenId The unique identifier for a token.
885
+ * @param dataKey The key for the data to retrieve.
886
+ * @return dataValues The data value associated with the given `tokenId` and `dataKey`.
887
+ */
888
+ function _getDataForTokenId(
889
+ bytes32 tokenId,
890
+ bytes32 dataKey
891
+ ) internal view virtual returns (bytes memory dataValues) {
892
+ return _store[keccak256(bytes.concat(tokenId, dataKey))];
893
+ }
894
+
895
+ /**
896
+ * @dev Hook that is called before any token transfer, including minting and burning.
897
+ * Allows to run custom logic before updating balances and notifying sender/recipient by overriding this function.
898
+ *
899
+ * @param from The sender address
900
+ * @param to The recipient address
901
+ * @param tokenId The tokenId to transfer
902
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
903
+ * @param data The data sent alongside the transfer
904
+ */
905
+ function _beforeTokenTransfer(
906
+ address from,
907
+ address to,
908
+ bytes32 tokenId,
909
+ bool force,
910
+ bytes memory data // solhint-disable-next-line no-empty-blocks
911
+ ) internal virtual {}
912
+
913
+ /**
914
+ * @dev Hook that is called after any token transfer, including minting and burning.
915
+ * Allows to run custom logic after updating balances, but **before notifying sender/recipient via LSP1** by overriding this function.
916
+ *
917
+ * @param from The sender address
918
+ * @param to The recipient address
919
+ * @param tokenId The tokenId to transfer
920
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
921
+ * @param data The data sent alongside the transfer
922
+ */
923
+ function _afterTokenTransfer(
924
+ address from,
925
+ address to,
926
+ bytes32 tokenId,
927
+ bool force,
928
+ bytes memory data // solhint-disable-next-line no-empty-blocks
929
+ ) internal virtual {}
930
+
931
+ /**
932
+ * @dev Attempt to notify the operator `operator` about the `tokenId` being authorized.
933
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP8_TOKENOPERATOR` as typeId, if `operator` is a contract that supports the LSP1 interface.
934
+ * If `operator` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
935
+
936
+ * @param operator The address to call the {universalReceiver} function on.
937
+ * @param lsp1Data the data to be sent to the `operator` address in the `universalReceiver` call.
938
+ */
939
+ function _notifyTokenOperator(
940
+ address operator,
941
+ bytes memory lsp1Data
942
+ ) internal virtual {
943
+ LSP1Utils.notifyUniversalReceiver(
944
+ operator,
945
+ _TYPEID_LSP8_TOKENOPERATOR,
946
+ lsp1Data
947
+ );
948
+ }
949
+
950
+ /**
951
+ * @dev Attempt to notify the token sender `from` about the `tokenId` being transferred.
952
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP8_TOKENSSENDER` as typeId, if `from` is a contract that supports the LSP1 interface.
953
+ * If `from` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
954
+
955
+ * @param from The address to call the {universalReceiver} function on.
956
+ * @param lsp1Data the data to be sent to the `from` address in the `universalReceiver` call.
957
+ */
958
+ function _notifyTokenSender(
959
+ address from,
960
+ bytes memory lsp1Data
961
+ ) internal virtual {
962
+ LSP1Utils.notifyUniversalReceiver(
963
+ from,
964
+ _TYPEID_LSP8_TOKENSSENDER,
965
+ lsp1Data
966
+ );
967
+ }
968
+
969
+ /**
970
+ * @dev Attempt to notify the token receiver `to` about the `tokenId` being received.
971
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP8_TOKENSRECIPIENT` as typeId, if `to` is a contract that supports the LSP1 interface.
972
+ *
973
+ * If `to` is is an EOA or a contract that does not support the LSP1 interface, the behaviour will depend on the `force` boolean flag.
974
+ * - if `force` is set to `true`, nothing will happen and no notification will be sent.
975
+ * - if `force` is set to `false, the transaction will revert.
976
+ *
977
+ * @param to The address to call the {universalReceiver} function on.
978
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
979
+ * @param lsp1Data The data to be sent to the `to` address in the `universalReceiver(...)` call.
980
+ */
981
+ function _notifyTokenReceiver(
982
+ address to,
983
+ bool force,
984
+ bytes memory lsp1Data
985
+ ) internal virtual {
986
+ if (
987
+ ERC165Checker.supportsERC165InterfaceUnchecked(
988
+ to,
989
+ _INTERFACEID_LSP1
990
+ )
991
+ ) {
992
+ ILSP1(to).universalReceiver(_TYPEID_LSP8_TOKENSRECIPIENT, lsp1Data);
993
+ } else if (!force) {
994
+ if (to.code.length != 0) {
995
+ revert LSP8NotifyTokenReceiverContractMissingLSP1Interface(to);
996
+ } else {
997
+ revert LSP8NotifyTokenReceiverIsEOA(to);
998
+ }
999
+ }
1000
+ }
241
1001
  }