draft-ERC6909Metadata.sol 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (token/ERC6909/extensions/draft-ERC6909Metadata.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC6909} from "../draft-ERC6909.sol";
  5. import {IERC6909Metadata} from "../../../interfaces/draft-IERC6909.sol";
  6. /**
  7. * @dev Implementation of the Metadata extension defined in ERC6909. Exposes the name, symbol, and decimals of each token id.
  8. */
  9. contract ERC6909Metadata is ERC6909, IERC6909Metadata {
  10. struct TokenMetadata {
  11. string name;
  12. string symbol;
  13. uint8 decimals;
  14. }
  15. mapping(uint256 id => TokenMetadata) private _tokenMetadata;
  16. /// @dev The name of the token of type `id` was updated to `newName`.
  17. event ERC6909NameUpdated(uint256 indexed id, string newName);
  18. /// @dev The symbol for the token of type `id` was updated to `newSymbol`.
  19. event ERC6909SymbolUpdated(uint256 indexed id, string newSymbol);
  20. /// @dev The decimals value for token of type `id` was updated to `newDecimals`.
  21. event ERC6909DecimalsUpdated(uint256 indexed id, uint8 newDecimals);
  22. /// @inheritdoc IERC6909Metadata
  23. function name(uint256 id) public view virtual override returns (string memory) {
  24. return _tokenMetadata[id].name;
  25. }
  26. /// @inheritdoc IERC6909Metadata
  27. function symbol(uint256 id) public view virtual override returns (string memory) {
  28. return _tokenMetadata[id].symbol;
  29. }
  30. /// @inheritdoc IERC6909Metadata
  31. function decimals(uint256 id) public view virtual override returns (uint8) {
  32. return _tokenMetadata[id].decimals;
  33. }
  34. /**
  35. * @dev Sets the `name` for a given token of type `id`.
  36. *
  37. * Emits an {ERC6909NameUpdated} event.
  38. */
  39. function _setName(uint256 id, string memory newName) internal virtual {
  40. _tokenMetadata[id].name = newName;
  41. emit ERC6909NameUpdated(id, newName);
  42. }
  43. /**
  44. * @dev Sets the `symbol` for a given token of type `id`.
  45. *
  46. * Emits an {ERC6909SymbolUpdated} event.
  47. */
  48. function _setSymbol(uint256 id, string memory newSymbol) internal virtual {
  49. _tokenMetadata[id].symbol = newSymbol;
  50. emit ERC6909SymbolUpdated(id, newSymbol);
  51. }
  52. /**
  53. * @dev Sets the `decimals` for a given token of type `id`.
  54. *
  55. * Emits an {ERC6909DecimalsUpdated} event.
  56. */
  57. function _setDecimals(uint256 id, uint8 newDecimals) internal virtual {
  58. _tokenMetadata[id].decimals = newDecimals;
  59. emit ERC6909DecimalsUpdated(id, newDecimals);
  60. }
  61. }