ERC1155SupplyUpgradeable.sol 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (token/ERC1155/extensions/ERC1155Supply.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC1155Upgradeable.sol";
  5. import "../../../proxy/utils/Initializable.sol";
  6. /**
  7. * @dev Extension of ERC1155 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. abstract contract ERC1155SupplyUpgradeable is Initializable, ERC1155Upgradeable {
  15. function __ERC1155Supply_init() internal onlyInitializing {
  16. }
  17. function __ERC1155Supply_init_unchained() internal onlyInitializing {
  18. }
  19. mapping(uint256 => uint256) private _totalSupply;
  20. /**
  21. * @dev Total amount of tokens in with a given id.
  22. */
  23. function totalSupply(uint256 id) public view virtual returns (uint256) {
  24. return _totalSupply[id];
  25. }
  26. /**
  27. * @dev Indicates whether any token exist with a given id, or not.
  28. */
  29. function exists(uint256 id) public view virtual returns (bool) {
  30. return ERC1155SupplyUpgradeable.totalSupply(id) > 0;
  31. }
  32. /**
  33. * @dev See {ERC1155-_beforeTokenTransfer}.
  34. */
  35. function _beforeTokenTransfer(
  36. address operator,
  37. address from,
  38. address to,
  39. uint256[] memory ids,
  40. uint256[] memory amounts,
  41. bytes memory data
  42. ) internal virtual override {
  43. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  44. if (from == address(0)) {
  45. for (uint256 i = 0; i < ids.length; ++i) {
  46. _totalSupply[ids[i]] += amounts[i];
  47. }
  48. }
  49. if (to == address(0)) {
  50. for (uint256 i = 0; i < ids.length; ++i) {
  51. _totalSupply[ids[i]] -= amounts[i];
  52. }
  53. }
  54. }
  55. /**
  56. * This empty reserved space is put in place to allow future versions to add new
  57. * variables without shifting down storage in the inheritance chain.
  58. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  59. */
  60. uint256[49] private __gap;
  61. }