draft-ERC6909Metadata.sol 2.4 KB

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