ERC1155.sol 17 KB

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