ERC1155.sol 14 KB

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