ERC1155Supply.sol 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.3.2 (token/ERC1155/extensions/ERC1155Supply.sol)
  3. pragma solidity ^0.8.0;
  4. import "../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. abstract contract ERC1155Supply is ERC1155 {
  14. mapping(uint256 => uint256) private _totalSupply;
  15. /**
  16. * @dev Total amount of tokens in with a given id.
  17. */
  18. function totalSupply(uint256 id) public view virtual returns (uint256) {
  19. return _totalSupply[id];
  20. }
  21. /**
  22. * @dev Indicates whether any token exist with a given id, or not.
  23. */
  24. function exists(uint256 id) public view virtual returns (bool) {
  25. return ERC1155Supply.totalSupply(id) > 0;
  26. }
  27. /**
  28. * @dev See {ERC1155-_mint}.
  29. */
  30. function _mint(
  31. address account,
  32. uint256 id,
  33. uint256 amount,
  34. bytes memory data
  35. ) internal virtual override {
  36. super._mint(account, id, amount, data);
  37. _totalSupply[id] += amount;
  38. }
  39. /**
  40. * @dev See {ERC1155-_mintBatch}.
  41. */
  42. function _mintBatch(
  43. address to,
  44. uint256[] memory ids,
  45. uint256[] memory amounts,
  46. bytes memory data
  47. ) internal virtual override {
  48. super._mintBatch(to, ids, amounts, data);
  49. for (uint256 i = 0; i < ids.length; ++i) {
  50. _totalSupply[ids[i]] += amounts[i];
  51. }
  52. }
  53. /**
  54. * @dev See {ERC1155-_burn}.
  55. */
  56. function _burn(
  57. address account,
  58. uint256 id,
  59. uint256 amount
  60. ) internal virtual override {
  61. super._burn(account, id, amount);
  62. _totalSupply[id] -= amount;
  63. }
  64. /**
  65. * @dev See {ERC1155-_burnBatch}.
  66. */
  67. function _burnBatch(
  68. address account,
  69. uint256[] memory ids,
  70. uint256[] memory amounts
  71. ) internal virtual override {
  72. super._burnBatch(account, ids, amounts);
  73. for (uint256 i = 0; i < ids.length; ++i) {
  74. _totalSupply[ids[i]] -= amounts[i];
  75. }
  76. }
  77. }