ERC1155Supply.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (token/ERC1155/extensions/ERC1155Supply.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC1155} from "../ERC1155.sol";
  5. /**
  6. * @dev Extension of ERC1155 that adds tracking of total supply per id.
  7. *
  8. * Useful for scenarios where Fungible and Non-fungible tokens have to be
  9. * clearly identified. Note: While a totalSupply of 1 might mean the
  10. * corresponding is an NFT, there is no guarantees that no other token with the
  11. * same id are not going to be minted.
  12. *
  13. * NOTE: This contract implies a global limit of 2**256 - 1 to the number of tokens
  14. * that can be minted.
  15. *
  16. * CAUTION: This extension should not be added in an upgrade to an already deployed contract.
  17. */
  18. abstract contract ERC1155Supply is ERC1155 {
  19. mapping(uint256 id => uint256) private _totalSupply;
  20. uint256 private _totalSupplyAll;
  21. /**
  22. * @dev Total value of tokens in with a given id.
  23. */
  24. function totalSupply(uint256 id) public view virtual returns (uint256) {
  25. return _totalSupply[id];
  26. }
  27. /**
  28. * @dev Total value of tokens.
  29. */
  30. function totalSupply() public view virtual returns (uint256) {
  31. return _totalSupplyAll;
  32. }
  33. /**
  34. * @dev Indicates whether any token exist with a given id, or not.
  35. */
  36. function exists(uint256 id) public view virtual returns (bool) {
  37. return totalSupply(id) > 0;
  38. }
  39. /**
  40. * @dev See {ERC1155-_update}.
  41. */
  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[i];
  53. // Overflow check required: The rest of the code assumes that totalSupply never overflows
  54. _totalSupply[ids[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[i];
  64. unchecked {
  65. // Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
  66. _totalSupply[ids[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. }