|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.8.30; |
| 3 | + |
| 4 | +/* Compose |
| 5 | + * https://compose.diamonds |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * @title ERC-1155 Multi Token Standard |
| 10 | + * |
| 11 | + */ |
| 12 | +contract ERC1155DataFacet { |
| 13 | + /** |
| 14 | + * @notice Error indicating array length mismatch in batch operations. |
| 15 | + * @param _idsLength Length of the ids array. |
| 16 | + * @param _valuesLength Length of the values array. |
| 17 | + */ |
| 18 | + error ERC1155InvalidArrayLength(uint256 _idsLength, uint256 _valuesLength); |
| 19 | + |
| 20 | + /** |
| 21 | + * @dev Storage position determined by the keccak256 hash of the diamond storage identifier. |
| 22 | + */ |
| 23 | + bytes32 constant STORAGE_POSITION = keccak256("erc1155"); |
| 24 | + |
| 25 | + /** |
| 26 | + * @dev ERC-8042 compliant storage struct for ERC-1155 token data. |
| 27 | + * @custom:storage-location erc8042:erc1155 |
| 28 | + */ |
| 29 | + struct ERC1155Storage { |
| 30 | + mapping(uint256 id => mapping(address account => uint256 balance)) balanceOf; |
| 31 | + mapping(address account => mapping(address operator => bool)) isApprovedForAll; |
| 32 | + string uri; |
| 33 | + string baseURI; |
| 34 | + mapping(uint256 tokenId => string) tokenURIs; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @notice Returns the ERC-1155 storage struct from the predefined diamond storage slot. |
| 39 | + * @dev Uses inline assembly to set the storage slot reference. |
| 40 | + * @return s The ERC-1155 storage struct reference. |
| 41 | + */ |
| 42 | + function getStorage() internal pure returns (ERC1155Storage storage s) { |
| 43 | + bytes32 position = STORAGE_POSITION; |
| 44 | + assembly { |
| 45 | + s.slot := position |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @notice Returns the amount of tokens of token type `id` owned by `account`. |
| 51 | + * @param _account The address to query the balance of. |
| 52 | + * @param _id The token type to query. |
| 53 | + * @return The balance of the token type. |
| 54 | + */ |
| 55 | + function balanceOf(address _account, uint256 _id) external view returns (uint256) { |
| 56 | + return getStorage().balanceOf[_id][_account]; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @notice Batched version of {balanceOf}. |
| 61 | + * @param _accounts The addresses to query the balances of. |
| 62 | + * @param _ids The token types to query. |
| 63 | + * @return balances The balances of the token types. |
| 64 | + */ |
| 65 | + function balanceOfBatch(address[] calldata _accounts, uint256[] calldata _ids) |
| 66 | + external |
| 67 | + view |
| 68 | + returns (uint256[] memory balances) |
| 69 | + { |
| 70 | + if (_accounts.length != _ids.length) { |
| 71 | + revert ERC1155InvalidArrayLength(_ids.length, _accounts.length); |
| 72 | + } |
| 73 | + |
| 74 | + ERC1155Storage storage s = getStorage(); |
| 75 | + balances = new uint256[](_accounts.length); |
| 76 | + |
| 77 | + for (uint256 i = 0; i < _accounts.length; i++) { |
| 78 | + balances[i] = s.balanceOf[_ids[i]][_accounts[i]]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @notice Returns true if `operator` is approved to transfer `account`'s tokens. |
| 84 | + * @param _account The token owner. |
| 85 | + * @param _operator The operator to query. |
| 86 | + * @return True if the operator is approved, false otherwise. |
| 87 | + */ |
| 88 | + function isApprovedForAll(address _account, address _operator) external view returns (bool) { |
| 89 | + return getStorage().isApprovedForAll[_account][_operator]; |
| 90 | + } |
| 91 | +} |
0 commit comments