ERC1155.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (token/ERC1155/ERC1155.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC1155} from "./IERC1155.sol";
  5. import {IERC1155MetadataURI} from "./extensions/IERC1155MetadataURI.sol";
  6. import {ERC1155Utils} from "./utils/ERC1155Utils.sol";
  7. import {Context} from "../../utils/Context.sol";
  8. import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol";
  9. import {Arrays} from "../../utils/Arrays.sol";
  10. import {IERC1155Errors} from "../../interfaces/draft-IERC6093.sol";
  11. /**
  12. * @dev Implementation of the basic standard multi-token.
  13. * See https://eips.ethereum.org/EIPS/eip-1155
  14. * Originally based on code by Enjin: https://github.com/enjin/erc-1155
  15. */
  16. abstract contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI, IERC1155Errors {
  17. using Arrays for uint256[];
  18. using Arrays for address[];
  19. mapping(uint256 id => mapping(address account => uint256)) private _balances;
  20. mapping(address account => mapping(address operator => bool)) private _operatorApprovals;
  21. // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
  22. string private _uri;
  23. /**
  24. * @dev See {_setURI}.
  25. */
  26. constructor(string memory uri_) {
  27. _setURI(uri_);
  28. }
  29. /// @inheritdoc IERC165
  30. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  31. return
  32. interfaceId == type(IERC1155).interfaceId ||
  33. interfaceId == type(IERC1155MetadataURI).interfaceId ||
  34. super.supportsInterface(interfaceId);
  35. }
  36. /**
  37. * @dev See {IERC1155MetadataURI-uri}.
  38. *
  39. * This implementation returns the same URI for *all* token types. It relies
  40. * on the token type ID substitution mechanism
  41. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC].
  42. *
  43. * Clients calling this function must replace the `\{id\}` substring with the
  44. * actual token type ID.
  45. */
  46. function uri(uint256 /* id */) public view virtual returns (string memory) {
  47. return _uri;
  48. }
  49. /// @inheritdoc IERC1155
  50. function balanceOf(address account, uint256 id) public view virtual returns (uint256) {
  51. return _balances[id][account];
  52. }
  53. /**
  54. * @dev See {IERC1155-balanceOfBatch}.
  55. *
  56. * Requirements:
  57. *
  58. * - `accounts` and `ids` must have the same length.
  59. */
  60. function balanceOfBatch(
  61. address[] memory accounts,
  62. uint256[] memory ids
  63. ) public view virtual returns (uint256[] memory) {
  64. if (accounts.length != ids.length) {
  65. revert ERC1155InvalidArrayLength(ids.length, accounts.length);
  66. }
  67. uint256[] memory batchBalances = new uint256[](accounts.length);
  68. for (uint256 i = 0; i < accounts.length; ++i) {
  69. batchBalances[i] = balanceOf(accounts.unsafeMemoryAccess(i), ids.unsafeMemoryAccess(i));
  70. }
  71. return batchBalances;
  72. }
  73. /// @inheritdoc IERC1155
  74. function setApprovalForAll(address operator, bool approved) public virtual {
  75. _setApprovalForAll(_msgSender(), operator, approved);
  76. }
  77. /// @inheritdoc IERC1155
  78. function isApprovedForAll(address account, address operator) public view virtual returns (bool) {
  79. return _operatorApprovals[account][operator];
  80. }
  81. /// @inheritdoc IERC1155
  82. function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) public virtual {
  83. address sender = _msgSender();
  84. if (from != sender && !isApprovedForAll(from, sender)) {
  85. revert ERC1155MissingApprovalForAll(sender, from);
  86. }
  87. _safeTransferFrom(from, to, id, value, data);
  88. }
  89. /// @inheritdoc IERC1155
  90. function safeBatchTransferFrom(
  91. address from,
  92. address to,
  93. uint256[] memory ids,
  94. uint256[] memory values,
  95. bytes memory data
  96. ) public virtual {
  97. address sender = _msgSender();
  98. if (from != sender && !isApprovedForAll(from, sender)) {
  99. revert ERC1155MissingApprovalForAll(sender, from);
  100. }
  101. _safeBatchTransferFrom(from, to, ids, values, data);
  102. }
  103. /**
  104. * @dev Transfers a `value` amount of tokens of type `id` from `from` to `to`. Will mint (or burn) if `from`
  105. * (or `to`) is the zero address.
  106. *
  107. * Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
  108. *
  109. * Requirements:
  110. *
  111. * - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
  112. * or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  113. * - `ids` and `values` must have the same length.
  114. *
  115. * NOTE: The ERC-1155 acceptance check is not performed in this function. See {_updateWithAcceptanceCheck} instead.
  116. */
  117. function _update(address from, address to, uint256[] memory ids, uint256[] memory values) internal virtual {
  118. if (ids.length != values.length) {
  119. revert ERC1155InvalidArrayLength(ids.length, values.length);
  120. }
  121. address operator = _msgSender();
  122. for (uint256 i = 0; i < ids.length; ++i) {
  123. uint256 id = ids.unsafeMemoryAccess(i);
  124. uint256 value = values.unsafeMemoryAccess(i);
  125. if (from != address(0)) {
  126. uint256 fromBalance = _balances[id][from];
  127. if (fromBalance < value) {
  128. revert ERC1155InsufficientBalance(from, fromBalance, value, id);
  129. }
  130. unchecked {
  131. // Overflow not possible: value <= fromBalance
  132. _balances[id][from] = fromBalance - value;
  133. }
  134. }
  135. if (to != address(0)) {
  136. _balances[id][to] += value;
  137. }
  138. }
  139. if (ids.length == 1) {
  140. uint256 id = ids.unsafeMemoryAccess(0);
  141. uint256 value = values.unsafeMemoryAccess(0);
  142. emit TransferSingle(operator, from, to, id, value);
  143. } else {
  144. emit TransferBatch(operator, from, to, ids, values);
  145. }
  146. }
  147. /**
  148. * @dev Version of {_update} that performs the token acceptance check by calling
  149. * {IERC1155Receiver-onERC1155Received} or {IERC1155Receiver-onERC1155BatchReceived} on the receiver address if it
  150. * contains code (eg. is a smart contract at the moment of execution).
  151. *
  152. * IMPORTANT: Overriding this function is discouraged because it poses a reentrancy risk from the receiver. So any
  153. * update to the contract state after this function would break the check-effect-interaction pattern. Consider
  154. * overriding {_update} instead.
  155. */
  156. function _updateWithAcceptanceCheck(
  157. address from,
  158. address to,
  159. uint256[] memory ids,
  160. uint256[] memory values,
  161. bytes memory data
  162. ) internal virtual {
  163. _update(from, to, ids, values);
  164. if (to != address(0)) {
  165. address operator = _msgSender();
  166. if (ids.length == 1) {
  167. uint256 id = ids.unsafeMemoryAccess(0);
  168. uint256 value = values.unsafeMemoryAccess(0);
  169. ERC1155Utils.checkOnERC1155Received(operator, from, to, id, value, data);
  170. } else {
  171. ERC1155Utils.checkOnERC1155BatchReceived(operator, from, to, ids, values, data);
  172. }
  173. }
  174. }
  175. /**
  176. * @dev Transfers a `value` tokens of token type `id` from `from` to `to`.
  177. *
  178. * Emits a {TransferSingle} event.
  179. *
  180. * Requirements:
  181. *
  182. * - `to` cannot be the zero address.
  183. * - `from` must have a balance of tokens of type `id` of at least `value` amount.
  184. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  185. * acceptance magic value.
  186. */
  187. function _safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) internal {
  188. if (to == address(0)) {
  189. revert ERC1155InvalidReceiver(address(0));
  190. }
  191. if (from == address(0)) {
  192. revert ERC1155InvalidSender(address(0));
  193. }
  194. (uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
  195. _updateWithAcceptanceCheck(from, to, ids, values, data);
  196. }
  197. /**
  198. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  199. *
  200. * Emits a {TransferBatch} event.
  201. *
  202. * Requirements:
  203. *
  204. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  205. * acceptance magic value.
  206. * - `ids` and `values` must have the same length.
  207. */
  208. function _safeBatchTransferFrom(
  209. address from,
  210. address to,
  211. uint256[] memory ids,
  212. uint256[] memory values,
  213. bytes memory data
  214. ) internal {
  215. if (to == address(0)) {
  216. revert ERC1155InvalidReceiver(address(0));
  217. }
  218. if (from == address(0)) {
  219. revert ERC1155InvalidSender(address(0));
  220. }
  221. _updateWithAcceptanceCheck(from, to, ids, values, data);
  222. }
  223. /**
  224. * @dev Sets a new URI for all token types, by relying on the token type ID
  225. * substitution mechanism
  226. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the ERC].
  227. *
  228. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  229. * URI or any of the values in the JSON file at said URI will be replaced by
  230. * clients with the token type ID.
  231. *
  232. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  233. * interpreted by clients as
  234. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  235. * for token type ID 0x4cce0.
  236. *
  237. * See {uri}.
  238. *
  239. * Because these URIs cannot be meaningfully represented by the {URI} event,
  240. * this function emits no events.
  241. */
  242. function _setURI(string memory newuri) internal virtual {
  243. _uri = newuri;
  244. }
  245. /**
  246. * @dev Creates a `value` amount of tokens of type `id`, and assigns them to `to`.
  247. *
  248. * Emits a {TransferSingle} event.
  249. *
  250. * Requirements:
  251. *
  252. * - `to` cannot be the zero address.
  253. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  254. * acceptance magic value.
  255. */
  256. function _mint(address to, uint256 id, uint256 value, bytes memory data) internal {
  257. if (to == address(0)) {
  258. revert ERC1155InvalidReceiver(address(0));
  259. }
  260. (uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
  261. _updateWithAcceptanceCheck(address(0), to, ids, values, data);
  262. }
  263. /**
  264. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  265. *
  266. * Emits a {TransferBatch} event.
  267. *
  268. * Requirements:
  269. *
  270. * - `ids` and `values` must have the same length.
  271. * - `to` cannot be the zero address.
  272. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  273. * acceptance magic value.
  274. */
  275. function _mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) internal {
  276. if (to == address(0)) {
  277. revert ERC1155InvalidReceiver(address(0));
  278. }
  279. _updateWithAcceptanceCheck(address(0), to, ids, values, data);
  280. }
  281. /**
  282. * @dev Destroys a `value` amount of tokens of type `id` from `from`
  283. *
  284. * Emits a {TransferSingle} event.
  285. *
  286. * Requirements:
  287. *
  288. * - `from` cannot be the zero address.
  289. * - `from` must have at least `value` amount of tokens of type `id`.
  290. */
  291. function _burn(address from, uint256 id, uint256 value) internal {
  292. if (from == address(0)) {
  293. revert ERC1155InvalidSender(address(0));
  294. }
  295. (uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
  296. _updateWithAcceptanceCheck(from, address(0), ids, values, "");
  297. }
  298. /**
  299. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  300. *
  301. * Emits a {TransferBatch} event.
  302. *
  303. * Requirements:
  304. *
  305. * - `from` cannot be the zero address.
  306. * - `from` must have at least `value` amount of tokens of type `id`.
  307. * - `ids` and `values` must have the same length.
  308. */
  309. function _burnBatch(address from, uint256[] memory ids, uint256[] memory values) internal {
  310. if (from == address(0)) {
  311. revert ERC1155InvalidSender(address(0));
  312. }
  313. _updateWithAcceptanceCheck(from, address(0), ids, values, "");
  314. }
  315. /**
  316. * @dev Approve `operator` to operate on all of `owner` tokens
  317. *
  318. * Emits an {ApprovalForAll} event.
  319. *
  320. * Requirements:
  321. *
  322. * - `operator` cannot be the zero address.
  323. */
  324. function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  325. if (operator == address(0)) {
  326. revert ERC1155InvalidOperator(address(0));
  327. }
  328. _operatorApprovals[owner][operator] = approved;
  329. emit ApprovalForAll(owner, operator, approved);
  330. }
  331. /**
  332. * @dev Creates an array in memory with only one value for each of the elements provided.
  333. */
  334. function _asSingletonArrays(
  335. uint256 element1,
  336. uint256 element2
  337. ) private pure returns (uint256[] memory array1, uint256[] memory array2) {
  338. assembly ("memory-safe") {
  339. // Load the free memory pointer
  340. array1 := mload(0x40)
  341. // Set array length to 1
  342. mstore(array1, 1)
  343. // Store the single element at the next word after the length (where content starts)
  344. mstore(add(array1, 0x20), element1)
  345. // Repeat for next array locating it right after the first array
  346. array2 := add(array1, 0x40)
  347. mstore(array2, 1)
  348. mstore(add(array2, 0x20), element2)
  349. // Update the free memory pointer by pointing after the second array
  350. mstore(0x40, add(array2, 0x40))
  351. }
  352. }
  353. }