draft-ERC6909TokenSupply.sol 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (token/ERC6909/extensions/draft-ERC6909TokenSupply.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC6909} from "../draft-ERC6909.sol";
  5. import {IERC6909TokenSupply} from "../../../interfaces/draft-IERC6909.sol";
  6. /**
  7. * @dev Implementation of the Token Supply extension defined in ERC6909.
  8. * Tracks the total supply of each token id individually.
  9. */
  10. contract ERC6909TokenSupply is ERC6909, IERC6909TokenSupply {
  11. mapping(uint256 id => uint256) private _totalSupplies;
  12. /// @inheritdoc IERC6909TokenSupply
  13. function totalSupply(uint256 id) public view virtual override returns (uint256) {
  14. return _totalSupplies[id];
  15. }
  16. /// @dev Override the `_update` function to update the total supply of each token id as necessary.
  17. function _update(address from, address to, uint256 id, uint256 amount) internal virtual override {
  18. super._update(from, to, id, amount);
  19. if (from == address(0)) {
  20. _totalSupplies[id] += amount;
  21. }
  22. if (to == address(0)) {
  23. unchecked {
  24. // amount <= _balances[from][id] <= _totalSupplies[id]
  25. _totalSupplies[id] -= amount;
  26. }
  27. }
  28. }
  29. }