ERC1155Supply.sol 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(address account, uint256 id, uint256 amount, bytes memory data) internal virtual override {
  30. super._mint(account, id, amount, data);
  31. _totalSupply[id] += amount;
  32. }
  33. /**
  34. * @dev See {ERC1155-_mintBatch}.
  35. */
  36. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal virtual override {
  37. super._mintBatch(to, ids, amounts, data);
  38. for (uint256 i = 0; i < ids.length; ++i) {
  39. _totalSupply[ids[i]] += amounts[i];
  40. }
  41. }
  42. /**
  43. * @dev See {ERC1155-_burn}.
  44. */
  45. function _burn(address account, uint256 id, uint256 amount) internal virtual override {
  46. super._burn(account, id, amount);
  47. _totalSupply[id] -= amount;
  48. }
  49. /**
  50. * @dev See {ERC1155-_burnBatch}.
  51. */
  52. function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual override {
  53. super._burnBatch(account, ids, amounts);
  54. for (uint256 i = 0; i < ids.length; ++i) {
  55. _totalSupply[ids[i]] -= amounts[i];
  56. }
  57. }
  58. }