ERC1155Supply.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC1155.sol";
  4. /**
  5. * @dev Extension of ERC1155 that adds tracking of total supply per id.
  6. *
  7. * Useful for scenarios where Fungible and Non-fungible tokens have to be
  8. * clearly identified. Note: While a totalSupply of 1 might mean the
  9. * corresponding is an NFT, there is no guarantees that no other token with the
  10. * same id are not going to be minted.
  11. */
  12. abstract contract ERC1155Supply is ERC1155 {
  13. mapping(uint256 => uint256) private _totalSupply;
  14. /**
  15. * @dev Total amount of tokens in with a given id.
  16. */
  17. function totalSupply(uint256 id) public view virtual returns (uint256) {
  18. return _totalSupply[id];
  19. }
  20. /**
  21. * @dev Indicates weither any token exist with a given id, or not.
  22. */
  23. function exists(uint256 id) public view virtual returns (bool) {
  24. return ERC1155Supply.totalSupply(id) > 0;
  25. }
  26. /**
  27. * @dev See {ERC1155-_mint}.
  28. */
  29. function _mint(
  30. address account,
  31. uint256 id,
  32. uint256 amount,
  33. bytes memory data
  34. ) internal virtual override {
  35. super._mint(account, id, amount, data);
  36. _totalSupply[id] += amount;
  37. }
  38. /**
  39. * @dev See {ERC1155-_mintBatch}.
  40. */
  41. function _mintBatch(
  42. address to,
  43. uint256[] memory ids,
  44. uint256[] memory amounts,
  45. bytes memory data
  46. ) internal virtual override {
  47. super._mintBatch(to, ids, amounts, data);
  48. for (uint256 i = 0; i < ids.length; ++i) {
  49. _totalSupply[ids[i]] += amounts[i];
  50. }
  51. }
  52. /**
  53. * @dev See {ERC1155-_burn}.
  54. */
  55. function _burn(
  56. address account,
  57. uint256 id,
  58. uint256 amount
  59. ) internal virtual override {
  60. super._burn(account, id, amount);
  61. _totalSupply[id] -= amount;
  62. }
  63. /**
  64. * @dev See {ERC1155-_burnBatch}.
  65. */
  66. function _burnBatch(
  67. address account,
  68. uint256[] memory ids,
  69. uint256[] memory amounts
  70. ) internal virtual override {
  71. super._burnBatch(account, ids, amounts);
  72. for (uint256 i = 0; i < ids.length; ++i) {
  73. _totalSupply[ids[i]] -= amounts[i];
  74. }
  75. }
  76. }