@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
@@ -1,809 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.12;
3
-
4
- // interfaces
5
- import {
6
- ILSP1UniversalReceiver as ILSP1
7
- } from "@lukso/lsp1-contracts/contracts/ILSP1UniversalReceiver.sol";
8
- import {
9
- ILSP8IdentifiableDigitalAsset
10
- } from "./ILSP8IdentifiableDigitalAsset.sol";
11
-
12
- // modules
13
-
14
- import {
15
- LSP4DigitalAssetMetadataCore
16
- } from "@lukso/lsp4-contracts/contracts/LSP4DigitalAssetMetadataCore.sol";
17
-
18
- // libraries
19
- import {
20
- EnumerableSet
21
- } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
22
- import {
23
- ERC165Checker
24
- } from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
25
- import {LSP1Utils} from "@lukso/lsp1-contracts/contracts/LSP1Utils.sol";
26
-
27
- // errors
28
- import {
29
- LSP8NonExistentTokenId,
30
- LSP8NotTokenOwner,
31
- LSP8CannotUseAddressZeroAsOperator,
32
- LSP8TokenOwnerCannotBeOperator,
33
- LSP8OperatorAlreadyAuthorized,
34
- LSP8NotTokenOperator,
35
- LSP8InvalidTransferBatch,
36
- LSP8NonExistingOperator,
37
- LSP8CannotSendToAddressZero,
38
- LSP8TokenIdAlreadyMinted,
39
- LSP8NotifyTokenReceiverContractMissingLSP1Interface,
40
- LSP8NotifyTokenReceiverIsEOA,
41
- LSP8TokenIdsDataLengthMismatch,
42
- LSP8TokenIdsDataEmptyArray,
43
- LSP8BatchCallFailed,
44
- LSP8TokenOwnerChanged,
45
- LSP8RevokeOperatorNotAuthorized
46
- } from "./LSP8Errors.sol";
47
-
48
- // constants
49
- import {
50
- _INTERFACEID_LSP1
51
- } from "@lukso/lsp1-contracts/contracts/LSP1Constants.sol";
52
- import {
53
- _TYPEID_LSP8_TOKENOPERATOR,
54
- _TYPEID_LSP8_TOKENSSENDER,
55
- _TYPEID_LSP8_TOKENSRECIPIENT
56
- } from "./LSP8Constants.sol";
57
-
58
- /**
59
- * @title LSP8IdentifiableDigitalAsset contract
60
- * @author Matthew Stevens
61
- * @dev Core Implementation of a LSP8 compliant contract.
62
- */
63
- abstract contract LSP8IdentifiableDigitalAssetCore is
64
- LSP4DigitalAssetMetadataCore,
65
- ILSP8IdentifiableDigitalAsset
66
- {
67
- using EnumerableSet for EnumerableSet.AddressSet;
68
- using EnumerableSet for EnumerableSet.Bytes32Set;
69
-
70
- // --- Storage
71
-
72
- uint256 internal _existingTokens;
73
-
74
- // Mapping from `tokenId` to `tokenOwner`
75
- mapping(bytes32 => address) internal _tokenOwners;
76
-
77
- // Mapping `tokenOwner` to owned tokenIds
78
- mapping(address => EnumerableSet.Bytes32Set) internal _ownedTokens;
79
-
80
- // Mapping a `tokenId` to its authorized operator addresses.
81
- mapping(bytes32 => EnumerableSet.AddressSet) internal _operators;
82
-
83
- // Mapping from `tokenId` to `dataKey` to `dataValue`
84
- mapping(bytes32 => mapping(bytes32 => bytes)) internal _tokenIdData;
85
-
86
- // --- Token queries
87
-
88
- /**
89
- * @inheritdoc ILSP8IdentifiableDigitalAsset
90
- */
91
- function totalSupply() public view virtual override returns (uint256) {
92
- return _existingTokens;
93
- }
94
-
95
- // --- Token owner queries
96
-
97
- /**
98
- * @inheritdoc ILSP8IdentifiableDigitalAsset
99
- */
100
- function balanceOf(
101
- address tokenOwner
102
- ) public view virtual override returns (uint256) {
103
- return _ownedTokens[tokenOwner].length();
104
- }
105
-
106
- /**
107
- * @inheritdoc ILSP8IdentifiableDigitalAsset
108
- */
109
- function tokenOwnerOf(
110
- bytes32 tokenId
111
- ) public view virtual override returns (address) {
112
- address tokenOwner = _tokenOwners[tokenId];
113
-
114
- if (tokenOwner == address(0)) {
115
- revert LSP8NonExistentTokenId(tokenId);
116
- }
117
-
118
- return tokenOwner;
119
- }
120
-
121
- /**
122
- * @inheritdoc ILSP8IdentifiableDigitalAsset
123
- */
124
- function tokenIdsOf(
125
- address tokenOwner
126
- ) public view virtual override returns (bytes32[] memory) {
127
- return _ownedTokens[tokenOwner].values();
128
- }
129
-
130
- // --- TokenId Metadata functionality
131
-
132
- /**
133
- * @inheritdoc ILSP8IdentifiableDigitalAsset
134
- */
135
- function getDataForTokenId(
136
- bytes32 tokenId,
137
- bytes32 dataKey
138
- ) public view virtual override returns (bytes memory dataValue) {
139
- return _getDataForTokenId(tokenId, dataKey);
140
- }
141
-
142
- /**
143
- * @inheritdoc ILSP8IdentifiableDigitalAsset
144
- */
145
- function getDataBatchForTokenIds(
146
- bytes32[] memory tokenIds,
147
- bytes32[] memory dataKeys
148
- ) public view virtual override returns (bytes[] memory dataValues) {
149
- if (tokenIds.length != dataKeys.length) {
150
- revert LSP8TokenIdsDataLengthMismatch();
151
- }
152
-
153
- dataValues = new bytes[](tokenIds.length);
154
-
155
- for (uint256 i; i < tokenIds.length; ) {
156
- dataValues[i] = _getDataForTokenId(tokenIds[i], dataKeys[i]);
157
-
158
- // Increment the iterator in unchecked block to save gas
159
- unchecked {
160
- ++i;
161
- }
162
- }
163
-
164
- return dataValues;
165
- }
166
-
167
- /**
168
- * @inheritdoc ILSP8IdentifiableDigitalAsset
169
- */
170
- function setDataForTokenId(
171
- bytes32 tokenId,
172
- bytes32 dataKey,
173
- bytes memory dataValue
174
- ) public virtual override onlyOwner {
175
- _setDataForTokenId(tokenId, dataKey, dataValue);
176
- }
177
-
178
- /**
179
- * @inheritdoc ILSP8IdentifiableDigitalAsset
180
- */
181
- function setDataBatchForTokenIds(
182
- bytes32[] memory tokenIds,
183
- bytes32[] memory dataKeys,
184
- bytes[] memory dataValues
185
- ) public virtual override onlyOwner {
186
- if (
187
- tokenIds.length != dataKeys.length ||
188
- dataKeys.length != dataValues.length
189
- ) {
190
- revert LSP8TokenIdsDataLengthMismatch();
191
- }
192
-
193
- if (tokenIds.length == 0) {
194
- revert LSP8TokenIdsDataEmptyArray();
195
- }
196
-
197
- for (uint256 i; i < tokenIds.length; ) {
198
- _setDataForTokenId(tokenIds[i], dataKeys[i], dataValues[i]);
199
-
200
- // Increment the iterator in unchecked block to save gas
201
- unchecked {
202
- ++i;
203
- }
204
- }
205
- }
206
-
207
- // --- General functionality
208
-
209
- /**
210
- * @inheritdoc ILSP8IdentifiableDigitalAsset
211
- *
212
- * @custom:info It's not possible to send value along the functions call due to the use of `delegatecall`.
213
- */
214
- function batchCalls(
215
- bytes[] calldata data
216
- ) public virtual override returns (bytes[] memory results) {
217
- results = new bytes[](data.length);
218
- for (uint256 i; i < data.length; ) {
219
- (bool success, bytes memory result) = address(this).delegatecall(
220
- data[i]
221
- );
222
-
223
- if (!success) {
224
- // Look for revert reason and bubble it up if present
225
- if (result.length != 0) {
226
- // The easiest way to bubble the revert reason is using memory via assembly
227
- // solhint-disable no-inline-assembly
228
- /// @solidity memory-safe-assembly
229
- assembly {
230
- let returndata_size := mload(result)
231
- revert(add(32, result), returndata_size)
232
- }
233
- } else {
234
- revert LSP8BatchCallFailed({callIndex: i});
235
- }
236
- }
237
-
238
- results[i] = result;
239
-
240
- unchecked {
241
- ++i;
242
- }
243
- }
244
- }
245
-
246
- // --- Operator functionality
247
-
248
- /**
249
- * @inheritdoc ILSP8IdentifiableDigitalAsset
250
- */
251
- function authorizeOperator(
252
- address operator,
253
- bytes32 tokenId,
254
- bytes memory operatorNotificationData
255
- ) public virtual override {
256
- address tokenOwner = tokenOwnerOf(tokenId);
257
-
258
- if (tokenOwner != msg.sender) {
259
- revert LSP8NotTokenOwner(tokenOwner, tokenId, msg.sender);
260
- }
261
-
262
- if (operator == address(0)) {
263
- revert LSP8CannotUseAddressZeroAsOperator();
264
- }
265
-
266
- if (tokenOwner == operator) {
267
- revert LSP8TokenOwnerCannotBeOperator();
268
- }
269
-
270
- bool isAdded = _operators[tokenId].add(operator);
271
- if (!isAdded) revert LSP8OperatorAlreadyAuthorized(operator, tokenId);
272
-
273
- emit OperatorAuthorizationChanged(
274
- operator,
275
- tokenOwner,
276
- tokenId,
277
- operatorNotificationData
278
- );
279
-
280
- bytes memory lsp1Data = abi.encode(
281
- msg.sender,
282
- tokenId,
283
- true, // authorized
284
- operatorNotificationData
285
- );
286
-
287
- _notifyTokenOperator(operator, lsp1Data);
288
- }
289
-
290
- /**
291
- * @inheritdoc ILSP8IdentifiableDigitalAsset
292
- */
293
- function revokeOperator(
294
- address operator,
295
- bytes32 tokenId,
296
- bool notify,
297
- bytes memory operatorNotificationData
298
- ) public virtual override {
299
- address tokenOwner = tokenOwnerOf(tokenId);
300
-
301
- if (msg.sender != tokenOwner) {
302
- if (operator != msg.sender) {
303
- revert LSP8RevokeOperatorNotAuthorized(
304
- msg.sender,
305
- tokenOwner,
306
- tokenId
307
- );
308
- }
309
- }
310
-
311
- if (operator == address(0)) {
312
- revert LSP8CannotUseAddressZeroAsOperator();
313
- }
314
-
315
- if (tokenOwner == operator) {
316
- revert LSP8TokenOwnerCannotBeOperator();
317
- }
318
-
319
- _revokeOperator(
320
- operator,
321
- tokenOwner,
322
- tokenId,
323
- notify,
324
- operatorNotificationData
325
- );
326
-
327
- if (notify) {
328
- bytes memory lsp1Data = abi.encode(
329
- tokenOwner,
330
- tokenId,
331
- false, // unauthorized
332
- operatorNotificationData
333
- );
334
-
335
- _notifyTokenOperator(operator, lsp1Data);
336
- }
337
- }
338
-
339
- /**
340
- * @inheritdoc ILSP8IdentifiableDigitalAsset
341
- */
342
- function isOperatorFor(
343
- address operator,
344
- bytes32 tokenId
345
- ) public view virtual override returns (bool) {
346
- return _isOperatorOrOwner(operator, tokenId);
347
- }
348
-
349
- /**
350
- * @inheritdoc ILSP8IdentifiableDigitalAsset
351
- */
352
- function getOperatorsOf(
353
- bytes32 tokenId
354
- ) public view virtual override returns (address[] memory) {
355
- _existsOrError(tokenId);
356
-
357
- return _operators[tokenId].values();
358
- }
359
-
360
- /**
361
- * @dev verifies if the `caller` is operator or owner for the `tokenId`
362
- * @return true if `caller` is either operator or owner
363
- */
364
- function _isOperatorOrOwner(
365
- address caller,
366
- bytes32 tokenId
367
- ) internal view virtual returns (bool) {
368
- return (caller == tokenOwnerOf(tokenId) ||
369
- _operators[tokenId].contains(caller));
370
- }
371
-
372
- // --- Transfer functionality
373
-
374
- /**
375
- * @inheritdoc ILSP8IdentifiableDigitalAsset
376
- */
377
- function transfer(
378
- address from,
379
- address to,
380
- bytes32 tokenId,
381
- bool force,
382
- bytes memory data
383
- ) public virtual override {
384
- if (!_isOperatorOrOwner(msg.sender, tokenId)) {
385
- revert LSP8NotTokenOperator(tokenId, msg.sender);
386
- }
387
-
388
- _transfer(from, to, tokenId, force, data);
389
- }
390
-
391
- /**
392
- * @inheritdoc ILSP8IdentifiableDigitalAsset
393
- */
394
- function transferBatch(
395
- address[] memory from,
396
- address[] memory to,
397
- bytes32[] memory tokenId,
398
- bool[] memory force,
399
- bytes[] memory data
400
- ) public virtual override {
401
- uint256 fromLength = from.length;
402
- if (
403
- fromLength != to.length ||
404
- fromLength != tokenId.length ||
405
- fromLength != force.length ||
406
- fromLength != data.length
407
- ) {
408
- revert LSP8InvalidTransferBatch();
409
- }
410
-
411
- for (uint256 i; i < fromLength; ) {
412
- transfer(from[i], to[i], tokenId[i], force[i], data[i]);
413
-
414
- unchecked {
415
- ++i;
416
- }
417
- }
418
- }
419
-
420
- /**
421
- * @dev removes `operator` from the list of operators for the `tokenId`
422
- */
423
- function _revokeOperator(
424
- address operator,
425
- address tokenOwner,
426
- bytes32 tokenId,
427
- bool notified,
428
- bytes memory operatorNotificationData
429
- ) internal virtual {
430
- bool isRemoved = _operators[tokenId].remove(operator);
431
- if (!isRemoved) revert LSP8NonExistingOperator(operator, tokenId);
432
-
433
- emit OperatorRevoked(
434
- operator,
435
- tokenOwner,
436
- tokenId,
437
- notified,
438
- operatorNotificationData
439
- );
440
- }
441
-
442
- /**
443
- * @dev revoke all the current operators for a specific `tokenId` token which belongs to `tokenOwner`.
444
- *
445
- * @param tokenOwner The address that is the owner of the `tokenId`.
446
- * @param tokenId The token to remove the associated operators for.
447
- */
448
- function _clearOperators(
449
- address tokenOwner,
450
- bytes32 tokenId
451
- ) internal virtual {
452
- // here is a good example of why having multiple operators will be expensive.. we
453
- // need to clear them on token transfer
454
- //
455
- // NOTE: this may cause a tx to fail if there is too many operators to clear, in which case
456
- // the tokenOwner needs to call `revokeOperator` until there is less operators to clear and
457
- // the desired `transfer` or `burn` call can succeed.
458
- EnumerableSet.AddressSet storage operatorsForTokenId = _operators[
459
- tokenId
460
- ];
461
-
462
- uint256 operatorListLength = operatorsForTokenId.length();
463
- address operator;
464
- for (uint256 i; i < operatorListLength; ) {
465
- // we are emptying the list, always remove from index 0
466
- operator = operatorsForTokenId.at(0);
467
- _revokeOperator(operator, tokenOwner, tokenId, false, "");
468
-
469
- unchecked {
470
- ++i;
471
- }
472
- }
473
- }
474
-
475
- /**
476
- * @dev Returns whether `tokenId` exists.
477
- *
478
- * Tokens start existing when they are minted ({_mint}), and stop existing when they are burned ({_burn}).
479
- */
480
- function _exists(bytes32 tokenId) internal view virtual returns (bool) {
481
- return _tokenOwners[tokenId] != address(0);
482
- }
483
-
484
- /**
485
- * @dev When `tokenId` does not exist then revert with an error.
486
- */
487
- function _existsOrError(bytes32 tokenId) internal view virtual {
488
- if (!_exists(tokenId)) {
489
- revert LSP8NonExistentTokenId(tokenId);
490
- }
491
- }
492
-
493
- /**
494
- * @dev Create `tokenId` by minting it and transfers it to `to`.
495
- *
496
- * @custom:info Any logic in the:
497
- * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
498
- * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the recipient via LSP1**.
499
- *
500
- * @param to The address that will receive the minted `tokenId`.
501
- * @param tokenId The token ID to create (= mint).
502
- * @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.
503
- * @param data Any additional data the caller wants included in the emitted event, and sent in the hook of the `to` address.
504
- *
505
- * @custom:requirements
506
- * - `tokenId` must not exist and not have been already minted.
507
- * - `to` cannot be the zero address.
508
-
509
- * @custom:events {Transfer} event with `address(0)` as `from` address.
510
- */
511
- function _mint(
512
- address to,
513
- bytes32 tokenId,
514
- bool force,
515
- bytes memory data
516
- ) internal virtual {
517
- if (to == address(0)) {
518
- revert LSP8CannotSendToAddressZero();
519
- }
520
-
521
- // Check that `tokenId` is not already minted
522
- if (_exists(tokenId)) {
523
- revert LSP8TokenIdAlreadyMinted(tokenId);
524
- }
525
-
526
- _beforeTokenTransfer(address(0), to, tokenId, data);
527
-
528
- // Check that `tokenId` was not minted inside the `_beforeTokenTransfer` hook
529
- if (_exists(tokenId)) {
530
- revert LSP8TokenIdAlreadyMinted(tokenId);
531
- }
532
-
533
- // token being minted
534
- ++_existingTokens;
535
-
536
- _ownedTokens[to].add(tokenId);
537
- _tokenOwners[tokenId] = to;
538
-
539
- emit Transfer(msg.sender, address(0), to, tokenId, force, data);
540
-
541
- _afterTokenTransfer(address(0), to, tokenId, data);
542
-
543
- bytes memory lsp1Data = abi.encode(
544
- msg.sender,
545
- address(0),
546
- to,
547
- tokenId,
548
- data
549
- );
550
- _notifyTokenReceiver(to, force, lsp1Data);
551
- }
552
-
553
- /**
554
- * @dev Burn a specific `tokenId`, removing the `tokenId` from the {tokenIdsOf} the caller and decreasing its {balanceOf} by -1.
555
- * This will also clear all the operators allowed to transfer the `tokenId`.
556
- *
557
- * The owner of the `tokenId` will be notified about the `tokenId` being transferred through its LSP1 {universalReceiver}
558
- * function, if it is a contract that supports the LSP1 interface. Its {universalReceiver} function will receive
559
- * all the parameters in the calldata packed encoded.
560
- *
561
- * @custom:info Any logic in the:
562
- * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
563
- * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the sender via LSP1**.
564
- *
565
- * @param tokenId The token to burn.
566
- * @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.
567
- *
568
- * @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`.
569
- *
570
- * @custom:requirements
571
- * - `tokenId` must exist.
572
- *
573
- * @custom:events {Transfer} event with `address(0)` as the `to` address.
574
- */
575
- function _burn(bytes32 tokenId, bytes memory data) internal virtual {
576
- address tokenOwner = tokenOwnerOf(tokenId);
577
-
578
- _beforeTokenTransfer(tokenOwner, address(0), tokenId, data);
579
-
580
- // Re-fetch and update `tokenOwner` in case `tokenId`
581
- // was transferred inside the `_beforeTokenTransfer` hook
582
- tokenOwner = tokenOwnerOf(tokenId);
583
-
584
- // token being burned
585
- --_existingTokens;
586
-
587
- _clearOperators(tokenOwner, tokenId);
588
-
589
- _ownedTokens[tokenOwner].remove(tokenId);
590
- delete _tokenOwners[tokenId];
591
-
592
- emit Transfer(msg.sender, tokenOwner, address(0), tokenId, false, data);
593
-
594
- _afterTokenTransfer(tokenOwner, address(0), tokenId, data);
595
-
596
- bytes memory lsp1Data = abi.encode(
597
- msg.sender,
598
- tokenOwner,
599
- address(0),
600
- tokenId,
601
- data
602
- );
603
-
604
- _notifyTokenSender(tokenOwner, lsp1Data);
605
- }
606
-
607
- /**
608
- * @dev Change the owner of the `tokenId` from `from` to `to`.
609
- *
610
- * Both the sender and recipient will be notified of the `tokenId` being transferred through their LSP1 {universalReceiver}
611
- * function, if they are contracts that support the LSP1 interface. Their `universalReceiver` function will receive
612
- * all the parameters in the calldata packed encoded.
613
- *
614
- * @custom:info Any logic in the:
615
- * - {_beforeTokenTransfer} function will run before updating the balances and ownership of `tokenId`s.
616
- * - {_afterTokenTransfer} function will run after updating the balances and ownership of `tokenId`s, **but before notifying the sender/recipient via LSP1**.
617
- *
618
- * @param from The sender address.
619
- * @param to The recipient address.
620
- * @param tokenId The token to transfer.
621
- * @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.
622
- * @param data Additional data the caller wants included in the emitted event, and sent in the hooks to `from` and `to` addresses.
623
- *
624
- * @custom:requirements
625
- * - `to` cannot be the zero address.
626
- * - `tokenId` token must be owned by `from`.
627
- *
628
- * @custom:events {Transfer} event.
629
- *
630
- * @custom:danger This internal function does not check if the sender is authorized or not to operate on the `tokenId`.
631
- */
632
- function _transfer(
633
- address from,
634
- address to,
635
- bytes32 tokenId,
636
- bool force,
637
- bytes memory data
638
- ) internal virtual {
639
- address tokenOwner = tokenOwnerOf(tokenId);
640
- if (tokenOwner != from) {
641
- revert LSP8NotTokenOwner(tokenOwner, tokenId, from);
642
- }
643
-
644
- if (to == address(0)) {
645
- revert LSP8CannotSendToAddressZero();
646
- }
647
-
648
- _beforeTokenTransfer(from, to, tokenId, data);
649
-
650
- // Check that `tokenId`'s owner was not changed inside the `_beforeTokenTransfer` hook
651
- address currentTokenOwner = tokenOwnerOf(tokenId);
652
- if (tokenOwner != currentTokenOwner) {
653
- revert LSP8TokenOwnerChanged(
654
- tokenId,
655
- tokenOwner,
656
- currentTokenOwner
657
- );
658
- }
659
-
660
- _clearOperators(from, tokenId);
661
-
662
- _ownedTokens[from].remove(tokenId);
663
- _ownedTokens[to].add(tokenId);
664
- _tokenOwners[tokenId] = to;
665
-
666
- emit Transfer(msg.sender, from, to, tokenId, force, data);
667
-
668
- _afterTokenTransfer(from, to, tokenId, data);
669
-
670
- bytes memory lsp1Data = abi.encode(msg.sender, from, to, tokenId, data);
671
-
672
- _notifyTokenSender(from, lsp1Data);
673
- _notifyTokenReceiver(to, force, lsp1Data);
674
- }
675
-
676
- /**
677
- * @dev Sets data for a specific `tokenId` and `dataKey` in the ERC725Y storage
678
- * The ERC725Y data key is the hash of the `tokenId` and `dataKey` concatenated
679
- * @param tokenId The unique identifier for a token.
680
- * @param dataKey The key for the data to set.
681
- * @param dataValue The value to set for the given data key.
682
- * @custom:events {TokenIdDataChanged} event.
683
- */
684
- function _setDataForTokenId(
685
- bytes32 tokenId,
686
- bytes32 dataKey,
687
- bytes memory dataValue
688
- ) internal virtual {
689
- _tokenIdData[tokenId][dataKey] = dataValue;
690
- emit TokenIdDataChanged(tokenId, dataKey, dataValue);
691
- }
692
-
693
- /**
694
- * @dev Retrieves data for a specific `tokenId` and `dataKey` from the ERC725Y storage
695
- * The ERC725Y data key is the hash of the `tokenId` and `dataKey` concatenated
696
- * @param tokenId The unique identifier for a token.
697
- * @param dataKey The key for the data to retrieve.
698
- * @return dataValues The data value associated with the given `tokenId` and `dataKey`.
699
- */
700
- function _getDataForTokenId(
701
- bytes32 tokenId,
702
- bytes32 dataKey
703
- ) internal view virtual returns (bytes memory dataValues) {
704
- return _tokenIdData[tokenId][dataKey];
705
- }
706
-
707
- /**
708
- * @dev Hook that is called before any token transfer, including minting and burning.
709
- * Allows to run custom logic before updating balances and notifiying sender/recipient by overriding this function.
710
- *
711
- * @param from The sender address
712
- * @param to The recipient address
713
- * @param tokenId The tokenId to transfer
714
- * @param data The data sent alongside the transfer
715
- */
716
- function _beforeTokenTransfer(
717
- address from,
718
- address to,
719
- bytes32 tokenId,
720
- bytes memory data // solhint-disable-next-line no-empty-blocks
721
- ) internal virtual {}
722
-
723
- /**
724
- * @dev Hook that is called after any token transfer, including minting and burning.
725
- * Allows to run custom logic after updating balances, but **before notifiying sender/recipient via LSP1** by overriding this function.
726
- *
727
- * @param from The sender address
728
- * @param to The recipient address
729
- * @param tokenId The tokenId to transfer
730
- * @param data The data sent alongside the transfer
731
- */
732
- function _afterTokenTransfer(
733
- address from,
734
- address to,
735
- bytes32 tokenId,
736
- bytes memory data // solhint-disable-next-line no-empty-blocks
737
- ) internal virtual {}
738
-
739
- /**
740
- * @dev Attempt to notify the operator `operator` about the `tokenId` being authorized.
741
- * 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.
742
- * If `operator` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
743
-
744
- * @param operator The address to call the {universalReceiver} function on.
745
- * @param lsp1Data the data to be sent to the `operator` address in the `universalReceiver` call.
746
- */
747
- function _notifyTokenOperator(
748
- address operator,
749
- bytes memory lsp1Data
750
- ) internal virtual {
751
- LSP1Utils.notifyUniversalReceiver(
752
- operator,
753
- _TYPEID_LSP8_TOKENOPERATOR,
754
- lsp1Data
755
- );
756
- }
757
-
758
- /**
759
- * @dev Attempt to notify the token sender `from` about the `tokenId` being transferred.
760
- * 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.
761
- * If `from` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
762
-
763
- * @param from The address to call the {universalReceiver} function on.
764
- * @param lsp1Data the data to be sent to the `from` address in the `universalReceiver` call.
765
- */
766
- function _notifyTokenSender(
767
- address from,
768
- bytes memory lsp1Data
769
- ) internal virtual {
770
- LSP1Utils.notifyUniversalReceiver(
771
- from,
772
- _TYPEID_LSP8_TOKENSSENDER,
773
- lsp1Data
774
- );
775
- }
776
-
777
- /**
778
- * @dev Attempt to notify the token receiver `to` about the `tokenId` being received.
779
- * 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.
780
- *
781
- * 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.
782
- * - if `force` is set to `true`, nothing will happen and no notification will be sent.
783
- * - if `force` is set to `false, the transaction will revert.
784
- *
785
- * @param to The address to call the {universalReceiver} function on.
786
- * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
787
- * @param lsp1Data The data to be sent to the `to` address in the `universalReceiver(...)` call.
788
- */
789
- function _notifyTokenReceiver(
790
- address to,
791
- bool force,
792
- bytes memory lsp1Data
793
- ) internal virtual {
794
- if (
795
- ERC165Checker.supportsERC165InterfaceUnchecked(
796
- to,
797
- _INTERFACEID_LSP1
798
- )
799
- ) {
800
- ILSP1(to).universalReceiver(_TYPEID_LSP8_TOKENSRECIPIENT, lsp1Data);
801
- } else if (!force) {
802
- if (to.code.length != 0) {
803
- revert LSP8NotifyTokenReceiverContractMissingLSP1Interface(to);
804
- } else {
805
- revert LSP8NotifyTokenReceiverIsEOA(to);
806
- }
807
- }
808
- }
809
- }