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