@lukso/lsp7-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 (39) hide show
  1. package/README.md +2 -2
  2. package/artifacts/ILSP7DigitalAsset.json +0 -112
  3. package/artifacts/LSP7Burnable.json +0 -11
  4. package/artifacts/LSP7BurnableInitAbstract.json +0 -16
  5. package/artifacts/LSP7CappedSupply.json +0 -11
  6. package/artifacts/LSP7CappedSupplyInitAbstract.json +0 -16
  7. package/artifacts/LSP7DigitalAsset.json +0 -11
  8. package/artifacts/LSP7DigitalAssetInitAbstract.json +0 -16
  9. package/artifacts/LSP7Mintable.json +2 -13
  10. package/artifacts/LSP7MintableInit.json +2 -13
  11. package/artifacts/LSP7Votes.json +1154 -0
  12. package/artifacts/LSP7VotesInitAbstract.json +1146 -0
  13. package/contracts/ILSP7DigitalAsset.sol +2 -9
  14. package/contracts/LSP7Constants.sol +10 -0
  15. package/contracts/LSP7DigitalAsset.sol +780 -27
  16. package/contracts/LSP7DigitalAssetInitAbstract.sol +784 -14
  17. package/contracts/extensions/LSP7CappedSupply.sol +1 -1
  18. package/contracts/extensions/LSP7CappedSupplyInitAbstract.sol +1 -1
  19. package/contracts/extensions/LSP7Votes.sol +416 -0
  20. package/contracts/extensions/LSP7VotesConstants.sol +8 -0
  21. package/contracts/extensions/LSP7VotesInitAbstract.sol +443 -0
  22. package/dist/index.cjs +5 -1
  23. package/dist/index.d.cts +5 -3
  24. package/dist/index.d.mts +5 -3
  25. package/dist/index.d.ts +5 -3
  26. package/dist/index.mjs +5 -1
  27. package/package.json +5 -6
  28. package/types/index.ts +3488 -1480
  29. package/contracts/LSP7DigitalAssetCore.sol +0 -744
  30. package/types/common.ts +0 -131
  31. package/types/contracts/ILSP7DigitalAsset.ts +0 -626
  32. package/types/contracts/LSP7DigitalAsset.ts +0 -698
  33. package/types/contracts/LSP7DigitalAssetInitAbstract.ts +0 -729
  34. package/types/contracts/extensions/LSP7Burnable.ts +0 -717
  35. package/types/contracts/extensions/LSP7BurnableInitAbstract.ts +0 -748
  36. package/types/contracts/extensions/LSP7CappedSupply.ts +0 -712
  37. package/types/contracts/extensions/LSP7CappedSupplyInitAbstract.ts +0 -743
  38. package/types/contracts/presets/LSP7Mintable.ts +0 -717
  39. package/types/contracts/presets/LSP7MintableInit.ts +0 -779
@@ -2,51 +2,94 @@
2
2
  pragma solidity ^0.8.4;
3
3
 
4
4
  // interfaces
5
- import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
5
+ import {
6
+ ILSP1UniversalReceiver as ILSP1
7
+ } from "@lukso/lsp1-contracts/contracts/ILSP1UniversalReceiver.sol";
8
+ import {ILSP7DigitalAsset} from "./ILSP7DigitalAsset.sol";
6
9
 
7
10
  // modules
8
- import {ERC725YCore} from "@erc725/smart-contracts/contracts/ERC725YCore.sol";
9
11
  import {
10
- LSP4DigitalAssetMetadata
12
+ EnumerableSet
13
+ } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
14
+ import {
15
+ ERC165Checker
16
+ } from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
17
+ import {
18
+ LSP4DigitalAssetMetadata,
19
+ ERC725Y
11
20
  } from "@lukso/lsp4-contracts/contracts/LSP4DigitalAssetMetadata.sol";
