ERC1155.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.sol";
  5. import "./IERC1155Receiver.sol";
  6. import "./extensions/IERC1155MetadataURI.sol";
  7. import "../../utils/Context.sol";
  8. import "../../utils/introspection/ERC165.sol";
  9. import "../../utils/Arrays.sol";
  10. import "../../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) public view virtual returns (string memory) {
  53. return _uri;
  54. }
  55. /**
  56. * @dev See {IERC1155-balanceOf}.
  57. *
  58. * Requirements:
  59. *
  60. * - `account` cannot be the zero address.
  61. */
  62. function balanceOf(address account, uint256 id) public view virtual returns (uint256) {
  63. return _balances[id][account];
  64. }
  65. /**
  66. * @dev See {IERC1155-balanceOfBatch}.
  67. *
  68. * Requirements:
  69. *
  70. * - `accounts` and `ids` must have the same length.
  71. */
  72. function balanceOfBatch(
  73. address[] memory accounts,
  74. uint256[] memory ids
  75. ) public view virtual returns (uint256[] memory) {
  76. if (accounts.length != ids.length) {
  77. revert ERC1155InvalidArrayLength(ids.length, accounts.length);
  78. }
  79. uint256[] memory batchBalances = new uint256[](accounts.length);
  80. for (uint256 i = 0; i < accounts.length; ++i) {
  81. batchBalances[i] = balanceOf(accounts.unsafeMemoryAccess(i), ids.unsafeMemoryAccess(i));
  82. }
  83. return batchBalances;
  84. }
  85. /**
  86. * @dev See {IERC1155-setApprovalForAll}.
  87. */
  88. function setApprovalForAll(address operator, bool approved) public virtual {
  89. _setApprovalForAll(_msgSender(), operator, approved);
  90. }
  91. /**
  92. * @dev See {IERC1155-isApprovedForAll}.
  93. */
  94. function isApprovedForAll(address account, address operator) public view virtual returns (bool) {
  95. return _operatorApprovals[account][operator];
  96. }
  97. /**
  98. * @dev See {IERC1155-safeTransferFrom}.
  99. */
  100. function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) public virtual {
  101. if (from != _msgSender() && !isApprovedForAll(from, _msgSender())) {
  102. revert ERC1155InsufficientApprovalForAll(_msgSender(), from);
  103. }
  104. _safeTransferFrom(from, to, id, amount, data);
  105. }
  106. /**
  107. * @dev See {IERC1155-safeBatchTransferFrom}.
  108. */
  109. function safeBatchTransferFrom(
  110. address from,
  111. address to,
  112. uint256[] memory ids,
  113. uint256[] memory amounts,
  114. bytes memory data
  115. ) public virtual {
  116. if (from != _msgSender() && !isApprovedForAll(from, _msgSender())) {
  117. revert ERC1155InsufficientApprovalForAll(_msgSender(), from);
  118. }
  119. _safeBatchTransferFrom(from, to, ids, amounts, data);
  120. }
  121. /**
  122. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`. Will mint (or burn) if `from` (or `to`) is the zero address.
  123. *
  124. * Emits a {TransferSingle} event if the arrays contain one element, and {TransferBatch} otherwise.
  125. *
  126. * Requirements:
  127. *
  128. * - If `to` refers to a smart contract, it must implement either {IERC1155Receiver-onERC1155Received}
  129. * or {IERC1155Receiver-onERC1155BatchReceived} and return the acceptance magic value.
  130. */
  131. function _update(
  132. address from,
  133. address to,
  134. uint256[] memory ids,
  135. uint256[] memory amounts,
  136. bytes memory data
  137. ) internal virtual {
  138. if (ids.length != amounts.length) {
  139. revert ERC1155InvalidArrayLength(ids.length, amounts.length);
  140. }
  141. address operator = _msgSender();
  142. for (uint256 i = 0; i < ids.length; ++i) {
  143. uint256 id = ids.unsafeMemoryAccess(i);
  144. uint256 amount = amounts.unsafeMemoryAccess(i);
  145. if (from != address(0)) {
  146. uint256 fromBalance = _balances[id][from];
  147. if (fromBalance < amount) {
  148. revert ERC1155InsufficientBalance(from, fromBalance, amount, id);
  149. }
  150. unchecked {
  151. _balances[id][from] = fromBalance - amount;
  152. }
  153. }
  154. if (to != address(0)) {
  155. _balances[id][to] += amount;
  156. }
  157. }
  158. if (ids.length == 1) {
  159. uint256 id = ids.unsafeMemoryAccess(0);
  160. uint256 amount = amounts.unsafeMemoryAccess(0);
  161. emit TransferSingle(operator, from, to, id, amount);
  162. if (to != address(0)) {
  163. _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
  164. }
  165. } else {
  166. emit TransferBatch(operator, from, to, ids, amounts);
  167. if (to != address(0)) {
  168. _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
  169. }
  170. }
  171. }
  172. /**
  173. * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
  174. *
  175. * Emits a {TransferSingle} event.
  176. *
  177. * Requirements:
  178. *
  179. * - `to` cannot be the zero address.
  180. * - `from` must have a balance of tokens of type `id` of at least `amount`.
  181. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  182. * acceptance magic value.
  183. */
  184. function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
  185. if (to == address(0)) {
  186. revert ERC1155InvalidReceiver(address(0));
  187. }
  188. if (from == address(0)) {
  189. revert ERC1155InvalidSender(address(0));
  190. }
  191. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  192. _update(from, to, ids, amounts, data);
  193. }
  194. /**
  195. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
  196. *
  197. * Emits a {TransferBatch} event.
  198. *
  199. * Requirements:
  200. *
  201. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  202. * acceptance magic value.
  203. */
  204. function _safeBatchTransferFrom(
  205. address from,
  206. address to,
  207. uint256[] memory ids,
  208. uint256[] memory amounts,
  209. bytes memory data
  210. ) internal {
  211. if (to == address(0)) {
  212. revert ERC1155InvalidReceiver(address(0));
  213. }
  214. if (from == address(0)) {
  215. revert ERC1155InvalidSender(address(0));
  216. }
  217. _update(from, to, ids, amounts, data);
  218. }
  219. /**
  220. * @dev Sets a new URI for all token types, by relying on the token type ID
  221. * substitution mechanism
  222. * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
  223. *
  224. * By this mechanism, any occurrence of the `\{id\}` substring in either the
  225. * URI or any of the amounts in the JSON file at said URI will be replaced by
  226. * clients with the token type ID.
  227. *
  228. * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
  229. * interpreted by clients as
  230. * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
  231. * for token type ID 0x4cce0.
  232. *
  233. * See {uri}.
  234. *
  235. * Because these URIs cannot be meaningfully represented by the {URI} event,
  236. * this function emits no events.
  237. */
  238. function _setURI(string memory newuri) internal virtual {
  239. _uri = newuri;
  240. }
  241. /**
  242. * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
  243. *
  244. * Emits a {TransferSingle} event.
  245. *
  246. * Requirements:
  247. *
  248. * - `to` cannot be the zero address.
  249. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
  250. * acceptance magic value.
  251. */
  252. function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
  253. if (to == address(0)) {
  254. revert ERC1155InvalidReceiver(address(0));
  255. }
  256. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  257. _update(address(0), to, ids, amounts, data);
  258. }
  259. /**
  260. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
  261. *
  262. * Emits a {TransferBatch} event.
  263. *
  264. * Requirements:
  265. *
  266. * - `ids` and `amounts` must have the same length.
  267. * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
  268. * acceptance magic value.
  269. */
  270. function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal {
  271. if (to == address(0)) {
  272. revert ERC1155InvalidReceiver(address(0));
  273. }
  274. _update(address(0), to, ids, amounts, data);
  275. }
  276. /**
  277. * @dev Destroys `amount` tokens of token type `id` from `from`
  278. *
  279. * Emits a {TransferSingle} event.
  280. *
  281. * Requirements:
  282. *
  283. * - `from` cannot be the zero address.
  284. * - `from` must have at least `amount` tokens of token type `id`.
  285. */
  286. function _burn(address from, uint256 id, uint256 amount) internal {
  287. if (from == address(0)) {
  288. revert ERC1155InvalidSender(address(0));
  289. }
  290. (uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
  291. _update(from, address(0), ids, amounts, "");
  292. }
  293. /**
  294. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
  295. *
  296. * Emits a {TransferBatch} event.
  297. *
  298. * Requirements:
  299. *
  300. * - `ids` and `amounts` must have the same length.
  301. */
  302. function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal {
  303. if (from == address(0)) {
  304. revert ERC1155InvalidSender(address(0));
  305. }
  306. _update(from, address(0), ids, amounts, "");
  307. }
  308. /**
  309. * @dev Approve `operator` to operate on all of `owner` tokens
  310. *
  311. * Emits an {ApprovalForAll} event.
  312. */
  313. function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  314. if (owner == operator) {
  315. revert ERC1155InvalidOperator(operator);
  316. }
  317. _operatorApprovals[owner][operator] = approved;
  318. emit ApprovalForAll(owner, operator, approved);
  319. }
  320. function _doSafeTransferAcceptanceCheck(
  321. address operator,
  322. address from,
  323. address to,
  324. uint256 id,
  325. uint256 amount,
  326. bytes memory data
  327. ) private {
  328. if (to.code.length > 0) {
  329. try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  330. if (response != IERC1155Receiver.onERC1155Received.selector) {
  331. // Tokens rejected
  332. revert ERC1155InvalidReceiver(to);
  333. }
  334. } catch (bytes memory reason) {
  335. if (reason.length == 0) {
  336. // non-ERC1155Receiver implementer
  337. revert ERC1155InvalidReceiver(to);
  338. } else {
  339. /// @solidity memory-safe-assembly
  340. assembly {
  341. revert(add(32, reason), mload(reason))
  342. }
  343. }
  344. }
  345. }
  346. }
  347. function _doSafeBatchTransferAcceptanceCheck(
  348. address operator,
  349. address from,
  350. address to,
  351. uint256[] memory ids,
  352. uint256[] memory amounts,
  353. bytes memory data
  354. ) private {
  355. if (to.code.length > 0) {
  356. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
  357. bytes4 response
  358. ) {
  359. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  360. // Tokens rejected
  361. revert ERC1155InvalidReceiver(to);
  362. }
  363. } catch (bytes memory reason) {
  364. if (reason.length == 0) {
  365. // non-ERC1155Receiver implementer
  366. revert ERC1155InvalidReceiver(to);
  367. } else {
  368. /// @solidity memory-safe-assembly
  369. assembly {
  370. revert(add(32, reason), mload(reason))
  371. }
  372. }
  373. }
  374. }
  375. }
  376. function _asSingletonArrays(
  377. uint256 element1,
  378. uint256 element2
  379. ) private pure returns (uint256[] memory array1, uint256[] memory array2) {
  380. /// @solidity memory-safe-assembly
  381. assembly {
  382. array1 := mload(0x40)
  383. mstore(array1, 1)
  384. mstore(add(array1, 0x20), element1)
  385. array2 := add(array1, 0x40)
  386. mstore(array2, 1)
  387. mstore(add(array2, 0x20), element2)
  388. mstore(0x40, add(array2, 0x40))
  389. }
  390. }
  391. }