draft-ERC6909TokenSupply.sol 1.1 KB

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