12
- import {LSP7DigitalAssetCore} from "./LSP7DigitalAssetCore.sol";
13
21
  import {
14
22
  LSP17Extendable
15
23
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Extendable.sol";
16
24
 
17
25
  // libraries
26
+ import {LSP1Utils} from "@lukso/lsp1-contracts/contracts/LSP1Utils.sol";
18
27
  import {LSP2Utils} from "@lukso/lsp2-contracts/contracts/LSP2Utils.sol";
19
28
 
20
29
  // constants
21
- import {_INTERFACEID_LSP7} from "./LSP7Constants.sol";
22
- import {LSP7TokenContractCannotHoldValue} from "./LSP7Errors.sol";
23
-
30
+ import {
31
+ _INTERFACEID_LSP1
32
+ } from "@lukso/lsp1-contracts/contracts/LSP1Constants.sol";
24
33
  import {
25
34
  _LSP17_EXTENSION_PREFIX
26
35
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Constants.sol";
36
+ import {
37
+ _INTERFACEID_LSP7,
38
+ _TYPEID_LSP7_TOKENOPERATOR,
39
+ _TYPEID_LSP7_TOKENSSENDER,
40
+ _TYPEID_LSP7_TOKENSRECIPIENT
41
+ } from "./LSP7Constants.sol";
27
42
 
28
43
  // errors
29
-
30
44
  import {
31
45
  NoExtensionFoundForFunctionSelector,
32
46
  InvalidFunctionSelector,
33
47
  InvalidExtensionAddress
34
48
  } from "@lukso/lsp17contractextension-contracts/contracts/LSP17Errors.sol";
49
+ import {
50
+ LSP7TokenContractCannotHoldValue,
51
+ LSP7AmountExceedsAuthorizedAmount,
52
+ LSP7InvalidTransferBatch,
53
+ LSP7AmountExceedsBalance,
54
+ LSP7DecreasedAllowanceBelowZero,
55
+ LSP7CannotUseAddressZeroAsOperator,
56
+ LSP7TokenOwnerCannotBeOperator,
57
+ LSP7CannotSendWithAddressZero,
58
+ LSP7NotifyTokenReceiverContractMissingLSP1Interface,
59
+ LSP7NotifyTokenReceiverIsEOA,
60
+ OperatorAllowanceCannotBeIncreasedFromZero,
61
+ LSP7BatchCallFailed,
62
+ LSP7RevokeOperatorNotAuthorized,
63
+ LSP7DecreaseAllowanceNotAuthorized
64
+ } from "./LSP7Errors.sol";
35
65
 
36
66
  /**
37
- * @title Implementation of a LSP7 Digital Asset, a contract that represents a fungible token.
67
+ * @title Implementation of the LSP7 Digital Asset standard, a contract that represents a fungible token.
38
68
  * @author Matthew Stevens
39
- *
40
- * @dev Minting and transferring are supplied with a `uint256` amount.
41
- * This implementation is agnostic to the way tokens are created.
42
- * A supply mechanism has to be added in a derived contract using {_mint}
43
- * For a generic mechanism, see {LSP7Mintable}.
44
69
  */
45
70
  abstract contract LSP7DigitalAsset is
71
+ ILSP7DigitalAsset,
46
72
  LSP4DigitalAssetMetadata,
47
- LSP7DigitalAssetCore,
48
73
  LSP17Extendable
49
74
  {
75
+ using EnumerableSet for EnumerableSet.AddressSet;
76
+
77
+ // --- Storage
78
+
79
+ bool internal _isNonDivisible;
80
+
81
+ uint256 internal _existingTokens;
82
+
83
+ // Mapping from `tokenOwner` to an `amount` of tokens
84
+ mapping(address => uint256) internal _tokenOwnerBalances;
85
+
86
+ // Mapping an `address` to its authorized operator addresses.
87
+ mapping(address => EnumerableSet.AddressSet) internal _operators;
88
+
89
+ // Mapping a `tokenOwner` to an `operator` to `amount` of tokens.
90
+ mapping(address => mapping(address => uint256))
91
+ internal _operatorAuthorizedAmount;
92
+
50
93
  /**
51
94
  * @notice Sets the token-Metadata
52
95
  * @param name_ The name of the token.
@@ -65,7 +108,7 @@ abstract contract LSP7DigitalAsset is
65
108
  _isNonDivisible = isNonDivisible_;
66
109
  }
67
110
 
68
- // fallback function
111
+ // fallback functions
69
112
 
70
113
  /**
71
114
  * @notice The `fallback` function was called with the following amount of native tokens: `msg.value`; and the following calldata: `callData`.
@@ -100,6 +143,7 @@ abstract contract LSP7DigitalAsset is
100
143
  * @dev Reverts whenever someone tries to send native tokens to a LSP7 contract.
101
144
  * @notice LSP7 contract cannot receive native tokens.
102
145
  */
146
+ // solhint-disable-next-line no-complex-fallback
103
147
  receive() external payable virtual {
104
148
  // revert on empty calls with no value
105
149
  if (msg.value == 0) {
@@ -169,9 +213,7 @@ abstract contract LSP7DigitalAsset is
169
213
  );
170
214
 
171
215
  // Check if there is an extension stored under the generated data key
172
- bytes memory extensionAddress = ERC725YCore._getData(
173
- mappedExtensionDataKey
174
- );
216
+ bytes memory extensionAddress = _getData(mappedExtensionDataKey);
175
217
  if (extensionAddress.length != 20 && extensionAddress.length != 0)
176
218
  revert InvalidExtensionAddress(extensionAddress);
177
219
 
@@ -179,20 +221,731 @@ abstract contract LSP7DigitalAsset is
179
221
  }
180
222
 
181
223
  /**
182
- * @inheritdoc IERC165
224
+ * @inheritdoc LSP17Extendable
183
225
  */
184
226
  function supportsInterface(
185
227
  bytes4 interfaceId
186
- )
187
- public
188
- view
189
- virtual
190
- override(IERC165, ERC725YCore, LSP17Extendable)
191
- returns (bool)
192
- {
228
+ ) public view virtual override(ERC725Y, LSP17Extendable) returns (bool) {
193
229
  return
194
230
  interfaceId == _INTERFACEID_LSP7 ||
195
231
  super.supportsInterface(interfaceId) ||
196
232
  LSP17Extendable._supportsInterfaceInERC165Extension(interfaceId);
197
233
  }
234
+
235
+ // --- Token queries
236
+
237
+ /**
238
+ * @inheritdoc ILSP7DigitalAsset
239
+ */
240
+ function decimals() public view virtual override returns (uint8) {
241
+ return _isNonDivisible ? 0 : 18;
242
+ }
243
+
244
+ /**
245
+ * @inheritdoc ILSP7DigitalAsset
246
+ */
247
+ function totalSupply() public view virtual override returns (uint256) {
248
+ return _existingTokens;
249
+ }
250
+
251
+ // --- Token owner queries
252
+
253
+ /**
254
+ * @inheritdoc ILSP7DigitalAsset
255
+ */
256
+ function balanceOf(
257
+ address tokenOwner
258
+ ) public view virtual override returns (uint256) {
259
+ return _tokenOwnerBalances[tokenOwner];
260
+ }
261
+
262
+ // --- General functionality
263
+
264
+ /**
265
+ * @inheritdoc ILSP7DigitalAsset
266
+ *
267
+ * @custom:info It's not possible to send value along the functions call due to the use of `delegatecall`.
268
+ */
269
+ function batchCalls(
270
+ bytes[] calldata data
271
+ ) public virtual override returns (bytes[] memory results) {
272
+ results = new bytes[](data.length);
273
+ for (uint256 i; i < data.length; ) {
274
+ (bool success, bytes memory result) = address(this).delegatecall(
275
+ data[i]
276
+ );
277
+
278
+ if (!success) {
279
+ // Look for revert reason and bubble it up if present
280
+ if (result.length != 0) {
281
+ // The easiest way to bubble the revert reason is using memory via assembly
282
+ // solhint-disable no-inline-assembly
283
+ /// @solidity memory-safe-assembly
284
+ assembly {
285
+ let returndata_size := mload(result)
286
+ revert(add(32, result), returndata_size)
287
+ }
288
+ } else {
289
+ revert LSP7BatchCallFailed({callIndex: i});
290
+ }
291
+ }
292
+
293
+ results[i] = result;
294
+
295
+ unchecked {
296
+ ++i;
297
+ }
298
+ }
299
+ }
300
+
301
+ // --- Operator functionality
302
+
303
+ /**
304
+ * @inheritdoc ILSP7DigitalAsset
305
+ *
306
+ * @custom:danger To avoid front-running and Allowance Double-Spend Exploit when
307
+ * increasing or decreasing the authorized amount of an operator, it is advised to
308
+ * use the {increaseAllowance} and {decreaseAllowance} functions.
309
+ *
310
+ * For more information, see:
311
+ * https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/
312
+ */
313
+ function authorizeOperator(
314
+ address operator,
315
+ uint256 amount,
316
+ bytes memory operatorNotificationData
317
+ ) public virtual override {
318
+ _updateOperator(
319
+ msg.sender,
320
+ operator,
321
+ amount,
322
+ true,
323
+ operatorNotificationData
324
+ );
325
+
326
+ bytes memory lsp1Data = abi.encode(
327
+ msg.sender,
328
+ amount,
329
+ operatorNotificationData
330
+ );
331
+
332
+ _notifyTokenOperator(operator, lsp1Data);
333
+ }
334
+
335
+ /**
336
+ * @inheritdoc ILSP7DigitalAsset
337
+ */
338
+ function revokeOperator(
339
+ address operator,
340
+ address tokenOwner,
341
+ bool notify,
342
+ bytes memory operatorNotificationData
343
+ ) public virtual override {
344
+ if (msg.sender != tokenOwner && msg.sender != operator) {
345
+ revert LSP7RevokeOperatorNotAuthorized(
346
+ msg.sender,
347
+ tokenOwner,
348
+ operator
349
+ );
350
+ }
351
+
352
+ _updateOperator(
353
+ tokenOwner,
354
+ operator,
355
+ 0,
356
+ notify,
357
+ operatorNotificationData
358
+ );
359
+
360
+ if (notify) {
361
+ bytes memory lsp1Data = abi.encode(
362
+ tokenOwner,
363
+ 0,
364
+ operatorNotificationData
365
+ );
366
+
367
+ _notifyTokenOperator(operator, lsp1Data);
368
+ }
369
+ }
370
+
371
+ /**
372
+ * @inheritdoc ILSP7DigitalAsset
373
+ */
374
+ function authorizedAmountFor(
375
+ address operator,
376
+ address tokenOwner
377
+ ) public view virtual override returns (uint256) {
378
+ if (tokenOwner == operator) {
379
+ return _tokenOwnerBalances[tokenOwner];
380
+ } else {
381
+ return _operatorAuthorizedAmount[tokenOwner][operator];
382
+ }
383
+ }
384
+
385
+ /**
386
+ * @inheritdoc ILSP7DigitalAsset
387
+ */
388
+ function getOperatorsOf(
389
+ address tokenOwner
390
+ ) public view virtual override returns (address[] memory) {
391
+ return _operators[tokenOwner].values();
392
+ }
393
+
394
+ /**
395
+ * @inheritdoc ILSP7DigitalAsset
396
+ */
397
+ function increaseAllowance(
398
+ address operator,
399
+ uint256 addedAmount,
400
+ bytes memory operatorNotificationData
401
+ ) public virtual override {
402
+ uint256 oldAllowance = authorizedAmountFor(operator, msg.sender);
403
+ if (oldAllowance == 0)
404
+ revert OperatorAllowanceCannotBeIncreasedFromZero(operator);
405
+
406
+ uint256 newAllowance = oldAllowance + addedAmount;
407
+
408
+ _updateOperator(
409
+ msg.sender,
410
+ operator,
411
+ newAllowance,
412
+ true,
413
+ operatorNotificationData
414
+ );
415
+
416
+ bytes memory lsp1Data = abi.encode(
417
+ msg.sender,
418
+ newAllowance,
419
+ operatorNotificationData
420
+ );
421
+
422
+ _notifyTokenOperator(operator, lsp1Data);
423
+ }
424
+
425
+ /**
426
+ * @inheritdoc ILSP7DigitalAsset
427
+ */
428
+ function decreaseAllowance(
429
+ address operator,
430
+ address tokenOwner,
431
+ uint256 subtractedAmount,
432
+ bytes memory operatorNotificationData
433
+ ) public virtual override {
434
+ if (msg.sender != tokenOwner && msg.sender != operator) {
435
+ revert LSP7DecreaseAllowanceNotAuthorized(
436
+ msg.sender,
437
+ tokenOwner,
438
+ operator
439
+ );
440
+ }
441
+
442
+ uint256 currentAllowance = authorizedAmountFor(operator, tokenOwner);
443
+ if (currentAllowance < subtractedAmount) {
444
+ revert LSP7DecreasedAllowanceBelowZero();
445
+ }
446
+
447
+ uint256 newAllowance;
448
+ unchecked {
449
+ newAllowance = currentAllowance - subtractedAmount;
450
+ _updateOperator(
451
+ tokenOwner,
452
+ operator,
453
+ newAllowance,
454
+ true,
455
+ operatorNotificationData
456
+ );
457
+ }
458
+
459
+ bytes memory lsp1Data = abi.encode(
460
+ tokenOwner,
461
+ newAllowance,
462
+ operatorNotificationData
463
+ );
464
+
465
+ _notifyTokenOperator(operator, lsp1Data);
466
+ }
467
+
468
+ // --- Transfer functionality
469
+
470
+ /**
471
+ * @inheritdoc ILSP7DigitalAsset
472
+ */
473
+ function transfer(
474
+ address from,
475
+ address to,
476
+ uint256 amount,
477
+ bool force,
478
+ bytes memory data
479
+ ) public virtual override {
480
+ if (msg.sender != from) {
481
+ _spendAllowance({
482
+ operator: msg.sender,
483
+ tokenOwner: from,
484
+ amountToSpend: amount
485
+ });
486
+ }
487
+
488
+ _transfer(from, to, amount, force, data);
489
+ }
490
+
491
+ /**
492
+ * @inheritdoc ILSP7DigitalAsset
493
+ */
494
+ function transferBatch(
495
+ address[] memory from,
496
+ address[] memory to,
497
+ uint256[] memory amount,
498
+ bool[] memory force,
499
+ bytes[] memory data
500
+ ) public virtual override {
501
+ uint256 fromLength = from.length;
502
+ if (
503
+ fromLength != to.length ||
504
+ fromLength != amount.length ||
505
+ fromLength != force.length ||
506
+ fromLength != data.length
507
+ ) {
508
+ revert LSP7InvalidTransferBatch();
509
+ }
510
+
511
+ for (uint256 i; i < fromLength; ) {
512
+ // using the public transfer function to handle updates to operator authorized amounts
513
+ transfer(from[i], to[i], amount[i], force[i], data[i]);
514
+
515
+ unchecked {
516
+ ++i;
517
+ }
518
+ }
519
+ }
520
+
521
+ /**
522
+ * @dev Changes token `amount` the `operator` has access to from `tokenOwner` tokens.
523
+ * If the amount is zero the operator is removed from the list of operators, otherwise he is added to the list of operators.
524
+ * If the amount is zero then the operator is being revoked, otherwise the operator amount is being modified.
525
+ *
526
+ * @param tokenOwner The address that will give `operator` an allowance for on its balance.
527
+ * @param operator The address to grant an allowance to spend.
528
+ * @param allowance The maximum amount of token that `operator` can spend from the `tokenOwner`'s balance.
529
+ * @param notified Boolean indicating whether the operator has been notified about the change of allowance
530
+ * @param operatorNotificationData The data to send to the universalReceiver function of the operator in case of notifying
531
+ *
532
+ * @custom:events
533
+ * - {OperatorRevoked} event when operator's allowance is set to `0`.
534
+ * - {OperatorAuthorizationChanged} event when operator's allowance is set to any other amount.
535
+ *
536
+ * @custom:requirements
537
+ * - `operator` cannot be the zero address.
538
+ * - `operator` cannot be the same address as `tokenOwner`.
539
+ */
540
+ function _updateOperator(
541
+ address tokenOwner,
542
+ address operator,
543
+ uint256 allowance,
544
+ bool notified,
545
+ bytes memory operatorNotificationData
546
+ ) internal virtual {
547
+ if (operator == address(0)) {
548
+ revert LSP7CannotUseAddressZeroAsOperator();
549
+ }
550
+
551
+ if (operator == tokenOwner) {
552
+ revert LSP7TokenOwnerCannotBeOperator();
553
+ }
554
+
555
+ _operatorAuthorizedAmount[tokenOwner][operator] = allowance;
556
+
557
+ if (allowance != 0) {
558
+ _operators[tokenOwner].add(operator);
559
+ emit OperatorAuthorizationChanged(
560
+ operator,
561
+ tokenOwner,
562
+ allowance,
563
+ operatorNotificationData
564
+ );
565
+ } else {
566
+ _operators[tokenOwner].remove(operator);
567
+ emit OperatorRevoked(
568
+ operator,
569
+ tokenOwner,
570
+ notified,
571
+ operatorNotificationData
572
+ );
573
+ }
574
+ }
575
+
576
+ /**
577
+ * @dev Mints `amount` of tokens and transfers it to `to`.
578
+ *
579
+ * @custom:info Any logic in the:
580
+ * - {_beforeTokenTransfer} function will run before updating the balances.
581
+ * - {_afterTokenTransfer} function will run after updating the balances, **but before notifying the recipient via LSP1**.
582
+ *
583
+ * @param to The address to mint tokens for.
584
+ * @param amount The amount of tokens to mint.
585
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
586
+ * @param data Additional data the caller wants included in the emitted {Transfer} event, and sent in the LSP1 hook to the `to` address.
587
+ *
588
+ * @custom:requirements
589
+ * - `to` cannot be the zero address.
590
+ *
591
+ * @custom:events {Transfer} event with `address(0)` as `from`.
592
+ */
593
+ function _mint(
594
+ address to,
595
+ uint256 amount,
596
+ bool force,
597
+ bytes memory data
598
+ ) internal virtual {
599
+ if (to == address(0)) {
600
+ revert LSP7CannotSendWithAddressZero();
601
+ }
602
+
603
+ _beforeTokenTransfer(address(0), to, amount, force, data);
604
+
605
+ // tokens being minted
606
+ _existingTokens += amount;
607
+
608
+ _tokenOwnerBalances[to] += amount;
609
+
610
+ emit Transfer({
611
+ operator: msg.sender,
612
+ from: address(0),
613
+ to: to,
614
+ amount: amount,
615
+ force: force,
616
+ data: data
617
+ });
618
+
619
+ _afterTokenTransfer(address(0), to, amount, force, data);
620
+
621
+ bytes memory lsp1Data = abi.encode(
622
+ msg.sender,
623
+ address(0),
624
+ to,
625
+ amount,
626
+ data
627
+ );
628
+ _notifyTokenReceiver(to, force, lsp1Data);
629
+ }
630
+
631
+ /**
632
+ * @dev Burns (= destroys) `amount` of tokens, decrease the `from` balance. This is done by sending them to the zero address.
633
+ *
634
+ * Both the sender and recipient will be notified of the token transfer through the LSP1 {universalReceiver}
635
+ * function, if they are contracts that support the LSP1 interface. Their `universalReceiver` function will receive
636
+ * all the parameters in the calldata packed encoded.
637
+ *
638
+ * @custom:info Any logic in the:
639
+ * - {_beforeTokenTransfer} function will run before updating the balances.
640
+ * - {_afterTokenTransfer} function will run after updating the balances, **but before notifying the sender via LSP1**.
641
+ *
642
+ * @param from The address to burn tokens from its balance.
643
+ * @param amount The amount of tokens to burn.
644
+ * @param data Additional data the caller wants included in the emitted event, and sent in the LSP1 hook to the `from` and `to` address.
645
+ *
646
+ * @custom:hint In dApps, you can know which address is burning tokens by listening for the `Transfer` event and filter with the zero address as `to`.
647
+ *
648
+ * @custom:requirements
649
+ * - `from` cannot be the zero address.
650
+ * - `from` must have at least `amount` tokens.
651
+ * - If the caller is not `from`, it must be an operator for `from` with access to at least
652
+ * `amount` tokens.
653
+ *
654
+ * @custom:events {Transfer} event with `address(0)` as the `to` address
655
+ */
656
+ function _burn(
657
+ address from,
658
+ uint256 amount,
659
+ bytes memory data
660
+ ) internal virtual {
661
+ if (from == address(0)) {
662
+ revert LSP7CannotSendWithAddressZero();
663
+ }
664
+
665
+ _beforeTokenTransfer(from, address(0), amount, false, data);
666
+
667
+ uint256 balance = _tokenOwnerBalances[from];
668
+ if (amount > balance) {
669
+ revert LSP7AmountExceedsBalance(balance, from, amount);
670
+ }
671
+ // tokens being burnt
672
+ _existingTokens -= amount;
673
+
674
+ _tokenOwnerBalances[from] -= amount;
675
+
676
+ emit Transfer({
677
+ operator: msg.sender,
678
+ from: from,
679
+ to: address(0),
680
+ amount: amount,
681
+ force: false,
682
+ data: data
683
+ });
684
+
685
+ _afterTokenTransfer(from, address(0), amount, false, data);
686
+
687
+ bytes memory lsp1Data = abi.encode(
688
+ msg.sender,
689
+ from,
690
+ address(0),
691
+ amount,
692
+ data
693
+ );
694
+ _notifyTokenSender(from, lsp1Data);
695
+ }
696
+
697
+ /**
698
+ * @dev Spend `amountToSpend` from the `operator`'s authorized on behalf of the `tokenOwner`.
699
+ *
700
+ * @param operator The address of the operator to decrease the allowance of.
701
+ * @param tokenOwner The address that granted an allowance on its balance to `operator`.
702
+ * @param amountToSpend The amount of tokens to subtract in allowance of `operator`.
703
+ *
704
+ * @custom:events
705
+ * - {OperatorRevoked} event when operator's allowance is set to `0`.
706
+ * - {OperatorAuthorizationChanged} event when operator's allowance is set to any other amount.
707
+ *
708
+ * @custom:requirements
709
+ * - The `amountToSpend` MUST be at least the allowance granted to `operator` (accessible via {`authorizedAmountFor}`)
710
+ * - `operator` cannot be the zero address.
711
+ * - `operator` cannot be the same address as `tokenOwner`.
712
+ */
713
+ function _spendAllowance(
714
+ address operator,
715
+ address tokenOwner,
716
+ uint256 amountToSpend
717
+ ) internal virtual {
718
+ uint256 authorizedAmount = _operatorAuthorizedAmount[tokenOwner][
719
+ operator
720
+ ];
721
+
722
+ if (authorizedAmount == 0 || amountToSpend > authorizedAmount) {
723
+ revert LSP7AmountExceedsAuthorizedAmount(
724
+ tokenOwner,
725
+ authorizedAmount,
726
+ operator,
727
+ amountToSpend
728
+ );
729
+ }
730
+
731
+ _updateOperator({
732
+ tokenOwner: tokenOwner,
733
+ operator: operator,
734
+ allowance: authorizedAmount - amountToSpend,
735
+ notified: false,
736
+ operatorNotificationData: ""
737
+ });
738
+ }
739
+
740
+ /**
741
+ * @dev Transfer tokens from `from` to `to` by decreasing the balance of `from` by `-amount` and increasing the balance
742
+ * of `to` by `+amount`.
743
+ *
744
+ * Both the sender and recipient will be notified of the token transfer through the LSP1 {universalReceiver}
745
+ * function, if they are contracts that support the LSP1 interface. Their `universalReceiver` function will receive
746
+ * all the parameters in the calldata packed encoded.
747
+ *
748
+ * @custom:info Any logic in the:
749
+ * - {_beforeTokenTransfer} function will run before updating the balances.
750
+ * - {_afterTokenTransfer} function will run after updating the balances, **but before notifying the sender/recipient via LSP1**.
751
+ *
752
+ * @param from The address to decrease the balance.
753
+ * @param to The address to increase the balance.
754
+ * @param amount The amount of tokens to transfer from `from` to `to`.
755
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
756
+ * @param data Additional data the caller wants included in the emitted event, and sent in the LSP1 hook to the `from` and `to` address.
757
+ *
758
+ * @custom:requirements
759
+ * - `from` cannot be the zero address.
760
+ * - `to` cannot be the zero address.
761
+ * - `from` must have at least `amount` of tokens.
762
+ *
763
+ * @custom:events {Transfer} event.
764
+ */
765
+ function _transfer(
766
+ address from,
767
+ address to,
768
+ uint256 amount,
769
+ bool force,
770
+ bytes memory data
771
+ ) internal virtual {
772
+ if (from == address(0) || to == address(0)) {
773
+ revert LSP7CannotSendWithAddressZero();
774
+ }
775
+
776
+ _beforeTokenTransfer(from, to, amount, force, data);
777
+
778
+ _update(from, to, amount, force, data);
779
+
780
+ _afterTokenTransfer(from, to, amount, force, data);
781
+
782
+ bytes memory lsp1Data = abi.encode(msg.sender, from, to, amount, data);
783
+
784
+ _notifyTokenSender(from, lsp1Data);
785
+ _notifyTokenReceiver(to, force, lsp1Data);
786
+ }
787
+
788
+ /**
789
+ * @dev Transfers `amount` of tokens from `from` to `to`, or alternatively mints (or burns) if `from`
790
+ * (or `to`) is the zero address. All customizations to transfers, mints, and burns should be done by overriding
791
+ * this function.
792
+ *
793
+ * @custom:events {Transfer} event.
794
+ */
795
+ function _update(
796
+ address from,
797
+ address to,
798
+ uint256 amount,
799
+ bool force,
800
+ bytes memory data
801
+ ) internal virtual {
802
+ if (from == address(0)) {
803
+ // Overflow check required: The rest of the code assumes that totalSupply never overflows
804
+ _existingTokens += amount;
805
+ } else {
806
+ uint256 fromBalance = _tokenOwnerBalances[from];
807
+ if (fromBalance < amount) {
808
+ revert LSP7AmountExceedsBalance(fromBalance, from, amount);
809
+ }
810
+ unchecked {
811
+ // Overflow not possible: amount <= fromBalance <= totalSupply.
812
+ _tokenOwnerBalances[from] = fromBalance - amount;
813
+ }
814
+ }
815
+
816
+ if (to == address(0)) {
817
+ unchecked {
818
+ // Overflow not possible: amount <= totalSupply or amount <= fromBalance <= totalSupply.
819
+ _existingTokens -= amount;
820
+ }
821
+ } else {
822
+ unchecked {
823
+ // Overflow not possible: balance + amount is at most totalSupply, which we know fits into a uint256.
824
+ _tokenOwnerBalances[to] += amount;
825
+ }
826
+ }
827
+
828
+ emit Transfer({
829
+ operator: msg.sender,
830
+ from: from,
831
+ to: to,
832
+ amount: amount,
833
+ force: force,
834
+ data: data
835
+ });
836
+
837
+ _afterTokenTransfer(from, to, amount, force, data);
838
+
839
+ bytes memory lsp1Data = abi.encode(msg.sender, from, to, amount, data);
840
+
841
+ _notifyTokenSender(from, lsp1Data);
842
+ _notifyTokenReceiver(to, force, lsp1Data);
843
+ }
844
+
845
+ /**
846
+ * @dev Hook that is called before any token transfer, including minting and burning.
847
+ * Allows to run custom logic before updating balances and notifying sender/recipient by overriding this function.
848
+ *
849
+ * @param from The sender address
850
+ * @param to The recipient address
851
+ * @param amount The amount of token to transfer
852
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
853
+ * @param data The data sent alongside the transfer
854
+ */
855
+ function _beforeTokenTransfer(
856
+ address from,
857
+ address to,
858
+ uint256 amount,
859
+ bool force,
860
+ bytes memory data // solhint-disable-next-line no-empty-blocks
861
+ ) internal virtual {}
862
+
863
+ /**
864
+ * @dev Hook that is called after any token transfer, including minting and burning.
865
+ * Allows to run custom logic after updating balances, but **before notifying sender/recipient** by overriding this function.
866
+ *
867
+ * @param from The sender address
868
+ * @param to The recipient address
869
+ * @param amount The amount of token to transfer
870
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
871
+ * @param data The data sent alongside the transfer
872
+ */
873
+ function _afterTokenTransfer(
874
+ address from,
875
+ address to,
876
+ uint256 amount,
877
+ bool force,
878
+ bytes memory data // solhint-disable-next-line no-empty-blocks
879
+ ) internal virtual {}
880
+
881
+ /**
882
+ * @dev Attempt to notify the operator `operator` about the `amount` tokens being authorized with.
883
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP7_TOKENOPERATOR` as typeId, if `operator` is a contract that supports the LSP1 interface.
884
+ * If `operator` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
885
+
886
+ * @param operator The address to call the {universalReceiver} function on.
887
+ * @param lsp1Data the data to be sent to the `operator` address in the `universalReceiver` call.
888
+ */
889
+ function _notifyTokenOperator(
890
+ address operator,
891
+ bytes memory lsp1Data
892
+ ) internal virtual {
893
+ LSP1Utils.notifyUniversalReceiver(
894
+ operator,
895
+ _TYPEID_LSP7_TOKENOPERATOR,
896
+ lsp1Data
897
+ );
898
+ }
899
+
900
+ /**
901
+ * @dev Attempt to notify the token sender `from` about the `amount` of tokens being transferred.
902
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP7_TOKENSSENDER` as typeId, if `from` is a contract that supports the LSP1 interface.
903
+ * If `from` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
904
+
905
+ * @param from The address to call the {universalReceiver} function on.
906
+ * @param lsp1Data the data to be sent to the `from` address in the `universalReceiver` call.
907
+ */
908
+ function _notifyTokenSender(
909
+ address from,
910
+ bytes memory lsp1Data
911
+ ) internal virtual {
912
+ LSP1Utils.notifyUniversalReceiver(
913
+ from,
914
+ _TYPEID_LSP7_TOKENSSENDER,
915
+ lsp1Data
916
+ );
917
+ }
918
+
919
+ /**
920
+ * @dev Attempt to notify the token receiver `to` about the `amount` tokens being received.
921
+ * This is done by calling its {universalReceiver} function with the `_TYPEID_LSP7_TOKENSRECIPIENT` as typeId, if `to` is a contract that supports the LSP1 interface.
922
+ *
923
+ * 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.
924
+ * - if `force` is set to `true`, nothing will happen and no notification will be sent.
925
+ * - if `force` is set to `false, the transaction will revert.
926
+ *
927
+ * @param to The address to call the {universalReceiver} function on.
928
+ * @param force A boolean that describe if transfer to a `to` address that does not support LSP1 is allowed or not.
929
+ * @param lsp1Data The data to be sent to the `to` address in the `universalReceiver(...)` call.
930
+ */
931
+ function _notifyTokenReceiver(
932
+ address to,
933
+ bool force,
934
+ bytes memory lsp1Data
935
+ ) internal virtual {
936
+ if (
937
+ ERC165Checker.supportsERC165InterfaceUnchecked(
938
+ to,
939
+ _INTERFACEID_LSP1
940
+ )
941
+ ) {
942
+ ILSP1(to).universalReceiver(_TYPEID_LSP7_TOKENSRECIPIENT, lsp1Data);
943
+ } else if (!force) {
944
+ if (to.code.length != 0) {
945
+ revert LSP7NotifyTokenReceiverContractMissingLSP1Interface(to);
946
+ } else {
947
+ revert LSP7NotifyTokenReceiverIsEOA(to);
948
+ }
949
+ }
950
+ }
198
951
  }