ERC1155.sol 14 KB

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