diff --git a/contracts/token/ERC1155/ERC1155Supply.sol b/contracts/token/ERC1155/ERC1155Supply.sol deleted file mode 100644 index 5b75f5f9f0f..00000000000 --- a/contracts/token/ERC1155/ERC1155Supply.sol +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity >=0.6.0 <0.9.0; - -import "./ERC1155.sol"; - -/** - * @dev ERC1155 token with totalSupply (per id). - * - * Useful for scenarios where Fungible and Non-fungible tokens have to be - * clearly identified. Note: While a totalSupply of 1 might mean the - * corresponding is an NFT, there is no guaranties that no other token with the - * same id are not going to be minted - */ -abstract contract ERC1155Supply is ERC1155 { - using SafeMath for uint256; - - mapping (uint256 => uint256) private _totalSupply; - - /** - * @dev Total amount of tokens in with a given id. - */ - function totalSupply(uint256 id) public view returns (uint256) { - return _totalSupply[id]; - } - - /** - * @dev Indicates weither any token exist with a given id, or not. - */ - function _exists(uint256 id) internal view returns(bool) { - return _totalSupply[id] > 0; - } - - /** - * @dev See {ERC1155-_beforeTokenTransfer}. - */ - function _beforeTokenTransfer( - address operator, - address from, - address to, - uint256[] memory ids, - uint256[] memory amounts, - bytes memory data - ) - internal - virtual - override - { - for (uint256 i = 0; i < ids.length; ++i) { - uint256 id = ids[i]; - uint256 amount = amounts[i]; - if (from == address(0)) { - _totalSupply[id] = _totalSupply[id].add(amount); - } - if (to == address(0)) { - _totalSupply[id] = _totalSupply[id].sub(amount); - } - } - super._beforeTokenTransfer(operator, from, to, ids, amounts, data); - } -} diff --git a/contracts/token/ERC1155/extensions/ERC1155Supply.sol b/contracts/token/ERC1155/extensions/ERC1155Supply.sol new file mode 100644 index 00000000000..3a0fe555ede --- /dev/null +++ b/contracts/token/ERC1155/extensions/ERC1155Supply.sol @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "../ERC1155.sol"; +import "../../../utils/math/SafeMath.sol"; + +/** + * @dev ERC1155 token with totalSupply (per id). + * + * Useful for scenarios where Fungible and Non-fungible tokens have to be + * clearly identified. Note: While a totalSupply of 1 might mean the + * corresponding is an NFT, there is no guaranties that no other token with the + * same id are not going to be minted + */ +abstract contract ERC1155Supply is ERC1155 { + using SafeMath for uint256; + + mapping (uint256 => uint256) private _totalSupply; + + /** + * @dev Total amount of tokens in with a given id. + */ + function totalSupply(uint256 id) public view virtual returns (uint256) { + return _totalSupply[id]; + } + + /** + * @dev Indicates weither any token exist with a given id, or not. + */ + function exists(uint256 id) public view virtual returns(bool) { + return ERC1155Supply.totalSupply(id) > 0; + } + + function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual override { + super._mint(account, id, amount, data); + _totalSupply[id] = _totalSupply[id].add(amount); + } + + function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal virtual override { + super._mintBatch(to, ids, amounts, data); + for (uint256 i = 0; i < ids.length; ++i) { + _totalSupply[ids[i]] = _totalSupply[ids[i]].add(amounts[i]); + } + } + + function _burn(address account, uint256 id, uint256 amount) internal virtual override { + super._burn(account, id, amount); + _totalSupply[id] = _totalSupply[id].sub(amount); + } + + function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual override { + super._burnBatch(account, ids, amounts); + for (uint256 i = 0; i < ids.length; ++i) { + _totalSupply[ids[i]] = _totalSupply[ids[i]].sub(amounts[i]); + } + } +}