ERC1155.sol 17 KB

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