ERC1155Supply.sol 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0-rc.0) (token/ERC1155/extensions/ERC1155Supply.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC1155} from "../ERC1155.sol";
  5. import {Arrays} from "../../../utils/Arrays.sol";
  6. /**
  7. * @dev Extension of ERC-1155 that adds tracking of total supply per id.
  8. *
  9. * Useful for scenarios where Fungible and Non-fungible tokens have to be
  10. * clearly identified. Note: While a totalSupply of 1 might mean the
  11. * corresponding is an NFT, there is no guarantees that no other token with the
  12. * same id are not going to be minted.
  13. *
  14. * NOTE: This contract implies a global limit of 2**256 - 1 to the number of tokens
  15. * that can be minted.
  16. *
  17. * CAUTION: This extension should not be added in an upgrade to an already deployed contract.
  18. */
  19. abstract contract ERC1155Supply is ERC1155 {
  20. using Arrays for uint256[];
  21. mapping(uint256 id => uint256) private _totalSupply;
  22. uint256 private _totalSupplyAll;
  23. /**
  24. * @dev Total value of tokens in with a given id.
  25. */
  26. function totalSupply(uint256 id) public view virtual returns (uint256) {
  27. return _totalSupply[id];
  28. }
  29. /**
  30. * @dev Total value of tokens.
  31. */
  32. function totalSupply() public view virtual returns (uint256) {
  33. return _totalSupplyAll;
  34. }
  35. /**
  36. * @dev Indicates whether any token exist with a given id, or not.
  37. */
  38. function exists(uint256 id) public view virtual returns (bool) {
  39. return totalSupply(id) > 0;
  40. }
  41. /// @inheritdoc ERC1155
  42. function _update(
  43. address from,
  44. address to,
  45. uint256[] memory ids,
  46. uint256[] memory values
  47. ) internal virtual override {
  48. super._update(from, to, ids, values);
  49. if (from == address(0)) {
  50. uint256 totalMintValue = 0;
  51. for (uint256 i = 0; i < ids.length; ++i) {
  52. uint256 value = values.unsafeMemoryAccess(i);
  53. // Overflow check required: The rest of the code assumes that totalSupply never overflows
  54. _totalSupply[ids.unsafeMemoryAccess(i)] += value;
  55. totalMintValue += value;
  56. }
  57. // Overflow check required: The rest of the code assumes that totalSupplyAll never overflows
  58. _totalSupplyAll += totalMintValue;
  59. }
  60. if (to == address(0)) {
  61. uint256 totalBurnValue = 0;
  62. for (uint256 i = 0; i < ids.length; ++i) {
  63. uint256 value = values.unsafeMemoryAccess(i);
  64. unchecked {
  65. // Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
  66. _totalSupply[ids.unsafeMemoryAccess(i)] -= value;
  67. // Overflow not possible: sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
  68. totalBurnValue += value;
  69. }
  70. }
  71. unchecked {
  72. // Overflow not possible: totalBurnValue = sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
  73. _totalSupplyAll -= totalBurnValue;
  74. }
  75. }
  76. }
  77. }